Sub topic 9
Cleaning up a branch with git rebase -i
This chapter assumes you can read git log, know how a plain git rebase origin/main replays commits, and have a feature branch that is not yet shared with others. Interactive rebase lets you edit the list of commits before Git replays them.
What interactive rebase does
git rebase -i opens a text file called the todo list. It has one line per commit, oldest first. Each line starts with a command. Git runs the lines from top to bottom, replaying each commit onto the new base. Because every replayed commit gets a new hash, this rewrites history, so use it on commits that only you have.
Start it with a base. Commits after that base appear in the list:
git switch feature
git rebase -i origin/mainThe todo list
Here is a todo list after some cleanup edits:
pick a1b2c3d Add login form
fixup d4e5f6a Fix typo in login form
reword 1234567 Add session handling
squash 89abcde Add session tests
drop fedcba9 Debug loggingSave and close the editor to start the rebase. Each command does one thing:
| Command | Effect |
|---|---|
| `pick` | Keep the commit as it is. |
| `reword` | Keep the changes and stop to let you edit the message. |
| `edit` | Stop after applying the commit so you can amend it or split it. |
| `squash` | Merge into the previous commit and combine both messages. |
| `fixup` | Merge into the previous commit and discard this message. |
| `drop` | Remove the commit. |
To reorder commits, move lines up or down. If a moved commit depends on changes from another commit, you may get a conflict. Resolve it as in a normal rebase with git add and git rebase --continue.
Fixups without editing the list
When you notice a mistake in an older commit, make a fixup commit that points at it. Then let Git arrange the list for you:
git commit --fixup a1b2c3d
git rebase -i --autosquash origin/main--autosquash moves the fixup commit directly below its target and sets its command to fixup. You only need to save the list.
Common errors
- Squashing the first line.
squashandfixupneed a commit above them. The first line must bepick,reword, oredit. - Deleting a line. Removing a line from the todo list drops that commit without warning. Use
dropwhen you mean it, so the intent is visible. - Emptying the file. If you save an empty todo list, Git aborts the rebase.
- Rewriting pushed commits. If you already pushed the branch, the remote has the old hashes. Check that nobody else uses the branch before you rewrite it.
- Losing track mid-rebase. Run
git statusto see which step you are on.git rebase --abortreturns the branch to its state before you started.
Exercise
Your branch has the commits "Add parser" and "Fix parser typo". You want one commit named "Add parser". Which todo list commands do you use, and in what order?
Show answer
Put "Add parser" first with pick, then put "Fix parser typo" directly below it with fixup. The fixup commit merges into "Add parser" and its message is discarded.
Exercise
What does git commit --fixup a1b2c3d followed by git rebase -i --autosquash origin/main save you from doing by hand?
Show answer
Moving the fixup commit below its target commit and changing its command from pick to fixup in the todo list.