Amend Files to Previous Commit

August 8, 2013

I run into this frequently when doing big a big refactor. I’m carefully committing my changes in nice bisectable units, but then Woah! there’s a change that should be included in a previous commit. Here’s how you fix this up in Git.

Disclaimer: We are modifying Git history. Do not do this if you have already pushed these changes.

$ git log --oneline

39e1678 Removed allowsAirPlayVideo method
6c750a1 Removed UILineBreak enums
ce2c34f Removed all UITextAlignment enums
a5c04ad Remove dispatch_release() calls
8681487 Removed Refresh Header

There we go. My current changes should be included in commit 8681487, “Removed Refresh Header”. Fixing this is easy. Add your changes to the staging area, then run:

git commit --fixup=8681487
git rebase --interactive --autosquash 8681487~1

The first command commits your changes, but modifies the commit message to be the same as 8681487 with “fixup!” appended in front. Running rebase in interactive mode with “autosquash” turned on will move our previous commit right after the commit we’d like to modify and update that commit with our new changes.

Easy as pie!

« | Home | »