Git: How to undo a commit
TLDR; To undo a local commit in GIT, type $ git reset HEAD~
(that last character is a tilde, the symbol above the 6, not a dash)
If you’ve made a local commit and have not yet pushed it to a remote repository, just do $ git reset HEAD~
. This will undo your commit and leave the changes unstaged.
Do $ git reset --soft HEAD~
to undo your commit but leave the changes staged (if you just need to add more changes to the commit, for example).
If you’ve already pushed your commit to a remote repo, you’ll need to revert it:
You’ll have to look up the ID, or the SHA (secure hash) for the commit you want to revert. Do this with git log:
$ git log
– the most recent commit is on top. The SHA in this example isa50b22ef7186d5a06248e406f18e265e3fb9b893
$ git revert a50b22ef7186d5a06248e406f18e265e3fb9b893
This will likely automatically open an editor in the terminal to edit your commit message. In my case, it opens VIM. (not seen in the screenshot). You may want to add some comments explaining why you’re reverting the commit. Then save, and you’ll return to your command line, as seen in the screenshot. (To exit VIM without making changes to the commit message, just type :q <enter>
).