Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I "reopen" a git commit?

(Note, I'm not looking for the answer git rebase -i)

In mercurial, I can "reopen" a commit by importing it into my patch queue:

hg qimport tip

The commit is "open" in the sense that it's just like before I had committed it, I can revert, do hg diff, hg status, etc. How do I do this in git?

(Everything I've found on the web suggests git rebase -i and then choose edit, but that's different, because the commit is not "open" in the same way.)

like image 993
Paul Biggar Avatar asked Nov 04 '11 16:11

Paul Biggar


4 Answers

You just need to move your HEAD pointer up without making any changes to your working copy:

git reset --soft HEAD^

Reset moves the pointer, and the soft option specifies that it shouldn't change any of your files. The default is mixed, which will reset your index, and the hard option will actually remove the changes since that commit in your working copy.

HEAD is a "magic" git pointer that is always pointing to the current ref (i.e. the parent of your working copy). The caret (^) indicates the parent. You can use this repeatedly, e.g. HEAD^^ refers to the parent of the last commit.

like image 77
coreyward Avatar answered Nov 13 '22 21:11

coreyward


Assuming you haven't yet pushed to your remote repository, git reset --soft HEAD^ will "reopen" your last commit at the expense of losing your commit message.

like image 44
wulong Avatar answered Nov 13 '22 23:11

wulong


You can achieve the same result using git commit --amend.

See comparison chart between Hg & Git.

like image 35
Sri Sankaran Avatar answered Nov 13 '22 22:11

Sri Sankaran


Scott Chacon has (wonderfully) elaborated about the 'git reset' command: http://progit.org/2011/07/11/reset.html Do not hesitate to have a look at it.

like image 1
Denis Arnaud Avatar answered Nov 13 '22 21:11

Denis Arnaud