Sub topic 6
Comparing the history each method leaves behind
This chapter assumes you can read git log output, know that a commit points to its parent commits, and have merged a pull request on GitHub before. Here you will see what each merge method leaves on the base branch and how to tell them apart afterward.
The same pull request, three results
Take a pull request with two commits, "Add feature" and "Add tests", branched from main at commit 1e2d3c4. The three merge methods turn it into three different histories.
Reading the graph
Run this on the base branch after merging:
git log --oneline --graph --decorate mainAfter Create a merge commit, the graph forks and rejoins:
* a1b2c3d (HEAD -> main, origin/main) Merge pull request #123 from user/feature
|\
| * 9f8e7d6 Add tests
| * 5c4b3a2 Add feature
|/
* 1e2d3c4 Previous workAfter Squash and merge, you see one commit and no trace of the branch:
* 7a6b5c4 (HEAD -> main, origin/main) Add feature (#123)
* 1e2d3c4 Previous workAfter Rebase and merge, you see a straight line with one commit per original commit:
* 3d2c1b0 (HEAD -> main, origin/main) Add tests
* 8e7f6a5 Add feature
* 1e2d3c4 Previous workWhat differs underneath
| Row | Merge commit | Squash and merge | Rebase and merge |
|---|---|---|---|
| New commits on base | Branch commits plus one merge commit | One | One per branch commit |
| Original SHAs on base | Kept | None | None, copies get new SHAs |
| Merge commit with two parents | Yes | No | No |
| Branch commits are ancestors of base | Yes | No | No |
| History shape | Branching | Linear | Linear |
The "ancestors of base" row has practical consequences. After a merge commit, Git knows the branch is fully merged. After a squash or rebase, the original branch commits are not reachable from main. Git sees the branch as unmerged even though its changes are in main. The effects:
git branch --merged maindoes not list a squashed or rebased branch, so you needgit branch -Dto delete it locally.git log main..featurestill lists the old commits.- Any branch built on top of the old one will try to replay commits that already landed. The "Follow-up branches" chapter covers this.
Trade-offs in the history
Each method gives up something.
- Merge commit keeps everything, including work-in-progress commits such as "fix typo" or "wip". The graph gets busy, and
git logwithout--first-parentinterleaves commits from many branches.git log --first-parent mainshows only the merge commits, one line per pull request. - Squash and merge gives one reviewed unit per pull request. That makes bisecting and reverting simple. You lose the individual commits, so a large pull request becomes one large commit.
- Rebase and merge keeps individual commits without merge noise. The commits on
mainare copies, so SHAs you referenced in review comments or issues no longer match. Each copy was not tested in that exact position on the new base, so intermediate commits may not build.
Identifying a method from history alone
You can work out how a pull request was merged by looking at the commit:
- A commit with two parents is a merge commit. Check with
git log --merges main. - A single-parent commit whose title ends in
(#123)is usually a squash. GitHub adds that suffix by default. - Several consecutive single-parent commits with no pull request reference are probably a rebase merge.
These are heuristics. Titles can be edited before merging, so confirm on the pull request page.
Exercise
A team's main shows git log --oneline --graph as a single straight line, and git log --merges main prints nothing. Can you tell whether pull requests were squashed or rebased? What would you check?
Show answer
No. Both methods give a linear history with no merge commits. Compare commit counts against the pull request: one commit per pull request suggests squash, and one commit per original branch commit suggests rebase. Also check the pull request's merge method on its page, or the repository's allowed methods in settings.