216

What's the best way to move your last git commit back into the "Changes not staged" + "Untracked files" areas (with the commit in question being not-pushed / only in your local repo, effectively removing it from HEAD)?

In other words, how do you roll back a commit, but automatically apply that diff to your unstaged area?

jlb
  • 19,090
  • 8
  • 34
  • 65

2 Answers2

401

You can use git reset to set the current branch to the preceding commit, i.e. HEAD^

git reset HEAD^

Adding --soft will keep those files in the index: (ready to be committed)

git reset --soft HEAD^

--soft

(…) This leaves all your changed files "Changes to be committed", as git status would put it.

Stefan
  • 109,145
  • 14
  • 143
  • 218
  • 2
    @Dr_Zaszuś, [here we go](https://git-scm.com/book/en/v2/Git-Tools-Reset-Demystified) (I liked the pictures in the original post better, to be honest ;-) ). – kostix Aug 14 '20 at 18:07
  • 2
    @Dr_Zaszuś, [the original](http://web.archive.org/web/20120511055637/http://git-scm.com/2011/07/11/reset.html) courtesy The Internet Wayback Machine. – kostix Aug 14 '20 at 18:09
  • If your zsh shell returns `zsh: no matches found: HEAD^` when you run this, see: https://stackoverflow.com/q/26292192/12484 – Jon Schneider Jul 14 '22 at 15:40
8

git-extras provides a git undo command which is an easier to remember way of doing the same thing (along with a number of other handy extras, as the name implies).

John
  • 14,944
  • 12
  • 57
  • 57