Git and GitHub · Part 3 · GitHub and remote repositories
Merge strategies on GitHub
One button, three histories
The arrow next to Merge pull request offers three ways to merge. All three put the same code into main. What differs is the history left behind, and that matters months later, when someone reads git log to find out when a bug came in.
All three examples start from the same pull request: #7, a branch with three commits, while main gained a footer meanwhile.
cbc2c4d Add cart page
24fba06 Fix typo
59228d3 Address review commentsCreate a merge commit
Keeps every commit and adds a merge commit, like git merge --no-ff. GitHub always makes the merge commit, even when a fast-forward was possible.
* 792b041 Merge pull request #7 from jknithin36/feature/cart
|\
| * 59228d3 Address review comments
| * 24fba06 Fix typo
| * cbc2c4d Add cart page
* | 504f383 Add footer
|/
* 4320bd1 Add homepageThe full story is kept, and the commits keep their hashes. But “Fix typo” and “Address review comments” land in main forever, and the graph gets busy on a big team.
Squash and merge
Combines every commit into one new commit on main, like git merge --squash. The message defaults to the pull request title and number:
* 1bea7c4 Add cart page (#7)
* 504f383 Add footer
* 4320bd1 Add homepageOne pull request, one commit: main reads like a list of features, and undoing a feature is one git revert. The branch's small commits are gone from main (they stay visible in the pull request).
Rebase and merge
Replays each commit on top of main, with no merge commit:
* b36c6fe Address review comments
* 6900644 Fix typo
* 581e5c4 Add cart page
* 504f383 Add footer
* 4320bd1 Add homepageA straight line with every commit. The commits get new hashes, as with any rebase (Lesson 13), and the history is only as good as the commits on the branch, so this suits teams that clean them up with interactive rebase first (Lesson 14).
Side by side
| Merge commit | Squash | Rebase | |
|---|---|---|---|
| Commits added to main | All, plus a merge commit | One | All |
| History shape | Branches and joins | Straight | Straight |
| Keeps original hashes | Yes | No | No |
| Revert a whole feature | Revert the merge commit | One revert | One revert per commit |
| Common in | Long-lived and release branches | Most product teams | Teams with tidy commits |
Afterwards, locally
After a squash or rebase merge, the local branch's commits aren't in main (different hashes), so git branch -d says not fully merged. Once GitHub shows the pull request as merged, that's expected:
git switch main
git pull
git branch -D feature/cartQuick reference
| GitHub button | Local equivalent | gh |
|---|---|---|
| Create a merge commit | git merge --no-ff | gh pr merge --merge |
| Squash and merge | git merge --squash | gh pr merge --squash |
| Rebase and merge | git rebase main | gh pr merge --rebase |