Search code examples
gitversion-controlrevert

Git Revert to Nth commit. How to find what N is


I need to do

git revert HEAD~N

Where N is a commit possibly 25-35 commits ago. How can I find what N is without reverting.


Solution

  • Why not just use the hash of the commit you want to revert instead? The HEAD~N syntax is just a shortcut for specifying a commit in relative terms; it gets resolved into a hash anyway by Git.

    First, do git log and find the commit hash of the commit you want to revert to. (If you want a more condensed log, you can use git log --oneline.)

    Then do git revert <hash>.

    Also note that if you're wanting to revert to a commit, as opposed to just reverting a commit, you actually want to use git reset rather than git revert.