Sub topic 4
Squash and merge
This chapter covers the second of GitHub's three merge methods. It assumes you can commit, branch, push, and pull, that you have opened and reviewed a pull request, and that you can read git log output. It builds on the earlier chapters on the commit graph and on what merging a pull request does.
What squash and merge does
Squash and merge takes every change on the pull request branch and adds it to the base branch as one new commit. That commit has one parent: the previous tip of the base branch. The branch's own commits are not added to the base branch.
Compare this with a merge commit. A merge commit has two parents, so the branch's commits become reachable from the base branch. A squash commit has one parent, so Git sees no link between the base branch and the feature branch.
The commit S has the same file contents that a merge commit would have produced. Only the recorded history differs.
How GitHub builds the squash commit
GitHub computes the squash commit in three steps:
- It finds the merge base of the pull request branch and the base branch.
- It takes the combined difference between the merge base and the branch tip.
- It applies that difference on top of the current base branch tip as a single commit.
Because the result is one commit, intermediate states like "fix typo", "WIP", or "undo previous change" disappear from the base branch. If the branch added a file in F1 and deleted it in F3, the squash commit shows nothing about that file.
The commit message is prefilled in the merge dialog, and you can edit it before you confirm. The default subject is usually the pull request title with the pull request number appended, for example Add retry to upload client (#123). The (#123) suffix lets you find the pull request from git log alone. The default body, the authorship rules, and the repository setting that controls them are covered in a later chapter.
Doing it from the web
On the pull request page, open the merge button's drop-down and choose Squash and merge. Edit the subject and body, then confirm. The drop-down only lists methods the repository allows. If squash is missing, an admin has disabled it in the repository settings.
GitHub remembers your last choice for that repository, so check the button label before you click. Merging with the wrong method is a common mistake, because the button looks the same for all three.
Doing it from the command line
The gh CLI can merge a pull request with the squash method:
gh pr merge 123 --squash --delete-branch--delete-branch removes the head branch after the merge. Without --squash, --merge, or --rebase, gh asks which method to use when run in a terminal. In a script, with no terminal, it fails, so name the method explicitly.
You can reproduce the same effect locally with git merge --squash:
git switch main
git merge --squash feature
git commit -m "Add feature"The local command differs from the GitHub button in one way that matters. git merge --squash stages the combined changes but does not create a commit and does not record a merge. You must run git commit yourself. GitHub does the equivalent work on the server and then updates the base branch.
Inspecting the result
Use the graph view to see what a squash merge left behind:
git log --oneline --graph --decorate mainAfter a squash merge, the output is a straight line:
* 4e1b7a2 (HEAD -> main, origin/main) Add retry to upload client (#123)
* c09d5f8 Fix timeout in config loader (#121)
* 91a3ce4 Bump dependenciesThere is no fork and no join, and none of the branch's own commits appear. A merge commit would have shown a join. Once you can read the shape, you can tell how any past pull request was merged.
What gets lost, and what does not
Squashing trades detail for readability. Know what each side of the trade is before you adopt it.
What you lose on the base branch:
- Commit granularity.
git bisectcan only land on the whole squash commit, not on the small commit that introduced a bug. - Per-commit messages. The explanation in each commit message survives only if you copy it into the squash message.
- Per-commit authorship. Only the squash commit's author and any co-author trailers are recorded.
git blamepoints to the squash commit, not to each person's original commit. - Signatures. Signatures on the original commits do not carry over. The squash commit is a new commit created by GitHub.
What you keep:
- The full commit list stays on the pull request page, so reviewers can still read the original sequence there.
- The original commits stay on the head branch until you delete it. If GitHub deletes the branch, the pull request page still shows the commits.
Edge cases
The branch looks unmerged to Git
After a squash merge, Git does not consider the feature branch merged, because the branch tip is not an ancestor of main. Two consequences follow:
git branch --merged maindoes not list the branch.git branch -d featurerefuses to delete it and says the branch is not fully merged. You have to usegit branch -D feature.
Using -D is safe once you have confirmed that the pull request is merged on GitHub. Before you delete a branch this way, check the pull request status first.
Branches that build on the squashed branch
If another branch was created from the feature branch, its history still contains the original commits. After the squash, those commits are not on main, so a plain rebase or merge replays them and conflicts. The fix is to move only the new commits onto the updated base:
git fetch origin
git rebase --onto origin/main feature-a feature-bThis is covered in a later chapter on follow-up branches. The point here is that squash merging changes what "already merged" means in Git's eyes.
Reverting
Because a squash commit has one parent, you can revert it like any ordinary commit:
git revert <squash-commit-sha>You do not need the -m 1 option that a merge commit requires. This is one of the practical advantages of squash: undoing a whole pull request is one revert of one commit.
Large pull requests
A squash commit has the same size as the entire pull request. If a pull request mixes a refactor, a feature, and a configuration change, they land as one inseparable unit. You can revert it only as a whole, and git bisect cannot split it. Keep pull requests focused on one change if you plan to squash them.
Squash and merge at a glance
| Property | Squash and merge |
|---|---|
| Commits added to base | One |
| Parents of the new commit | One (the previous base tip) |
| Branch commits reachable from base | No |
| `git branch -d` works afterward | No, use `-D` |
| Revert command | `git revert <sha>` |
| Per-commit history on base | Lost |
| History shape | Linear |
Because squash produces a linear history, it satisfies a branch protection rule that requires linear history. A later chapter covers that rule and the repository settings that control which methods are available.
When squash is a good fit
Squash and merge works well when:
- Branches contain many small, noisy commits.
- Each pull request represents one logical change, so one commit per pull request matches how the team thinks.
- You want
git logon the base branch to read as a list of pull requests. - You want a simple revert for each pull request.
It works poorly when a branch contains several independent, well-written commits that should stay separately bisectable and separately attributed. In that case, rebase and merge or a merge commit preserves more of what the authors wrote.
Exercise
A teammate squash-merged pull request 200. You run git branch -d feature-x and Git refuses with "not fully merged". Why does this happen, and what is the safe next step?
Show answer
Squash and merge created a new commit on main with one parent, so the tip of feature-x is not an ancestor of main. Git cannot tell the branch is merged. Confirm on GitHub that pull request 200 is merged, then delete the branch with git branch -D feature-x.
Exercise
You run git merge --squash feature on main and then git log. No new commit appears. What is missing, and how does this differ from what GitHub's Squash and merge button does?
Show answer
git merge --squash only stages the combined changes. You must run git commit to create the commit. GitHub's button creates the commit on the server in one step, and it also adds the pull request number to the default subject.
Exercise
A pull request was squash-merged as commit 4e1b7a2 and later turned out to be a mistake. Write the command that reverts it, and say why it needs no -m option.
Show answer
git revert 4e1b7a2. A squash commit has a single parent, so Git has only one possible mainline. The -m option is required only for merge commits, which have two parents.