tags:

views:

51

answers:

2

Frequently I'll have a workflow like the following:

  1. Commit changes to a group of files
  2. Commit changes to a different group of files
  3. Realize I missed some changes that belong in the first commit
  4. Curse

I can't make use of git commit --amend because it's not the most recent commit that I need to change. What's the best way to add changes to the first commit without touching the second one?

+1  A: 

If you haven't pushed upstream, you can:

git format-patch -1
git reset --hard HEAD~1
//change
git commit --amend
git am patchname
jetru
+5  A: 

You can use git rebase to solve this. Run git rebase -i sha1 where sha1 is the commit hash of the one you want to change. Find the commit you want to change, and replace "pick" with "edit" as described in the comments of the rebase editor. When you continue from there, you can edit that commit.

Note that this will change the sha1 of that commit as well as all children -- in other words, this rewrites the history from that point forward. You can break repos doing this, but if you haven't pushed, it's not as much of a big deal.

Daenyth
@rspeicher: Instead of 'sha1' you might also want to check 'HEAD~N', where N is the number of commits before 'HEAD' where you want to begin your rebase.
3lectrologos
That looks like it. Thank you!
rspeicher