Sub topic 14
Choosing a merge method for your team
This chapter assumes you can read git log --graph output and know how the three GitHub merge methods work: merge commit, squash and merge, and rebase and merge. It also assumes you know where repository settings and branch protection live. Here you turn those mechanics into a decision.
Start from what you need the history to do
No method is best in general. Each one trades something for something else, so first decide what your team uses history for.
| Method | What lands on the base branch | Strength | Cost |
|---|---|---|---|
| Merge commit | All branch commits plus one merge commit | Keeps the full record and the branch shape. Easy to revert as a unit. | Non-linear graph. Noisy if branches have "fix typo" commits. |
| Squash and merge | One new commit for the whole pull request | One pull request is one commit. Messy branch history disappears. | Original commits are lost. Follow-up branches can conflict. |
| Rebase and merge | Each branch commit, replayed with new SHAs | Linear history that keeps individual commits. | New SHAs. No merge commit to mark the pull request. Needs a clean commit series. |
Decision criteria
Work through these questions in order.
- Are the branch commits worth keeping? If contributors write small, well-described commits, use merge commit or rebase and merge. If branches contain "wip" and "address review" commits, squash.
- Do you need a linear history? Tools such as
git bisectand simple changelogs are easier on a straight line. Squash and rebase both produce one. A merge commit does not, so a "Require linear history" rule blocks it. - How will you revert? A squashed pull request is one commit, so
git revert <sha>undoes it. A merge commit needsgit revert -m 1 <merge-commit-sha>. A rebased pull request needs one revert per commit, or a revert of the range. - Do people stack branches? If branches build on other unmerged branches, squash and rebase both rewrite the parent commits. Merge commits keep the original SHAs, so stacked branches stay valid.
- Do you need to trace a change to its pull request? Squash titles carry the pull request number by default. Merge commits name it too. Rebased commits do not, unless you add it yourself.
Common team setups
- Small team, short branches, one pull request per change. Allow squash only. The base branch reads like a list of changes, and each one reverts cleanly.
- Teams that value detailed history. Allow merge commit only, or merge commit plus squash. Long-lived branches and stacked work stay safe.
- Strict linear history with disciplined commits. Allow rebase and merge, usually with squash as a second option, and turn on "Require linear history".
You can allow more than one method. Pick a default, document it, and use the other methods as exceptions. For example, squash by default, but merge commit for a release branch whose commits must stay intact.
Enforce the choice
A convention that nobody enforces drifts. In the repository settings, untick the methods you do not want. Add the linear history rule if you chose squash or rebase. You can check the result later with:
git log --oneline --graph --decorate mainIf you see merge commits where you expect none, a method is still enabled or a rule is missing.
When you merge from the terminal, pass the method explicitly so scripts behave the same for everyone:
gh pr merge 123 --squash --delete-branchCommon mistakes
- Choosing squash, then stacking branches on each other without rebasing the child. Use
git rebase --onto origin/main feature-a feature-bafter the parent merges. - Choosing rebase and merge for branches full of fixup commits. The noise goes straight into
main. - Allowing all three methods with no default. The history then mixes styles, and nobody can say how to revert a given pull request without looking.
Exercise
A team squash-merges every pull request. A developer has feature-b branched from feature-a, and feature-a was just squash-merged. What command moves feature-b onto the updated main without duplicate conflicts, and why is it needed?
Show answer
Run git fetch origin and then git rebase --onto origin/main feature-a feature-b. The squash created a new commit on main that has the same changes as the original feature-a commits but different SHAs. A plain rebase would try to replay the old feature-a commits again and conflict with their squashed copy. --onto replays only the commits after feature-a.
Exercise
Your team wants linear history, but contributors often push "wip" and "fix review comments" commits. Which method do you allow, and which branch protection rule do you add?
Show answer
Allow squash and merge, and add "Require linear history". Squashing turns each pull request into one clean commit, and the rule blocks merge commits.