Merge methods: squash, rebase and merge

Intermediate · 6 min

GitHub offers three ways to merge a pull request. See the history each leaves, why a squashed branch looks unmerged to Git, and which suits your team.

Before thisMerging: fast-forward and three-wayMerge or rebase: choosingPull requestsCode review on GitHub

Dev Leonardo · 6 min

Merge methods: squash, rebase and merge

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Dev LeonardoReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A merge commit keeps every commit from the branch and adds one merge commit, so history shows exactly how work was integrated.
  • Squash and merge folds the whole pull request into one new commit on the base branch, giving a clean main but losing the individual commits and their SHAs.
  • Rebase and merge replays each commit onto the base with new SHAs and no merge commit, which gives linear history but is not a plain fast-forward.
  • After a squash or rebase merge, git branch -d refuses to delete your local branch, because the original commits are not in main's history.
  • If you keep working on a branch after it was squash-merged, merging main back in can conflict on changes you already shipped, so start new work from a fresh branch.

One button, three histories

When a pull request is approved you click merge, and GitHub lets you choose how. The choice has no effect on the code that ends up in main. It changes the history: which commits exist, what they are called, and whether the shape is a straight line.

You met the underlying Git operations in merging: fast-forward and three-way and merge or rebase. GitHub packages three variants as buttons, and what they do differs a little from the local command of the same name.

What each method leaves behind

I reproduced all three in a scratch repository, with a local bare repository standing in for GitHub. Each branch had three commits (feat, fix, wip: review feedback), and I merged them with the nearest Git command. The results below are real output. The caveats about GitHub’s own behaviour come from its docs, read on 2026-11-23.

Create a merge commit. Equivalent to git merge --no-ff. All three commits are kept, plus one merge commit with two parents.

*   154952a Merge pull request #1 from ada/mc
|\
| * 9a717bf wip: mc review feedback
| * 17e0891 fix: mc typo
| * d47f041 feat: mc part one
|/
* 2bebdc8 feat: add notes command

Everything is preserved. git log --first-parent main shows one line per pull request, and the branch commits sit behind it. The cost is a noisy graph, and a history full of “wip” commits.

Squash and merge. Equivalent to git merge --squash followed by a commit. The three commits become one new commit on main.

* 3fc049a feat: sq (#2)

The history is clean, one commit per PR, which is why many teams choose it. GitHub’s docs say the cost is that you lose the original authorship detail and SHAs of the individual commits.

Rebase and merge. The commits are replayed onto the tip of main, one by one, with no merge commit. History is a straight line. The docs say this is not the same as a fast-forward: GitHub always creates new commits with new SHAs and updates the committer, where plain git rebase can keep the original committer information. It also cannot add signatures to the rewritten commits, so a rebase-merged branch loses commit signature verification.

I imitated GitHub’s rewrite with git rebase --force-rebase under a committer named GitHub. The log then showed commits whose author was Ada and committer was GitHub, with SHAs unlike the originals.

Keeps individual commits Merge commit Original SHAs Linear history
Create a merge commit yes yes yes no
Squash and merge no, folded into one no no yes
Rebase and merge yes no no, new SHAs yes

The cleanup surprise

After merging a pull request, you want to delete your local branch. I ran git branch -d on each:

Deleted branch mc (was 9a717bf).
error: the branch 'sq' is not fully merged

The merge-commit branch deleted cleanly. The squashed branch refused. So did my simulated rebase-merged one (error: the branch 'rb2' is not fully merged).

The reason is how Git decides “merged”: the branch tip must be reachable from where you are. A squash commit is a new commit that no longer links back to the originals, and rebased commits have different SHAs, so neither looks merged. Nothing is wrong. Once you have confirmed the PR merged, use git branch -D sq, or turn on GitHub’s setting that deletes head branches automatically and run git fetch --prune to tidy your remote-tracking branches.

git cherry main sq is a useful check. It compares patches, not SHAs. After the rebase-merge it marked the old commits -, meaning an equivalent patch is already upstream. After the squash it marked them +, because three patches were folded into one that matches none of them individually.

The long-lived branch trap

I also tested the situation GitHub’s docs warn about. I squash-merged a branch containing a new file, f.txt. I then added another commit to that same branch and merged main into it. Git reported CONFLICT (add/add): Merge conflict in f.txt. Git does not know the squash commit contains your earlier commits, so it sees two unrelated additions of the same file.

The rule that avoids this: after a squash merge, abandon the branch. Start the next piece of work with git switch main, git pull, git switch -c next-thing.

Choosing

  • Merge commit when you want the full record, or when branches are shared by several people. It never rewrites anything.
  • Squash and merge when pull requests are small and the individual commits are not worth keeping. Write the final commit message carefully, since it is the only one that survives. If your team uses Conventional Commits, the PR title usually becomes the commit subject, so name it that way.
  • Rebase and merge when you want linear history and each commit is meaningful on its own. Clean commits are required, which the interactive-rebase lesson shows how to produce.

A repository can switch methods on or off in its settings, so a team can enforce one. A rule that requires linear history, which you will see in branch protection and rulesets, pushes people toward squash or rebase because merge commits are blocked.

What the video covers

Dev Leonardo’s “How to Close a Pull Request - Merge Commit vs Squash vs Rebase on GitHub” is about six minutes and was published around 2023. The description says it explains the three strategies and what a merge commit is. I could not read a transcript. It is older, so button names and layout may differ slightly from today’s page, but the three strategies themselves have not changed. English is judged from the title and the channel. It does not cover the branch-deletion and long-lived-branch problems above, which is why they are on this page. A short opinion piece on squashing is linked under deeper.

Where this fits

Merging is the last step of one change. Tracking the changes you want next is the job of issues and projects: issues and projects.

Check yourself

  1. 01Which merge method gives one commit on main per pull request?

    Squash and merge. All the pull request's commits are combined into a single new commit on the base branch.

  2. 02Why does git branch -d refuse to delete a branch whose pull request was squash-merged?

    Git checks whether the branch's commits are reachable from the branch you are on. A squash commit is a new commit with different content and ancestry, so Git sees the originals as unmerged. Use -D once you know it was merged.

  3. 03Is Rebase and merge on GitHub the same as a fast-forward?

    No. GitHub always creates new commits with new SHAs and its own committer, even when a fast-forward would have been possible. A fast-forward moves the pointer and keeps the original commits.

  4. 04You squash-merged your branch, then added another commit on it and merged main in. What goes wrong?

    Git does not know your earlier commits were already applied through the squash commit, so the same changes collide as conflicts. Branch from the updated main for the next piece of work instead.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark