Sub topic 11
Merging from the command line with gh
This chapter assumes you can commit, branch, and push, that you have opened and reviewed a pull request on GitHub, and that you have read about the three merge methods in earlier chapters. It also assumes the GitHub CLI (gh) is installed and signed in with gh auth login.
Why merge from the terminal
The gh pr merge command runs the same merge that the Merge pull request button runs. GitHub performs the merge on its servers, so your local checkout is not part of the operation. You choose the method with a flag, and the command is easy to script or repeat.
The result on the base branch is identical to a click in the web UI. The method you choose still has to be allowed by the repository settings.
The three methods as flags
gh pr merge 123 --merge
gh pr merge 123 --squash --delete-branch
gh pr merge 123 --rebase| Flag | Method | Effect on the base branch |
|---|---|---|
| `--merge` | Create a merge commit | Adds the branch commits plus one merge commit |
| `--squash` | Squash and merge | Adds one new commit with the whole diff |
| `--rebase` | Rebase and merge | Adds copies of the branch commits, with no merge commit |
--delete-branch (short form -d) deletes the head branch after the merge. It works with any of the three flags.
If you omit the number, gh uses the pull request for your current branch. If you omit the method flag in a terminal, gh asks you to pick one. In a script with no terminal, you must give a method flag.
Setting the commit message
For merge and squash, you can set the message from the command line:
gh pr merge 123 --squash --subject "Add feature (#123)" --body "Adds the export option."--subject (-t) sets the commit title and --body (-b) sets the commit body. Rebase merges keep the original commit messages, so these flags do not apply to them.
Waiting for checks
Two options control timing:
--autoturns on auto-merge. GitHub merges the pull request later, when required checks pass and required reviews are in place. Auto-merge must be enabled in the repository settings.--adminmerges even when branch protection requirements are not met. Use it only if you have admin rights and a real reason, because it bypasses the safeguards the team set up.
gh pr merge 123 --squash --autoHow settings affect the command
gh cannot override the allowed methods. If the repository disables squash merging, this fails:
gh pr merge 123 --squashThe error says the method is not allowed, and you need to pick one of the enabled methods. The same applies to the Require linear history rule, which blocks --merge.
Check the base branch afterward to confirm what was created:
git fetch origin
git log --oneline --graph --decorate origin/mainA merge commit shows a fork and join. A squash or rebase shows a straight line.
Local checkout behavior
When you run gh pr merge from a branch that belongs to the pull request, gh can switch you back to the default branch and delete the local branch after a successful merge. When you pass --delete-branch, it handles both the remote and local branch. Run git pull afterward to bring the new commits into your local base branch.
Exercise
You run gh pr merge 77 --rebase and get an error that says rebase merging is not allowed. What do you check, and what are your options?
Show answer
Check the repository's merge settings under Settings > General > Pull Requests. The maintainers disabled rebase merging, so either run the command with an enabled method such as --squash or --merge, or ask an admin to enable rebase merging.