Back

Explore Courses Blog Tutorials Interview Questions
+2 votes
2 views
in DevOps and Agile by (29.3k 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 (50.2k 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.

Browse Categories

...