How to undo "git commit --amend" done instead of "git commit"
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.)
# The -C option takes the given commit and reuses the log message and
# authorship information.
git commit -C HEAD@{1}
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
Find your amended commits by:
git log --reflog
Note: You may add --patch
to see the body of the commits for clarity. Same as git reflog
.
then reset your HEAD to any previous commit at the point it was fine by:
git reset SHA1 --hard
Note: Replace SHA1 with your real commit hash. Also note that this command will lose any uncommitted changes, so you may stash them before. Alternatively, use --soft
instead to retain the latest changes and then commit them.
Then cherry-pick the other commit that you need on top of it:
git cherry-pick SHA1