== Undoing an errant '_git commit --amend_' Suppose, not entirely hypothetically, that late some evening you commit a change and somewhat later that evening notice that you left something out of it. Sleepily confident that you haven't pushed your first commit you fix the omission with '_git commit --amend_', start updating your copy of the repo at work only to have it throw you into a merge, and discover that not only did you propagate the first commit, you pushed it to [[the public Github repo https://github.com/siebenmann/smtpd/]]. Oops. Now you need to reverse or undo the '_git commit --amend_' (and discard the update that you pulled into your repo copy at work). .pn prewrap on I don't know if the procedure I followed to fix my oops is the completely right one, but it worked and repaired things. Here is what I did: * Use '_git reflog_' to look at recent changes in your repo ([[cf http://gitready.com/intermediate/2009/02/09/reflog-your-safety-net.html]]). If you literally committed and then amended, the top two commits are the relevant ones. For me, I saw: _ae22c04 HEAD@{0}: commit (amend): ...._ \\ _9cae7d1 HEAD@{1}: commit: ..._ This is the amended commit and then the original one. We want to get back to the original commit (which is currently orphaned, [[cf http://whileimautomaton.net/2011/10/26220809]]). * Get your repo back to the original commit. For me: _git reset "HEAD@{1}"_ Various Internet sources suggest using '_git reset --soft_' instead. If I understand Git right, this will leave your amended version of the commit sitting in the git index ready to be immediately committed again. I personally would rather roll all the way back to the stage where I have to '_git add_' things again, which is what the command here did. * Inspect the output of '_git diff_', then make a real (non-amended) commit with '_git add_' and '_git commit_' as usual. * Push the new commit to Github et al. (There is probably some way to get Github and your other repos to accept your new amended commit and forget the old one, but I don't know it offhand, I didn't feel like working it out at the time, and I shouldn't assume that no one has pulled a copy from my Github repo.) To clean up my work repo, I aborted the in-flight merge with '_git merge --abort_' and then re-pulled the master with '_git pull_'. Presumably the work repo now has an orphaned copy of the amended commit just as my home repo does, but I don't really care. If I was really curious I could probably find it with some git command, especially as I know (part of) its hash. (The work and home repos are exact mirrors of each other and I'm only making changes in one at any given point, in this case in the home repo. I have to say that git handily beats _rsync_ for this, although I have to overcome my twitch about possibly committing incomplete work.) PS: The right search terms to find this stuff on the Internet are apparently [undo git commit amend]. Searching for [reverse git commit amend] mostly got me discussions about how to *use* _git commit -amend_. PPS: There may well be better ways to do this in Git (and if so, feel free to mention them in the comments). I'm not entirely hep to Git yet, partly because I want my repos to have a straight linear history if at all possible. Someday I may start doing rebases off private branches to get this, but not so far. (This is the kind of entry that I write because someday I'm going to make this mistake again someday.)