Skip to content

Instantly share code, notes, and snippets.

@tkersey
Created October 8, 2010 22:36
Show Gist options
  • Star 40 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save tkersey/617683 to your computer and use it in GitHub Desktop.
Save tkersey/617683 to your computer and use it in GitHub Desktop.
Git: undo a commit and redo
http://stackoverflow.com/questions/927358/git-undo-last-commit
Undo a commit and redo
$ git commit ...
$ git reset --soft HEAD^ (1)
$ edit (2)
$ git commit -a -c ORIG_HEAD (3)
This is most often done when you remembered what you just committed is incomplete, or you misspelled your commit message, or both. Leaves working tree as it was before "reset".
Make corrections to working tree files.
"reset" copies the old head to .git/ORIG_HEAD; redo the commit by starting with its log message. If you do not need to edit the message further, you can give -C option instead.
@colbypalmer
Copy link

I actually could have used this earlier today. Starred, and thanks for posting Tim. :)

@tkersey
Copy link
Author

tkersey commented Oct 9, 2010

Glad helping myself helped you :)

@dcvezzani
Copy link

Very useful. And this gist has been around a good while. Thanks! I didn't know about ORIG_HEAD.

Since I do this so often, I have found the following useful to add to my ~/.gitconfig:

[alias]
undo = reset --soft HEAD^
recommit = commit -a -c ORIG_HEAD

@foresmac
Copy link

What if you revert a particular commit, then later want to re-commit the same changes?

e.g.:
commit 1
commit 2
commit 3
commit 4

commit 3 has some unfinished changes, but everything else needs pushed upstream. You can git revert 3 as long as that commit has nothing to do the the files you want to push. But the changes are in progress, and I don't want to permanently lose my place. Is there a way to get those particular changes back?

@tannewt
Copy link

tannewt commented Oct 21, 2016

This was super useful but beware that if you had files unstaged during the original commit they will be committed with the commit -a.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment