koblas koblas - 3 months ago 19
Git Question

Break a previous commit into multiple commits

Without creating a branch and doing a bunch of funky work on a branch... Is it possible to break a single commit into a few different commits after it's been committed to the local repository?

Answer

git rebase -i will do it.

To split apart your most recent commit, first:

$ git reset HEAD~

Now commit the pieces individually in the usual way, producing as many commits as you need.

If it was farther back in the tree, then

$ git rebase -i HEAD~3

where 3 is how many commits back it is.

When you get the rebase edit screen, find the commit you want to break apart. At the beginning of that line, replace pick with edit (e for short). Save the buffer and exit. Rebase will now stop just after the commit you want to edit. Then:

$ git reset HEAD~

Commit the pieces individually in the usual way, producing as many commits as you need, then

$ git rebase --continue