kevinlinxp
12/31/2016 - 9:50 AM

Git:reset

Git:reset

$ git commit -m "Something terribly misguided" (1)
$ git reset HEAD~                              (2)
<< edit files as necessary >>                  (3)
$ git add ...                                  (4)
$ git commit -c ORIG_HEAD                      (5)

(1). This is what you want to undo
(2). This leaves your working tree (the state of your files on disk) unchanged but undoes the commit and leaves the changes you committed unstaged (so they'll appear as "Changes not staged for commit" in git status and you'll need to add them again before committing). If you only want to add more changes to the previous commit, or change the commit message1, you could use git reset --soft HEAD~ instead, which is like git reset HEAD~ but leaves your existing changes staged.
(3). Make corrections to working tree files.
(4). git add anything that you want to include in your new commit.
(5). Commit the changes, reusing the old commit message. reset copied the old head to .git/ORIG_HEAD; commit with -c ORIG_HEAD will open an editor, which initially contains the log message from the old commit and allows you to edit it. If you do not need to edit the message, you could use the -C option.

# Reference: http://stackoverflow.com/questions/927358/how-to-undo-last-commits-in-git
# Solution 1:
# 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}

# Solution 2:
# 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