You're close. You need to specify the reset mode. The command you ran without specifying the mode will use the default, which would be equivalent to:
git reset --mixed HEAD~1
I assume you were unhappy with this because even though it removed (or more accurately hid) commit 2, all of the changes in commit 2 remained on your drive as pending changes. At that point, you could just undo all of those pending changes to get where you want to be, which is back at commit 1 as if you never made commit 2 at all. If you still have commit 2, the command you wanted to run was:
git reset --hard HEAD~1
Note that HEAD~1
is identical to the parent of the commit you're currently on. So if you're currently on commit 2, then HEAD~1 would be commit 1. Alternatively, you could also do:
git reset --hard commit-1-ID # commit-1-ID is the hash, e.g. a1b2c3d4
If you already pushed out commit 2 to GitHub, then after you've done the reset, you need to force push your branch back out to replace the copy of the branch on GitHub, like this:
git push --force-with-lease
Side note: you said, "I want to revert commit 2...", and in Git, there is actually a command called revert
which you use to undo changes from a specific commit in a brand new commit. (So in your example you make commit 3 which is the reverse of commit 2, effectively putting your state back to commit 1, but with 2 extra commits in your history.) I can tell that you want reset
instead because you don't want to be able to see commit 2 anymore, but realize the option to revert
does exist if you prefer that.