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.

feature/cart
cbc2c4d Add cart page
24fba06 Fix typo
59228d3 Address review comments

Create 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.

git log --oneline --graph
*   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 homepage

The 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:

git log --oneline --graph
* 1bea7c4 Add cart page (#7)
* 504f383 Add footer
* 4320bd1 Add homepage

One 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:

git log --oneline --graph
* b36c6fe Address review comments
* 6900644 Fix typo
* 581e5c4 Add cart page
* 504f383 Add footer
* 4320bd1 Add homepage

A 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 commitSquashRebase
Commits added to mainAll, plus a merge commitOneAll
History shapeBranches and joinsStraightStraight
Keeps original hashesYesNoNo
Revert a whole featureRevert the merge commitOne revertOne revert per commit
Common inLong-lived and release branchesMost product teamsTeams 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:

Terminal
git switch main
git pull
git branch -D feature/cart

Quick reference

GitHub buttonLocal equivalentgh
Create a merge commitgit merge --no-ffgh pr merge --merge
Squash and mergegit merge --squashgh pr merge --squash
Rebase and mergegit rebase maingh pr merge --rebase