Sub topic 2
What happens when you merge a pull request
This chapter assumes you can commit, branch, push, and pull with Git. It also assumes you can open and review a pull request on GitHub and read git log output. It explains what GitHub does between your click on the merge button and the moment the base branch has new content. The next three chapters look at each merge method in detail.
The pull request is a pointer, not a copy
A pull request (PR) is a record on GitHub that links two branches:
- The head branch holds the changes you want to merge. It is also called the compare branch.
- The base branch is the branch that should receive them, usually
main.
The PR does not store its own copy of the code. It points at the current tip of the head branch and the current tip of the base branch. When you push more commits to the head branch, the PR updates itself. The PR's list of commits is the set of commits reachable from the head that are not reachable from the base.
Merging a PR means moving the base branch forward so that it contains the work from the head branch. GitHub does this on the server, in the repository's bare copy. It does not touch your local clone. After a merge, your local main is stale until you run git pull.
The test merge before you click
While a PR is open, GitHub keeps checking whether the head can be merged into the base. It builds a trial merge commit and stores it under a special reference:
git fetch origin pull/123/head:pr-123
git fetch origin refs/pull/123/merge
git log --oneline --graph -n 5 FETCH_HEADThe first command downloads the PR's head commit into a local branch named pr-123. The second downloads the trial merge commit that GitHub computed. The third shows the first lines of its history.
Three facts about this trial merge matter:
- It is always computed as a merge commit, whichever method you later choose. CI systems that check out
refs/pull/N/mergetherefore test the combination of head and base, not the head alone. - GitHub recomputes it when either branch moves. If
maingets new commits, the trial merge changes. - If the head and base change the same lines in incompatible ways, the trial merge fails. The PR then shows "This branch has conflicts that must be resolved", and the merge button is disabled until you resolve them.
The trial merge only tells GitHub whether the content combines cleanly. It is not the commit that ends up on the base branch unless you choose the merge-commit method, and even then GitHub creates a fresh commit at merge time.
What must be true before the button works
The merge button is enabled only when several independent checks pass:
- The PR is open and not a draft.
- The trial merge has no conflicts.
- All branch protection requirements are met. These can include required reviews, required status checks, resolved conversations, and a signed-commit requirement.
- The chosen merge method is allowed in the repository settings.
Repository administrators can sometimes bypass protection rules, depending on how the rules are configured. Teams that use a merge queue never merge directly. The queue builds a temporary branch, runs checks on it, and then merges. The three methods below still apply to what lands on the base branch.
The three methods at a glance
When the checks pass, you choose one method. Each one adds different commits to the base branch.
| Method | What is added to the base branch | Original commit SHAs on the base |
|---|---|---|
| Create a merge commit | All head commits plus one new merge commit with two parents | Kept |
| Squash and merge | One new commit holding the whole diff, with one parent | Not present |
| Rebase and merge | Copies of each head commit, replayed on top of the base | Not present (new SHAs) |
A SHA is the identifier Git computes from a commit's content, its parent, its author, and its committer. If any of those change, the SHA changes. This is why rebased commits are "copies" and not the originals.
The common core of all three methods is the same. GitHub takes the content difference between the head and the base and records it as history on the base branch. The methods differ in how many commits that takes and how those commits connect to each other.
The following diagram shows the sequence of events for any method.
What changes after the merge
Once the base branch has moved, several things happen on GitHub's side.
- The PR becomes "Merged". Its state changes from open to merged, and the PR keeps its comments, reviews, and checks permanently. The history of the discussion survives even when the commit history is rewritten by squash or rebase.
- Linked issues may close. Keywords such as
Closes #12orFixes #12in the PR description or commit messages close the issue, but only when the base branch is the repository's default branch. - The head branch stays by default. A repository setting called "Automatically delete head branches" removes it after the merge. Otherwise you get a "Delete branch" button on the merged PR. Deleting the branch does not delete any commits that are reachable from the base. A deleted branch can also be restored from the PR page.
- Your local clone is unchanged. Run
git fetchorgit pullto see the new base tip.
You can see the effect on the base branch with the command that the rest of this tutorial uses to compare methods:
git log --oneline --graph --decorate mainA merge commit shows a diamond shape with two lines joining. A squash or rebase merge shows a straight line. The next chapters explain how to read each pattern.
Merging through the API
The web button, the gh CLI, and third-party tools all call the same REST endpoint. The merge_method parameter takes merge, squash, or rebase:
gh api --method PUT repos/OWNER/REPO/pulls/123/merge \
-f merge_method=squash \
-f sha=HEAD_COMMIT_SHAThe optional sha parameter is a safety check. If the PR's head has moved since you read it, for example because the author pushed another commit, GitHub rejects the request with a conflict error instead of merging a commit you did not review. Automation that merges PRs should send it.
A rejected request returns an HTTP status code that tells you why. A 405 means the PR is not mergeable, which covers conflicts, failed protection rules, or a disallowed method. A 409 means the head SHA did not match.
Merging outside GitHub
You can merge locally and push the result to the base branch. GitHub then detects what happened, but it can only recognize some cases.
- With a regular merge or a fast-forward, the PR's head commit becomes reachable from the base branch. GitHub marks the PR as merged.
- With a local squash or rebase, the original head commit is not reachable from the base, because the commits on the base are new ones. GitHub shows the PR as closed, not merged. The work is on the base branch, but the PR does not record that fact.
This difference is a small example of why the choice of method matters beyond the shape of the graph: history that GitHub can trace is history that tools, audits, and release notes can trace as well.
Common errors
- The merge button is disabled with no conflicts. A required check or review is probably missing. Open the checks section of the PR and look for items marked required.
- The method you want is not offered. The repository allows only some methods. Repository settings control the list, and a later chapter covers them.
- The merged code differs from what you tested on the branch. The base branch changed after your last test. Because the trial merge always combines the head with the current base, checks that run on
refs/pull/N/mergecatch this and checks that run on the head alone do not. - A reviewer's approval no longer counts. Some protection rules dismiss stale approvals when new commits are pushed. Pushing a fixup right before merging can reset the approval.
Exercise
A teammate runs git merge --squash feature locally on main, commits, and pushes. Their PR from feature into main is still listed on GitHub. What state does GitHub show for it, and why?
Show answer
The PR shows as closed, not merged. A squash creates a new commit on main that does not have the original feature commits as ancestors, so GitHub cannot see the PR's head commit in the base branch's history and does not mark the PR as merged.
Exercise
Your CI job checks out refs/pull/123/merge. A colleague says this means CI tests your branch exactly as it will appear after "Squash and merge". Is that correct?
Show answer
Not exactly. The ref contains a trial merge commit that combines the head and the current base, so the file content matches what the squash will produce. The history shape is different, because the trial commit has two parents and a squash commit has one. The tested content is the same only if the base has not moved since GitHub last computed the trial merge.