I have several commits already and I need to undo the latest one but to have changes I made in it in place. Please advice how to do that. Thank you.
-
http://stackoverflow.com/questions/927358/git-undo-last-commit – simplyharsh Sep 06 '11 at 09:31
-
Why? Do you need to change that commit (adding/removing changes to it)? – Mat Sep 06 '11 at 09:32
-
I need to change comment and add some other files. – Eugene Sep 06 '11 at 09:35
2 Answers
git reset --soft HEAD^
Note that you should only rewind HEAD if it's not pushed yet.
edit In response to your comment on the original question:
If you only want to edit the most recent commit, Git has a neat feature commit --amend
. Simply use git add
/git rm
/git checkout
until your index is in the state you want it to be (new changes added, some changes thrown away, files removed, …). Then use git commit --amend
, it will pop up vi (or whatever you have configured in core.editor
) with your last commit's commit message for you to edit.
Note, that when I say ›edit the most recent commit‹, Git actually creates a new commit. So don't do it, when the commit was already pulled from other people (i.e. pushed to your public repository)

- 246,190
- 53
- 318
- 364
If you want to update the most recent commit, then amending this commit would do the job:
<do the changes you want to>
git add <missing files>
git add <changed files>
git commit --amend
That's it.
If you need to change comment and add some other files to previous commit, I suggest you do interactive rebase instead.
<do the changes you want to>
git add <missing files>
git commit -a
git rebase -i HEAD~3
In editor that pops up you'll see something like
pick <SHA1> Commit that you wanted to change
pick <SHA2> Next commit that you seem to try to rollback in your question
pick <SHA3> Just committed fixes to SHA1
Change it to be
pick <SHA1> Commit that you wanted to change
f <SHA3> Just committed fixes to SHA1
pick <SHA2> Next commit that you seem to try to rollback in your question
('f' means 'fixup' -- squash SHA1 ans SHA3 and discard commit message of SHA3 one)
Save changes, quit editor. You are done.
That should never be done for commits you've already pushed upstream.

- 7,844
- 1
- 28
- 32
-
What is the difference between `previous` and `most recent` commit? I see no difference. – Eugene Sep 06 '11 at 13:59
-
1The most recent commit is the current one (ie the head of your branch). The previous commit is the parent of the current one. – mgautierfr Sep 06 '11 at 14:09
-
Yeah, sorry for ambiguity here. In my terminology "the most recent" was `HEAD`, "the previous one" was `HEAD^`. – Alexander Poluektov Sep 06 '11 at 14:11