How to Split Commits

Sometimes in a rush developing, I'll commit two distinct changes in a single commit. From a code perspective, this isn't an issue because the code works. But from a systems perspective you can no longer split changes from A and B. They're forever married.Β 

Splitting those changes into two commits will allow us to keep a better history of the system and allow our pull request to "tell a better story".

We can fix combined commits with an interactive rebase. I use PyCharm for part of this in my regular workflow at work, so rather than providing a concrete example, I'll instead summarize the procedure.

  • git rebase -i origin/mainΒ  (or whatever branch you rebase on to) to start an interactive rebase.
  • Find the commit you want to split and mark it as "edit"
  • git reset HEAD~1
  • Add the files / changes for change A, commit
  • Add the files / changes for change B, commit
  • git rebase --continue

The "secret" is that when you edit stops the rebase after the combined commit. By resetting HEAD~1, we effectively undo that commit. But since it's a soft reset, the changes are not rolled back, just the commit. This allows us to tweak and commit individual parts separately as desired before continuing to the next commit in our branch.