Sub topic 6
Combining commits with squash and fixup
This chapter assumes you can use git add, git commit, git log and git status. It also assumes you know how to start git rebase -i and edit its todo list. Both commands below combine a commit with the one above it in the list. They differ only in what happens to the commit message.
What squash and fixup do
When you mark a commit squash or fixup, Git applies its changes on top of the previous commit in the list and merges the two into one new commit. Because the todo list is read from oldest to newest, the marked commit melts into the commit above it.
squash(ors) keeps both messages. Git opens your editor so you can write one combined message.fixup(orf) discards the marked commit's message and keeps the message of the commit above it. No editor opens for the message.
Use squash when the extra commit adds something worth saying. Use fixup for typo fixes, forgotten files and other small corrections.
| Command | Changes merged | Message result | Editor opens |
|---|---|---|---|
| `squash` | Yes | Both messages, which you edit | Yes |
| `fixup` | Yes | Message of the earlier commit only | No |
A worked example
Start by listing recent commits, then open the todo list for the last four:
git log --oneline -n 5
git rebase -i HEAD~4Suppose you want to fold a typo fix into the login form commit, reword the validation commit, drop a debug commit and keep the tests. The todo list looks like this after editing:
pick 3f2a1b9 Add login form
fixup 8c4d7e2 Fix typo in login form
reword 51ab9c0 Add validation
drop 7d90e1f Debug logging
pick a6b2c33 Add login testsRead it from top to bottom:
3f2a1b9is kept as the base commit.8c4d7e2is afixup, so its changes join3f2a1b9and its message disappears.51ab9c0is reworded, so Git stops to let you change its message.7d90e1fis dropped.a6b2c33is kept as it is.
After you save and close the editor, the five commits become three. The history now reads as a clean series: the login form, validation, and tests.
Choosing the order
A squashed commit only merges with the line directly above it. If the commits you want to combine are not next to each other, move the line first. For example, you can move Fix typo in login form so that it sits right under Add login form, and then change its command. Reordering is covered in the next chapter.
Several commits can be combined into one by marking each of them:
pick 3f2a1b9 Add login form
squash 8c4d7e2 Fix typo in login form
squash 2b7c8d1 Adjust form spacingGit then opens the editor once with all three messages listed. Delete the lines you do not want and write a single clear message.
Common mistakes
- Marking the wrong line. The first commit in the list should usually stay
pick. Marking the one you wanted to keep asfixuploses the message you meant to keep. - Squashing across unrelated work. A commit that mixes two ideas is harder to review and revert. Combine only commits that belong to the same change.
- Leftover messages. After
squash, the editor shows every message with comment lines. Lines starting with#are ignored, but unwanted text without#stays in the final message. - Rewriting shared history. Squashing replaces commits with new ones. Do this only on commits you have not shared, or coordinate before pushing.
Exercise
You have the todo list "pick 11aa Add search", "pick 22bb Add search tests", "pick 33cc Fix search typo". You want one commit for the search feature that keeps the message "Add search" and a separate commit for the tests. What do you change?
Show answer
Move the typo fix directly under "Add search" and mark it fixup: "pick 11aa Add search", "fixup 33cc Fix search typo", "pick 22bb Add search tests". The fixup merges into the commit above it and drops its own message.
Exercise
What is the difference between squash and fixup when the rebase reaches the marked commit?
Show answer
Squash combines the changes and opens the editor with both messages so you can write a combined one. Fixup combines the changes and keeps only the earlier commit's message, with no editor.