loading...

How to revert back to older commit in git

paramharrison profile image Paramanantham Harrison Originally published at learnwithparam.com on ・3 min read

I have my new FREE workshop on API design for beginners using Node Js and MongoDB. It is my first workshop, let's learn together. You can register for the workshop here

If we mess up something and know that it was working before two or three commits. We often want to go to that previous commit and check it out.

we will see how to do that using git commands,

There are three possible scenarios,

  • Just go to the previous commit and then come back to the latest commit (temporary jump)
  • Go back to the previous commit and modify some code there but don’t want to lose the current update history too
  • Go back to the previous commit and discard all the new updates after that.

Just go to the previous commit and then come back to the latest

This is probably the easiest one. The steps to follow are,

  • git stash to stash any uncommitted changes
  • git log to check the commit hash for the previous commit you are looking for

then

git checkout <commit-hash>

This will automatically go to the commit and show the stale branch. Once you finish what you want to look for in the code, you can once again go back to the latest by running

  • git checkout master or any other branch name if you are in a different branch
  • If you did stash any uncommitted changes, then run git stash pop to pop the uncommitted changes from the stash array

Go back to the previous commit and modify, but keep the latest commits somewhere

This is a typical situation where you don’t want to lose your new commits but want to check and fix something in the old commit.

You can do that same thing until get the commit hash for the previous commit and then,

Check out a new branch from the previous commit,

git checkout -b branch-name <commit-hash>

In this way, you can keep both the latest commit and separate branch to debug and fix from the old commit.

Go back to the previous commit and discard all the latest commit after that

Again it is simple to do,

git reset --hard <commit-hash>

This simply reset to the old commit point and discard all new commits. One disadvantage with this approach is, you lose the history of all the latest commit. Potentially you might lose some relevant code.

This command also won’t work as expected if you have uncommitted changes, so stash it before running this.

There is one more way to achieve this using git revert. If you know the exact number of commits you want to go back in history, then you can do so by,

git revert HEAD~3 # here 3 is the number of the commit you want to go before to the latest commit

One advantage with revert is, it keeps the history and create a new commit to revert all the changes. You can even create your own message by not committing automatically when doing revert.

You can also revert selective commits using commit hashes.

  • git log and find all the commits you want to revert. Copy those commit hashes
git revert hash1 hash2 #... and so on

In this way, you can selectively revert some of the commits and keep the code with proper commit log and history.

I hope you learned some tricks about git. Stay tuned for more git magics and JavaScript tutorials 🎉

Follow me on twitter. I share my reading list and short tips on twitter

Discussion

markdown guide