Sub topic 13
Choosing a merge strategy
This chapter assumes you can create branches and commits, open a pull request, and read its checks and review status. A merge strategy decides how GitHub puts the pull request's commits into the base branch. The choice shapes your history, your ability to revert, and how git log reads months later.
The three options
GitHub offers three buttons on a pull request. A repository admin can turn each one on or off in the repository settings.
| Strategy | Resulting history | Commit hashes |
|---|---|---|
| Create a merge commit | All branch commits plus one merge commit | Preserved |
| Squash and merge | One new commit on the base branch | New hash, branch commits discarded from base |
| Rebase and merge | Each branch commit replayed on top of base | New hashes, no merge commit |
Merge commit
The merge commit keeps every commit exactly as you made it and adds a commit with two parents. History is accurate but can be noisy, with many "fix typo" and "wip" commits. Use it when the individual commits are meaningful and you want a record of when the branch landed.
Squash and merge
GitHub combines all the branch's changes into one commit on the base branch. The default message is the pull request title and the commit messages. This keeps main linear and gives one commit per pull request, so reverting a whole change is a single git revert. The cost is that detail is lost. A pull request that mixes a refactor and a feature becomes one large commit, and git blame points to the whole squashed change.
Rebase and merge
GitHub replays each commit onto the tip of the base branch without a merge commit. History stays linear and keeps the individual steps. The commits get new hashes, so the hashes you saw on the branch will not exist on main. It works well when authors write clean, self-contained commits.
Choosing for your team
Ask these questions in order:
- Are the branch commits tidy and each one buildable? If yes, rebase or merge commit keeps that value. If no, squash.
- Do you want a linear history? Squash and rebase give it. A merge commit does not.
- Do you need to revert whole features easily? Squash makes that simplest.
- Do long-lived branches share work? Squashing a branch that others branched from causes confusing conflicts, because their history still contains the original commits.
Most teams pick squash as the default and allow one other option for exceptions.
Running it from the command line
gh pr merge 123 --squash --delete-branch
gh pr merge 123 --rebase
gh pr merge 123 --mergeThe --delete-branch flag removes the head branch after merging. Branch protection can restrict which strategies are allowed, and a required linear history rule blocks merge commits.
Common errors
- Squashing with the default message, which can include dozens of noisy lines. Edit the message before confirming.
- Allowing all three strategies, so history becomes inconsistent.
- Using rebase and merge on commits that do not build individually, which breaks
git bisect.
Exercise
Your team wants one commit per pull request, a linear history on main, and the ability to revert a whole feature with one command. Which strategy fits, and what is one drawback?
Show answer
Squash and merge. It produces one commit per pull request on a linear history, so one git revert undoes the feature. The drawback is that the branch's individual commits are lost, so a large or mixed change becomes one big commit and git blame cannot show the smaller steps.