Sub topic 14
Choosing a merge method for your team
This chapter assumes you know the three merge methods from earlier chapters: what each one adds to the base branch, how to enable them in repository settings, and how to read git log --graph. It covers how to pick one for a team and how to defend the choice.
There is no best method
Each method trades something away:
- A merge commit keeps the most information and makes the history noisy.
- A squash merge gives the cleanest history and discards the branch's internal steps.
- A rebase merge keeps individual commits on a straight line and rewrites every SHA.
Pick the method whose cost your team can tolerate. The sections below compare the methods on the questions that matter in practice.
Compare the methods on what you will do later
Look at what the history lets you do after the merge.
| Question | Merge commit | Squash and merge | Rebase and merge |
|---|---|---|---|
| Commits added to base | Branch commits plus one merge commit | One new commit | One new commit per branch commit |
| Original SHAs kept | Yes | No (branch commits are not in base) | No (commits are replayed with new SHAs) |
| History shape | Branches and joins | Straight line | Straight line |
| One PR is one unit | Yes, the merge commit | Yes, the single commit | No, you must find the range |
| Revert the PR | `git revert -m 1 <merge-sha>` | `git revert <squash-sha>` | Revert each commit, newest first |
| Works with conflicts on the web | Only if there are none | Only if there are none | Only if there are none |
| Follow-up branches | Safe | Need `rebase --onto` | Need `rebase --onto` |
| Allowed under "Require linear history" | No | Yes | Yes |
On the conflicts row, GitHub's merge button is disabled while a pull request has conflicts, whatever the method. Rebase and merge is stricter in practice, because every replayed commit must apply cleanly. A branch whose combined diff merges without conflict can still fail to rebase, since an early commit may conflict with something that a later commit would have resolved.
Criteria that decide the choice
What do reviewers look at?
If reviewers read a PR as one diff, the individual commits are scaffolding. Messages like "fix typo", "address review" and "try again" add nothing to the base branch, so squash fits.
If reviewers go commit by commit and authors write careful commits, each with a purpose and a message, squashing throws away work the team values. Use a merge commit or a rebase merge.
How granular must revert and bisect be?
Squash makes each PR one commit. git revert and git bisect then land on a whole change, which is easy to reason about. Bisect cannot point inside the PR, so a large PR leaves you with a large diff to inspect.
Rebase merge makes bisect precise, because each commit is a step. Reverting a whole PR is harder, because the PR no longer exists as an object in the history. This works only if each commit builds and passes tests on its own. Many teams don't enforce that, and bisect then lands on broken intermediate commits.
A merge commit gives both. git bisect can follow the original commits, and git revert -m 1 removes the PR in one step.
Do you need to find the pull request from a commit?
Squash and merge adds the PR number to the commit title, for example Add feature (#123), and the PR page links to the single commit. With rebase merge, neither the commits nor any message in the history identifies the PR. With a merge commit, the message Merge pull request #123 from ... does.
Who reads the history?
Run this on your main branch and ask whether the output is useful to a new team member:
git log --oneline --graph --decorate mainA line of descriptive one-commit-per-PR entries is easy to scan. A graph with many parallel lines is accurate but hard to read. A long straight run of commits with mixed quality is easy to scan but hard to learn from.
Match the method to the workflow
These are common pairings, not rules.
- Small PRs, trunk-based development, frequent deploys. Use squash and merge. PRs are small, so losing internal commits costs little, and each deploy maps to one commit.
- Long-lived feature branches, release branches, or teams that curate commits. Use a merge commit. It preserves the full record and keeps reverting a whole feature simple.
- Teams that write clean, atomic commits and want a straight history. Use rebase and merge, and make sure commits build individually.
- Mixed contributors, such as open source. Allow squash and a merge commit, and let maintainers choose per PR. Contributors' commit habits vary too much to rely on rebase.
You are not locked into one method. A repository can allow several, and the merge button remembers the last one used. You can also pass the method on the command line:
gh pr merge 123 --merge
gh pr merge 123 --squash --delete-branch
gh pr merge 123 --rebaseThe second command also deletes the head branch after merging.
Enforce the decision
A team agreement that lives only in a document gets broken. Encode it in two places.
- Repository settings. Under the pull request settings, enable only the methods you accept. If you allow only squash, nobody can create a merge commit by accident. You can also set the default squash message, for example PR title plus description, so history entries are consistent.
- Branch protection. "Require linear history" blocks merge commits on the protected branch. It forces squash or rebase, and it also blocks a plain
git pushof a merge commit to that branch.
If you choose squash only, disable the other two in settings. Linear history is then automatic, and the rule adds a safety net for direct pushes.
Costs you accept with each choice
Squash
- The author's individual commits, and their messages, are gone from the base branch. The branch keeps them only until it is deleted.
- Follow-up branches built on a squashed branch conflict, because Git sees the original commits as unmerged. The fix is to move only the new commits onto the updated base:
git fetch origin
git rebase --onto origin/main feature-a feature-bHere feature-a is the old branch that was squash-merged, and feature-b is the branch that grew from it. Git replays the commits that are in feature-b but not in feature-a.
- You can reproduce the result locally with:
git switch main
git merge --squash feature
git commit -m "Add feature"Rebase
- Every commit gets a new SHA, so references in issues, chat or release notes to the branch's old SHAs no longer point at anything on main.
- Original signatures are not preserved, because the commits are new objects.
- Follow-up branches have the same problem as with squash, and the same
rebase --ontofix applies.
Merge commit
- The history shows every branch and join. Tools that list commits linearly, such as
git logwithout--first-parent, mix branch commits with merges. Use--first-parentto see only the base branch's view:
git log --oneline --first-parent main- Reverting the merge works, but it has a trap. After
git revert -m 1 <merge-sha>, Git treats the branch's changes as already merged and reversed. If you later merge the same branch again with more commits, only the new commits come in. To bring the original changes back, revert the revert first.
A decision path
Common mistakes
- Allowing all three methods without a rule for when to use each. The history becomes a mix of shapes, and reverting needs a different procedure for each PR.
- Choosing rebase merge but not checking that commits build. Bisect will then stop on broken commits that never existed as merged states in a real PR.
- Choosing squash and keeping stacked PRs. Plan for
rebase --onto, or merge the stack from the bottom and retarget each next PR tomainbefore merging it. - Enabling linear history while still allowing merge commits in the dropdown. Contributors see the option, try it, and the merge is rejected.
- Reverting a merge commit without
-m 1. Git refuses, because it cannot know which parent to keep. Parent 1 is the base branch.
Exercise
Your team opens many small PRs and deploys from main several times a day. Authors often push commits like "fix lint" and "address review". A manager wants every deploy to map to one commit that is easy to revert. Which method do you recommend, which settings do you change, and what must authors do when they build a second PR on top of a first one that was merged?
Show answer
Recommend squash and merge. In repository settings, enable only squash merging and disable merge commits and rebase merging, so the history stays uniform. Optionally add "Require linear history" as a safety net. Each PR becomes one commit, so a deploy maps to one commit and git revert <sha> undoes it. For the second PR, authors run git fetch origin and git rebase --onto origin/main feature-a feature-b, where feature-a is the squashed branch, so only the new commits are replayed and the squashed commits do not cause duplicate conflicts.
Exercise
A PR was merged with "Create a merge commit". The change broke production. What command removes it from main, and what happens if you later merge the same branch again after adding more commits?
Show answer
Run git revert -m 1 <merge-commit-sha>. The -m 1 keeps the first parent, the base branch, as the mainline. If you later merge the same branch again, Git considers its old commits already merged, so only the new commits come in and the original changes stay reverted. To restore them, first revert the revert commit, then merge.