Categories
Opensource

How to split a git commit

Sometimes you write a piece of code within a context, and such context grows wider and wider, or you simply need all the pieces in one place to make sure it works.

Then during reviews, or to work in parallel it makes sense to split your patch in more logical patchlets. I always needed to ask google. So I decided to write it down here.

Let’s assume $COMMIT is the commit you want to split (set the commit for edit with the edit action):

git rebase -i $COMMIT^

And this will leave your commit changes in the working tree, but you will be back in the previous commit.

git reset HEAD^

loop:

git add -p # the pieces of code you want to
git commit
git rebase --continue

If you were working with gerrit, make sure that only one of your patches (probably the biggest one) keeps the original change ID, so the change can still be tracked, and old comments will be available.

banana split