Thursday, February 24, 2011

Splitting a patch into two parts with Git...

Another cool trick I learned today while working.

Assume you submitted a patch for review and someone said "it's too long, split it into two separate patches". What do you do?

GOAL: Take a past commit and split it into two (or more) separate patches.

In your repo, go back to that specific commit and edit it (see my previous blog post for how to edit an historical commit). While in the edit area for the list of commits be sure to take note of the hash code for the commit you're editing.

Once you're in the commit and ready to edit, type:

git reset HEAD^

to back out your changes so that they're there but not committed. Now type:

git add -p

and pick the specific changes you want for the first patch. Be sure not to pick the changes you want to add to the follow up patch.

Once you've selected the changes, commit the patch with:

git commit -c {HASHCODE}

and use the hashcode you noted earlier.

Now all that's left in your repo are the changes you wanted to move to the separate patch. You can add them using:

git commit -a

and you now have a separate patch! Easy as that.