+2 votes
1 view
in Devops and Agile by (28.1k points)
edited by

How can I go about rolling back to a specific commit in git?

The best answer someone could give me was to use git revert X times until I reach the desired commit.

So let's say I want to revert back to a commit that's 20 commits old, I'd have to run it 20 times.

Is there an easier way to do this?

I can't use reset because this repository is public.

1 Answer

+3 votes
by (48k points)

To roll back to an old git commit try this 

git checkout [CommitHash] .

Note: You should not leave out the dot '.' at the end. This will apply whole changes to the tree.

And if you are in the subdirectory then it will make the changes in the current directory.

For the whole to be changed, run this command on the git project root.

Undo this by using

git reset --hard

NOTE: This will delete all modifications from the working directory.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !