Sub topic 10
Splitting one commit into several
A large commit is hard to review, hard to revert, and hard to bisect. Interactive rebase lets you cut it into smaller commits that each do one thing. This chapter assumes you can start git rebase -i, read its todo list, and use edit to stop on a commit. Those topics are covered in earlier chapters.
How a split works
Git has no "split" command. You use edit to stop the rebase on the commit you want to divide. You then undo that commit while keeping its changes in your working tree. Finally you commit the changes again in several pieces.
The undo step is a mixed reset. git reset HEAD^ moves the branch back one commit and resets the index to match. It leaves your files alone. After it runs, every change from the old commit appears as an unstaged change.
Worked example
Suppose your branch has a commit "Add login form" that contains both the HTML markup and the validation logic in src/login.js. Find it and start the rebase from its parent:
git log --oneline -n 5
git rebase -i HEAD~4In the todo list, change pick to edit on the commit to split, then save and close the editor. Git applies that commit and stops. Now split it:
# In the todo list, change the commit to split from 'pick' to 'edit'.
# The rebase stops after applying that commit.
git reset HEAD^
git add -p src/login.js
git commit -m "Add login form markup"
git add -A
git commit -m "Add login form validation"
git rebase --continuegit add -p walks through each changed hunk and asks whether to stage it. The answers you will use most are:
| Key | Effect |
|---|---|
| `y` | Stage this hunk |
| `n` | Skip this hunk for now |
| `s` | Split the hunk into smaller hunks |
| `e` | Edit the hunk by hand |
| `q` | Quit, leaving the remaining hunks unstaged |
The first commit takes only the hunks you stage. git add -A then stages everything that is left, and the second commit takes it. After git rebase --continue, Git replays any later commits on top of your new pair.
Checking your work
Before you continue, run git diff --cached to see exactly what the next commit will contain. After the rebase finishes, run git log --oneline -n 5 to confirm the new commits. You can also run git diff ORIG_HEAD HEAD. It should print nothing, because the final tree is the same as before and only the way it is divided has changed.
Common errors
- Using
git add -Atoo early. It stages everything, including files you meant to put in a later commit. Use it only for the final piece. - Splitting into commits that do not build. Each new commit should compile and pass tests on its own. Run the tests before each
git commit, or use--execas described in a later chapter. - Forgetting
git rebase --continue. Until you run it, the rebase is still in progress.git statuswill say so. - Conflicts in later commits. If a later commit touched the lines you divided, the replay may stop with a conflict. Resolve it as you would any rebase conflict. If you get stuck,
git rebase --abortreturns you to the state before the rebase began.
Exercise
You marked a commit as edit and the rebase stopped. You ran git reset HEAD^, but a new file the commit added does not appear in git add -p. Why, and what do you do?
Show answer
After the mixed reset the new file is untracked, and git add -p only offers hunks from tracked files. Stage the file by name with git add path/to/file, or run git add -N path/to/file first so its content shows up in git add -p.
Exercise
After finishing a split, how can you confirm that you changed only how the work is divided and not the final content?
Show answer
Run git diff ORIG_HEAD HEAD. An empty result means the final tree is identical to the one before the rebase.