Sub topic 9
Choosing which merge methods a repository allows
This chapter assumes you can commit, branch, push and pull, open and review a pull request, and read git log output. It also assumes you know what a merge commit, a squash merge and a rebase merge each add to the base branch, which earlier chapters covered. Here you decide which of those methods a repository offers, and what happens in the web UI and the gh CLI as a result.
Where the setting lives
Merge methods are a repository setting. A person with admin access opens Settings, then General, and scrolls to the Pull Requests section. It has three checkboxes:
- Allow merge commits
- Allow squash merging
- Allow rebase merging
At least one must stay enabled. The setting applies to every pull request in the repository. GitHub has no per-pull-request switch, so you restrict the choices in settings and the person merging picks from what remains.
What the merge button does with your choices
The green merge button on a pull request follows the enabled methods:
- With one method enabled, the button performs that method and shows no choice.
- With several enabled, the button has a dropdown. GitHub remembers the last method you picked and uses it as the default next time.
- A disabled method does not appear in the dropdown.
If your team wants everyone to squash, enable only squash merging. The dropdown disappears and nobody can pick a different method by accident.
Setting defaults for squash commits
When squash merging is enabled, the settings page also lets you choose the default commit message that GitHub proposes. Options include the pull request title and description, the pull request title and commit details, or the pull request title only. The person merging can still edit the message before confirming. This is a default, not a rule.
Checking and changing settings with the CLI
You can read the same settings through the GitHub API. The gh CLI wraps it, so you can inspect or script the configuration:
gh api repos/OWNER/REPO \
--jq '{merge: .allow_merge_commit, squash: .allow_squash_merge, rebase: .allow_rebase_merge}'To allow only squash merging and delete head branches after merge, send a patch:
gh api -X PATCH repos/OWNER/REPO \
-F allow_merge_commit=false \
-F allow_squash_merge=true \
-F allow_rebase_merge=false \
-F delete_branch_on_merge=trueReplace OWNER/REPO with your repository. You need admin permission, and the call fails if you disable all three methods.
Common errors
- A method is missing from the dropdown. It is disabled in settings. Ask a repository admin, or check whether an organization policy controls it.
gh pr merge 123 --rebasefails. The repository does not allow rebase merging. Use one of the enabled methods.- The dropdown shows a method you did not expect. Another admin changed settings. Re-read them with the
gh apicommand above. - Only some methods are offered because of branch protection. A rule requiring linear history removes merge commits as an option. The next chapters cover this rule.
Choosing a starting point
| Team preference | Methods to enable |
|---|---|
| One commit per pull request on the base branch | Squash only |
| Keep every commit and the branch shape | Merge commits only |
| Keep every commit, no merge commits | Rebase only |
| Different pull requests need different shapes | Enable two or three, and document when to use each |
Enabling fewer methods makes history more predictable. Enabling more gives authors flexibility but means the base branch can mix shapes.
Exercise
Your team wants every pull request to land on main as exactly one commit, and nobody should be able to choose another method. Which settings do you choose, and what will the merge button look like?
Show answer
Enable only "Allow squash merging" and disable merge commits and rebase merging. The merge button then performs a squash merge and shows no dropdown, because only one method is available.