The Git Revert Command
In this post we saw how to reset the HEAD to a specified state (a specific commit) by using the Git reset command. But what if we wanted to just undo a specific commit without resetting the HEAD? In this case, the Git revert command will help us.
In case that you want to revert a specific commit, you can use the git revert command as follows:
git revert commit
This will create a commit for the revert. If you do not want to create a commit for the revert, you can use the -n option:
git revert -n commit
You can specify the commit by
- its hash value,
- its n-th last commit number from HEAD (e.g. HEAD~3),
- its n-th last commit number of another branch (e.g. master~3),
- a range of n-th last commits to be undone, e.g. master~5..master~3. The fifth last and the third last commit will be included.
For full reference see the Git revert documentation.