Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move (or "Undo") last git commit to unstaged area [duplicate]

Tags:

git

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?

like image 715
jlb Avatar asked Jun 01 '12 08:06

jlb


People also ask

How do I revert a commit to unstaged?

To unstage commits on Git, use the “git reset” command with the “–soft” option and specify the commit hash. Alternatively, if you want to unstage your last commit, you can the “HEAD” notation in order to revert it easily. Using the “–soft” argument, changes are kept in your working directory and index.

How do I move all changes since last commit to staging area?

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.

How do I move changes to staging area in git?

git add. The git add command adds a change in the working directory to the staging area. It tells Git that you want to include updates to a particular file in the next commit. However, git add doesn't really affect the repository in any significant way—changes are not actually recorded until you run git commit .


1 Answers

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.

like image 185
Stefan Avatar answered Sep 17 '22 11:09

Stefan