gitgit-amend

How do I do an interactive git amend?


How can I undo parts of the last commit, while selecting which parts using an interactive patch mode? This would come in useful when you've accidentally checked in a few lines too many in your last commit, e.g. debug print statements or whitespace changes that your editor did for you.

The naive approach is

git commit --amend --interactive --patch

but this does not seem to work. i.e. it just drops you into an editor to do a rewording.

The ugly approach is

git show HEAD~1:path/to/some/file > path/to-some/file
git add -up
git commit --amend

but this is annoying because it makes you specify the file beforehand, and makes you re-add all patches one by one (rather than allowing you to un-add only bits and pieces)


Solution

  • What about

    git reset -p HEAD~
    git commit --amend --no-edit
    

    First command will allow you to unstage chunks interactively (select y for the parts you want to get out of the commit), then the commit amend will write that into the new commit.