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. You have already seen what each method does. Here you compare what each leaves on the base branch, and how to tell them apart in an existing repository.
One pull request, three results
Take one scenario for every example. The main branch has two commits: Initial commit and Update config. A branch named feature forked from Initial commit and has two commits: Add parser and Add tests. Pull request #12 is open from feature into main.
The method you pick decides which commits end up on main:
- Create a merge commit keeps both feature commits and adds one new commit with two parents.
- Squash and merge adds one new commit with one parent. It contains the combined changes of the branch.
- Rebase and merge adds copies of both feature commits on top of
main. Each copy has a new SHA, and no merge commit is created.
Reading the graph
Run this on the base branch after the merge:
git log --oneline --graph --decorate mainAfter a merge commit, the graph has a visible fork and join:
* e4f5a6b (HEAD -> main, origin/main) Merge pull request #12 from acme/feature
|\
| * 9c1d2e3 Add tests
| * 7b8a9c0 Add parser
* | 3d4e5f6 Update config
|/
* 1a2b3c4 Initial commitAfter a squash merge, there is a single new commit and no trace of the branch's individual commits:
* 5a6b7c8 (HEAD -> main, origin/main) Add parser (#12)
* 3d4e5f6 Update config
* 1a2b3c4 Initial commitAfter a rebase merge, the graph is a straight line, but each feature commit is a new commit:
* c1d2e3f (HEAD -> main, origin/main) Add tests
* b0a9c8d Add parser
* 3d4e5f6 Update config
* 1a2b3c4 Initial commitThe rebased commits b0a9c8d and c1d2e3f have different SHAs from the original 7b8a9c0 and 9c1d2e3. A commit's SHA covers its parent, so changing the parent changes the SHA. GitHub's rebase and merge always creates new SHAs, even when a plain fast-forward would have been possible.
What each method keeps and loses
| Property | Merge commit | Squash and merge | Rebase and merge |
|---|---|---|---|
| Commits added to base | Feature commits plus one merge commit | One commit | One copy per feature commit |
| Original feature SHAs on base | Yes | No | No |
| Parents of the new tip | Two | One | One |
| Shape of the graph | Forks and joins | Linear | Linear |
| Per-commit detail from the branch | Kept | Collapsed into one message | Kept |
| Link back to the pull request | Merge commit message | `(#12)` suffix in the title | None in the commits |
| Revert the pull request | One revert with `-m 1` | One revert | One revert per commit |
The link back to the pull request is the easy one to miss. A rebase merge leaves no reference to the pull request in the commit messages. You can still find the pull request from the commit page on GitHub, but not from git log alone.
Effects on everyday tools
Blame and bisect
git blame shows the commit that last changed each line.
- After a merge commit or rebase merge, blame points at the small, focused commits from the branch, with their original messages.
- After a squash merge, blame points at the squash commit for every line the pull request touched. You then need the pull request page to learn why a line changed.
git bisect searches history for the commit that introduced a bug.
- On a squash history, every commit on
mainis a whole reviewed pull request, so bisect lands on a meaningful unit. - On a rebase history, each commit is a tested-in-isolation candidate only if the author made each one build. Intermediate commits that don't build can send bisect off course.
- On a merge commit history, bisect also walks the side branches. To bisect only the pull-request-sized steps, use
git bisect start --first-parent. Git supports this option in recent versions.
Reading only the pull-request level
The --first-parent option follows only the first parent of each commit, which is the base branch side of a merge commit:
git log --oneline --first-parent mainOn a merge-commit history, this shows one line per merged pull request and hides the branch commits. On a squash history, it changes nothing, because the history is already one commit per pull request.
Authorship and committer
In all three methods the original author is kept where commits are preserved. With a squash, the author is the pull request author, and GitHub adds Co-authored-by lines for other contributors on the branch. With a rebase, the author field is unchanged, but the committer information is updated, which is one reason the SHAs differ. Signatures on the original commits do not carry over to the copies.
Telling which method was used
You can inspect a repository you did not set up. Check the number of parents of recent commits on the base branch:
git log --oneline --merges -5 main
git rev-list --parents -n 1 mainThe first command lists only commits with two or more parents, which are merge commits. The second prints a SHA followed by its parent SHAs. Two parents means a merge commit. One parent could be either a squash or a rebase.
To tell squash from rebase, use the messages and the pull request link:
- A squash commit usually has a
(#123)suffix and a body that lists the branch's commit messages. - Rebased commits keep their original messages and usually have no
(#123)suffix.
The branch itself gives another check. Run this after fetching:
git branch --merged mainA branch merged with a merge commit appears in the list, because its tip is an ancestor of main. A branch merged with squash or rebase does not appear, because its original commits are not ancestors of main. Git thinks the branch is unmerged even though its changes are in main.
Same content, different history
All three methods should leave main with the same file contents when the branch has no conflicts, assuming the base did not move in a conflicting way. Only the history differs. You can check this on a throwaway repository by comparing trees:
git diff --stat main~1 mainIn the squash example, this shows the combined change of the pull request. In the merge-commit example, main~1 is the first parent, so the same command also shows the whole pull request. In the rebase example, main~1 is only the last rebased commit, so you would use main~2 main instead. That difference is the practical cost of rebase merges: the number of commits per pull request varies, so "the change for this pull request" is no longer one commit.
Trade-offs in short
- A merge commit is the most faithful record. It preserves the true shape of development and original SHAs. The cost is a branching graph and noisy history if branches contain "fix typo" commits.
- A squash merge is the cleanest. Each pull request is one commit, which makes reverts and scanning easy. The cost is lost detail, and later branches that build on the squashed branch get duplicate conflicts.
- A rebase merge keeps detailed commits and a straight line. The cost is rewritten SHAs, no merge marker, and the same dependent-branch problem as squash.
Both squash and rebase rewrite what Git sees as history. A local branch that still holds the original commits no longer has a common ancestor with main for that work. Later chapters cover how to recover from this and how to pick a method for a team.
Exercise
After a pull request is merged, git rev-list --parents -n 1 main prints a SHA followed by a single parent SHA, and git branch --merged main does not list the pull request's branch. Which merge methods are possible, and what could you look at to tell them apart?
Show answer
The tip has one parent, so it is not a merge commit. That leaves squash and merge, or rebase and merge. The branch is not listed as merged because its original commits are not ancestors of main in either case. To tell them apart, look at the last few commits on main. A squash adds one commit, usually with a (#123) title suffix and a body listing the branch's messages. A rebase adds one commit per feature commit, with their original messages and new SHAs.
Exercise
A team wants git log --first-parent main to show one line per merged pull request, and wants git blame to point at small commits with their original messages. Which method fits, and what does it cost?
Show answer
Create a merge commit. --first-parent then shows one merge commit per pull request, and blame sees the original branch commits. The cost is a branching graph, and any low-quality commits on the branch stay in history.