Learning How to Git: Rolling Back to Previous Commit

Haydar Ali Ismail
4 min readJan 31, 2017

--

If you search for how to rollback to the previous commit you will found that sometimes people use revert and sometimes reset. What are the differences?

Reset vs Revert

Both of them are actually pretty similar when it comes to the actual result. But, they did a contrast different approach. If we let’s say run a git reset --hard HEAD~1, then that means we are rolling back to the previous commit before the current commit. If you still haven’t understood about the relative reference (the HEAD~1 part), then you definitely should check the article below.

By using reset, the commit we have earlier will be gone (deleted from our current branch). The important thing to note is that if we have pushed the deleted commit, we are messing up with the history from other repositories. If your partner has pulled those commits, then it creates an inconsistency.

If you are in the situation where you already pushed the wanna-be deleted commit, we can use other command called revert. Revert also rolling back our commit to specific commit, however instead of deleting the commit we create a new commit that rolling back the state of files to the one we want to keep. To understand it better, let’s go to the example.

Reset and Revert in Action

First, let’s check what our git log --oneline --all --graph --decorate shows.

Okay, so it returns us the list of the commits we have. Now, let’s say I want to roll back to the “Add Create Git Branch article link”. We can use either relative reference or absolute reference in either reset and revert. However, I do prefer the relative reference. So, I’m going to use the reference HEAD~1.

The command for resetting to specific commit is using git reset --hard <reference>. There are actually other parameters other than hard, you can learn more about it later. In this case, I’m going to run git reset --hard HEAD~1.

You will notice that our HEAD -> master is now pointed to the commit we want to keep. The file and code inside our file will be exactly the one from the ‘2b2d784’ commit. If you notice, the other commit is still there. That means that those commits already stored in the remote repository (notice the origin keyword). If this is the case, let’s try using revert.

So, now instead of using reset we will use revert. The command for that will be git revert <reference>. Let’s try to revert to the commit before the current commit.

Oops, it seems now we got a conflict on our file. Let’s check our file.

Okay, so it seems we use revert, Git doesn’t always force to revert the file (most of the case they do it automatically), it gives us a conflict instead. Now, it will be similar to the one when we are resolving conflict from merging. If you haven’t read about that you can check it below.

Wrap Up

Now, we have talked about doing a rollback using either reset or revert in Git. I hope you can learn a thing or two from this story. Thank you for reading.

References

--

--

Haydar Ali Ismail
Haydar Ali Ismail

Written by Haydar Ali Ismail

Half Data Engineer, Half Software Engineer

Responses (1)