258

If you want to move a commit to the staging area - that is uncommit it and move all of the changes which were in it into the staging area (effectively putting the branch in the state that it would have been in prior to the commit) - how do you do it? Or is it something that you can't do?

The closest that I know how to do is to copy all of the files that were changed in the commit to somewhere else, reset the branch to the commit before the commit that you're trying to move into the staging area, move all of the copied files back into the repository, and then add them to the staging area. It works, but it's not exactly a nice solution. What I'd like to be able to do is just undo the commit and move its changing into the staging area. Can it be done? And if so, how?

Jonathan M Davis
  • 37,181
  • 17
  • 72
  • 102

3 Answers3

460
git reset --soft HEAD^

This will reset your index to HEAD^ (the previous commit) but leave your changes in the staging area.

There are some handy diagrams in the git-reset docs

If you are on Windows you might need to use this format:

git reset --soft HEAD~1
Neuron
  • 5,141
  • 5
  • 38
  • 59
Abizern
  • 146,289
  • 39
  • 203
  • 257
  • 18
    To clarify, tilde and carot mean different things in git versions. HEAD~1 always follows the first parent of a commit, which might not be what you want if it is a merge commit that has multiple parents. Regarding the Windows cmd shell, you just need to escape the carot with another carot e.g. "git reset --soft HEAD^^" to go back to "HEAD^". This is purely an artifact of the Windows cmd shell, so you shouldn't have to do that if you use a git GUI on Windows. Tilde and carot are explained at http://schacon.github.io/git/git-rev-parse#_specifying_revisions – ahains Jun 04 '15 at 18:52
  • I added a link for the "handy diagrams". Could you quickly check if it leads to the section you meant? Thanks – Neuron Apr 27 '18 at 21:11
  • 1
    Note that for for Mac OS (zsh), you will need to escape the caret: `git reset --soft HEAD\^` – Andrei Savin Jan 14 '21 at 19:27
  • lets say first commit is commit1 and last commit is commit4, is it possible to move only commit 2 to staging area? – fall Jun 24 '21 at 02:54
  • 1
    @fall in a manner of speaking it is possible, through a combination of moving commit 2 to come after commit 4 by using `git rebase -i` and then `git reset --soft head~1` – Abizern Jun 25 '21 at 07:05
  • How to do it for just one file from the last commit? – Sumit Wadhwa Mar 29 '22 at 08:36
19

A Simple Way

  1. Committed files to Staging Area

    git reset --soft HEAD^1

  2. Staging to UnStage :(use "git reset HEAD ..." to unstage)

    git reset HEAD git commands.txt or git reset HEAD *ds.txt

here, *--> all files end with ds.txt to unstage.

Refer the below pic for clarity:

enter image description here

Shiva
  • 20,575
  • 14
  • 82
  • 112
Py-Coder
  • 2,024
  • 1
  • 22
  • 28
12

To move a commit back to the staging area depends on your last commit. If your last commit was the first(or initial) commit of the repo, then you need to execute

git update-ref -d HEAD

If your last commit is not the first(or initial) commit, then execute

git reset HEAD~
Sanyam Gupta
  • 258
  • 3
  • 8