Sub topic 2
What happens when you merge a pull request
This chapter assumes you can commit, branch, push, and pull in Git, open and review a pull request on GitHub, and read git log output.
Merging updates a branch on the server
A pull request asks GitHub to move the work on a head branch (for example feature) into a base branch (for example main). When you click merge, GitHub changes the base branch on the server. Your local clone does not change until you run git fetch or git pull.
GitHub offers three merge methods. They differ in two things: which commits they add to the base branch, and whether those commits keep their original identity (their SHA, the hash that names a commit).
The three outcomes
Suppose main has commits A and B, and feature branched from B and added C and D.
| Method | What `main` gains | Original SHAs on `main` | Shape of history |
|---|---|---|---|
| Create a merge commit | C, D, and a merge commit M with two parents | Kept | Branches and joins |
| Squash and merge | One new commit S holding the combined changes of C and D | Not present | Linear |
| Rebase and merge | New copies of C and D, with no merge commit | Replaced by new SHAs | Linear |
Later chapters cover each method in detail. For now, remember that only the merge commit method keeps the original commits and records that a branch existed.
What stays the same
In the simple case, where nothing else landed on main while the pull request was open, all three methods leave main with the same file contents. Only the history differs.
Every method also does these things:
- It marks the pull request as merged.
- It requires the same checks and approvals, because branch protection rules apply to all of them.
- It leaves the head branch in place unless you delete it. The repository can be set to delete it automatically.
See the result locally
After merging on GitHub, update your local main and look at its history:
git switch main
git pull
git log --oneline --graph --decorate mainA merge commit shows a fork and a join. The hashes below are examples:
* 9f3c2ab (HEAD -> main, origin/main) Merge pull request #123 from team/feature
|\
| * 4d1e7c0 Add validation
| * b82a9f1 Add form
|/
* 1a2b3c4 Previous workA squash or rebase merge shows a straight line with no join. A squash line has one commit for the whole pull request. A rebase line has one commit per original commit, with different hashes.
Common errors
- The merged branch still looks unmerged. After a squash or rebase merge,
git branch --merged mainmay not list your branch. The original commits are not ancestors ofmain, becausemainholds a new commit or copies instead. GitHub has still merged the pull request. - Your local
mainhas no new commit. You merged on the server and never pulled. - A method is missing from the merge button. The repository settings can disable any of the three methods.
Exercise
A pull request adds two commits, C and D, to a branch that started from main. After merging, git log --oneline --graph main shows a single straight line with one new commit that contains both changes. Which merge method was used, and why can you rule out the other two?
Show answer
Squash and merge. A merge commit would show a fork and join with C, D, and a two-parent merge commit. A rebase merge would also be straight, but it would add two commits (copies of C and D), not one.