Sub topic 4
Squash and merge
This chapter assumes you can open and review a pull request, read git log output, and know that a branch is a movable pointer to a commit. It builds on the overview of what merging a pull request does. It covers the squash method on its own.
What squash and merge does
Squash and merge takes every commit in the pull request and combines their combined changes into one new commit on the base branch. The original commits are not added to the base branch.
The new commit has these properties:
- It has one parent, the previous tip of the base branch.
- Its file contents match what a merge of the branch would have produced.
- It is a new commit with a new SHA. None of the branch's SHAs appear in the base branch.
Because S has no link to F2, Git cannot tell from the graph that feature was merged. This explains most of the side effects described below.
Reproducing it locally
You can reproduce the same result by hand:
git switch main
git merge --squash feature
git commit -m "Add feature"git merge --squash stages the combined changes but does not create a commit. It also does not record feature as a second parent. You write the commit message yourself. On GitHub, the web interface does these steps for you and then pushes the result.
What you see in the history
Run this on the base branch after the merge:
git log --oneline --graph --decorate mainThe output is a straight line, with one entry per pull request. Intermediate commits such as "fix typo" or "address review comments" are gone from main. The branch still holds them until you delete it.
Consequences to know about
Tidy history, less detail. Each pull request becomes one unit. This makes main easy to read and easy to revert. It also means git blame and git bisect can only point to the whole squashed change, not to the small step inside it.
The branch looks unmerged to Git. After the merge, git branch --merged main does not list feature, because its commits are not ancestors of main. GitHub's "Delete branch" button is the usual cleanup. From the CLI, add --delete-branch:
gh pr merge 123 --squash --delete-branchReverting is simple. The squash commit is an ordinary commit with one parent, so you revert it with git revert <sha>. You do not need the -m option that merge commits require.
Follow-up branches can conflict. If another branch was created from feature, its history still contains F1 and F2. Git sees those as changes that main lacks, even though S already contains them. The result is duplicate conflicts. A later chapter covers the fix with git rebase --onto.
Authorship
The squash commit is attributed to the person who opened the pull request. Other contributors to the branch are not credited unless someone adds Co-authored-by: lines to the message. The default message text is covered in the next chapters.
Exercise
A teammate squash-merged pull request 123, then you notice that your branch feature-b, which was created from feature-a, now shows conflicts on files you did not touch. Why does this happen, and what command fixes it?
Show answer
feature-b still contains the original commits of feature-a. Those commits are not in main, only the new squash commit with the same changes is, so Git treats them as conflicting changes. Fix it by moving only the commits unique to feature-b onto the updated base: git fetch origin and then git rebase --onto origin/main feature-a feature-b.