Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to undo "git commit --amend" done instead of "git commit"

I accidentally amended my previous commit. The commit should have been separate to keep history of the changes I made to a particular file.

Is there a way to undo that last commit? If I do something like git reset --hard HEAD^, the first commit also is undone.

(I have not yet pushed to any remote directories)

like image 737
Jesper Rønn-Jensen Avatar asked Sep 22 '09 09:09

Jesper Rønn-Jensen


People also ask

How do I abort a commit amend?

If you delete the commit message (no need to delete the ones starting with # ) you will abort the git commit --amend command. You will get output like this: Aborting commit due to empty commit message.

How do I undo a change without committing?

If you wish to add more changes before committing reverted changes, simply add --no-commit or -n to the revert message. This will prevent git revert from automatically creating a new commit; instead, the revert will appear in the staging index without a commit message.


2 Answers

What you need to do is to create a new commit with the same details as the current HEAD commit, but with the parent as the previous version of HEAD. git reset --soft will move the branch pointer so that the next commit happens on top of a different commit from where the current branch head is now.

# Move the current head so that it's pointing at the old commit # Leave the index intact for redoing the commit. # HEAD@{1} gives you "the commit that HEAD pointed at before  # it was moved to where it currently points at". Note that this is # different from HEAD~1, which gives you "the commit that is the # parent node of the commit that HEAD is currently pointing to." git reset --soft HEAD@{1}  # commit the current tree using the commit details of the previous # HEAD commit. (Note that HEAD@{1} is pointing somewhere different from the # previous command. It's now pointing at the erroneously amended commit.) git commit -C HEAD@{1} 
like image 155
CB Bailey Avatar answered Oct 07 '22 22:10

CB Bailey


use the ref-log:

git branch fixing-things HEAD@{1} git reset fixing-things 

you should then have all your previously amended changes only in your working copy and can commit again

to see a full list of previous indices type git reflog

like image 33
knittl Avatar answered Oct 07 '22 21:10

knittl