GitHub Tricks - Splitting Commits

November 11, 2017


This post is very useful to me, especially when I am trying to manage a couple of commits to be pull-requested. Let's say we have a branch [branch_x] which has [commit_x]. Now I want to break this commit into [commit_a] and [commit_b], although [commit_x] has already been pushed onto the repository. What should I do?


Step 1: Rebase the branch interactively.

$ git rebase -i [hash_of_the_commit_before_commit_x]

Step 2: In the iterative editor, mark as edit; save and exit.

Step 3: Advance the head of the rebasing process.

$ git reset HEAD~

Step 4: Git add and commit as many as necessary.

Step 5: After all the files are committed, continue the rebase process.

$ git rebase --continue

Step 6: Push by force if necessary onto the GitHub repository.


A typical use case, is that I have to split one commit and rearrange some of the split commits into [branch_a], while leaving the rest into [branch_b]. In this case I can cherry-pick [commit_a] into [branch_a], cherry-pick [commit_b] into [branch_b], and finally submit 2 separate pull-requests.