Merging Branches: Fast-Forward, Three-Way Merges, --no-ff and Squash Merges
How git merge combines branches: fast-forward and three-way merges, the merge base, choosing --ff-only, --no-ff or squash merges, previewing and stopping a merge, merge strategies and -X options, unrelated histories, and reading merges in the history.
kvi5hnuCode Sprout team member10 min readOn this page
Branches exist so that work can be combined later. git merge brings the commits of another branch into the current one. Depending on how the two branches relate, Git either just moves a pointer forward or creates a merge commit that joins two lines of history. This article covers both, the options that control which one you get, and squash merges. When both branches changed the same lines, the merge stops with a conflict, which is the subject of the next article.
The basic workflow
You merge into the branch you're on. To bring lake into main:
git switch main
git merge lake
Start with a clean working tree: commit or stash your changes first. Git refuses to merge if the merge would overwrite uncommitted changes, and a conflicted merge mixed with unrelated edits is hard to untangle.
Fast-forward and three-way merges
There are two cases, depending on whether the current branch has moved on since the other branch started:

- Fast-forward. If
mainhasn't changed sincelakewas created,main's commit is an ancestor oflake's. Git just movesmainforward tolake's commit. No new commit is created, and the history stays a straight line. - Three-way merge. If both branches have new commits, Git finds the merge base, the latest commit both share, and compares it with each branch's tip. Changes made on only one side are combined. The result is recorded as a merge commit with two parents.
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; echo "Ridge loop: 8 km" > trails.txt
git add . && git commit -q -m "Start project"
echo "=== fast-forward: main has no new commits"
git switch -q -c lake
echo "Lake path: 5 km" > lake.txt; git add .; git commit -q -m "Add lake page"
git switch -q main
git merge lake
git log --oneline --graph
echo "=== three-way: both branches have new commits"
git switch -q -c forest
echo "Forest walk: 3 km" > forest.txt; git add .; git commit -q -m "Add forest page"
git switch -q main
echo "body { margin: 0 }" > style.css; git add .; git commit -q -m "Add stylesheet"
echo "merge base: $(git merge-base main forest | cut -c1-7)"
git merge forest -m "Merge branch 'forest'"
git log --oneline --graph
=== fast-forward: main has no new commits
Updating 87b6836..af01b45
Fast-forward
lake.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 lake.txt
* af01b45 Add lake page
* 87b6836 Start project
=== three-way: both branches have new commits
merge base: af01b45
Merge made by the 'ort' strategy.
forest.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 forest.txt
* 30d2256 Merge branch 'forest'
|\
| * c26a22b Add forest page
* | 8fe53ee Add stylesheet
|/
* af01b45 Add lake page
* 87b6836 Start project
The first merge printed Fast-forward. The second printed Merge made by the 'ort' strategy. and created a merge commit, which --graph draws where the two lines join. git merge-base shows the commit Git compared against: "Add lake page", where forest started.
Without -m, Git opens your editor for the merge commit's message, pre-filled with Merge branch 'forest'. That default is usually fine. Add a line about why the work was merged if it's useful.
Choosing the result: --ff-only, --no-ff and --ff
| Option | When fast-forward is possible | When it isn't |
|---|---|---|
--ff (default) | fast-forward | merge commit |
--no-ff | merge commit anyway | merge commit |
--ff-only | fast-forward | refuse, and change nothing |
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"
git switch -q -c lake
echo "Lake path: 5 km" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "Boats for hire." >> lake.txt; git commit -q -am "Mention boat hire"
git switch -q main
echo "=== --no-ff: a merge commit even though a fast-forward was possible"
git merge --no-ff lake -m "Merge branch 'lake'"
git log --oneline --graph
echo "=== --ff-only when main has moved on"
git switch -q -c forest HEAD~1
echo "Forest" > forest.txt; git add .; git commit -q -m "Add forest page"
git switch -q main
echo "Maps included." >> README.md; git commit -q -am "Mention maps"
git merge --ff-only forest
echo "exit status: $?"
=== --no-ff: a merge commit even though a fast-forward was possible
Merge made by the 'ort' strategy.
lake.txt | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 lake.txt
* 529655d Merge branch 'lake'
|\
| * 659d6e9 Mention boat hire
| * d2a3ed1 Add lake page
|/
* 7fb62a0 Start project
=== --ff-only when main has moved on
hint: Diverging branches can't be fast-forwarded, you need to either:
hint:
hint: git merge --no-ff
hint:
hint: or:
hint:
hint: git rebase
hint:
hint: Disable this message with "git config set advice.diverging false"
fatal: Not possible to fast-forward, aborting.
exit status: 128
Why pick each:
--no-ffkeeps a record that a group of commits arrived together as one feature.git log --first-parentthen shows one line per feature onmain, and the whole feature can be undone by reverting one merge commit (article 25). Many teams merge pull requests this way.--ff-onlyguarantees you never create a merge commit by accident. It's a good default when updating a branch from its upstream, which is whygit pullusers often setpull.ff=only(article 19). When it refuses, rebase or merge deliberately.- The default suits most local work: it creates merge commits only when needed.
Set a default per repository or globally with merge.ff (false for --no-ff, only for --ff-only), or for one branch with branch.main.mergeOptions.
Squash merges
git merge --squash lake takes all the changes from lake and stages them as one change on the current branch, without committing and without recording a merge. You then commit, as a single ordinary commit:
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"
git switch -q -c lake
echo "Lake path: 5 km" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "Boats for hire." >> lake.txt; git commit -q -am "wip"
echo "Open May to September." >> lake.txt; git commit -q -am "fix typo"
git switch -q main
git merge --squash lake
git status -s
git commit -q -m "Add lake page with boat hire and opening months"
git log --oneline --graph --all
echo "=== Git doesn't know lake was merged"
git branch -d lake
Updating 7fb62a0..badc6e4
Fast-forward
Squash commit -- not updating HEAD
lake.txt | 3 +++
1 file changed, 3 insertions(+)
create mode 100644 lake.txt
A lake.txt
* badc6e4 fix typo
* a2b4912 wip
* d2a3ed1 Add lake page
| * 5a05ccd Add lake page with boat hire and opening months
|/
* 7fb62a0 Start project
=== Git doesn't know lake was merged
error: the branch 'lake' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D lake'
hint: Disable this message with "git config set advice.forceDeleteBranch false"
The Fast-forward line describes the change Git computed, but Squash commit -- not updating HEAD is what matters: main didn't move, and the changes were only staged. After the commit, the three messy commits became one clean commit on main. The trade-offs:
- The squashed commit has one parent, so Git doesn't know
lakewas merged.git branch -d lakerefuses,--mergeddoesn't list it, and merginglakeagain later would try to apply its changes a second time. Delete the branch with-Donce the squash is committed, and don't keep working on it. - The individual commits, and their messages, aren't on
main. That's the point when they're noise, and a loss when they told a useful story.
Hosting services offer the same three choices for pull requests: a merge commit, a squash merge, or a rebase merge (replaying the commits onto main, article 14). Teams pick one.
Previewing and stopping a merge
- See what's coming before merging:
git log --oneline main..lakelists the commits, andgit diff main...lakeshows the changes (articles 7 and 9). git merge --no-commit --no-ff lakeperforms the merge but stops before committing, so you can inspect or test the result. Thengit committo finish, orgit merge --abortto cancel.git merge --abortcancels a merge in progress, such as one that stopped with conflicts, and returns to the state before it started.- Undoing a finished merge that you haven't pushed:
git reset --hard ORIG_HEAD. Git setsORIG_HEADto where the branch was before the merge. Article 25 covers undoing merges in all situations.
Merge strategies and -X options
A strategy is the algorithm that combines the branches. You rarely need to choose one:
ortis the default for merging two branches since Git 2.34. It handles renames well and is fast. The olderrecursivestrategy was replaced by it: since Git 2.50,-s recursiveis accepted but runsort.octopusis used automatically when you merge more than two branches at once, as ingit merge lake forest. It refuses if there are conflicts.ours(-s ours) records a merge but keeps the current branch's content entirely, ignoring the other branch's changes. It's used to mark an old branch as merged without taking anything from it.
Strategy options, given with -X, adjust how ort works. Don't confuse them with the strategies above:
| Option | Effect |
|---|---|
-X ours | in conflicting parts only, keep our side. Non-conflicting changes from the other branch are still merged |
-X theirs | in conflicting parts only, keep their side |
-X ignore-space-change, -X ignore-all-space | treat whitespace-only differences as no change, avoiding conflicts from reindentation |
-X diff-algorithm=histogram | use a different diff algorithm (article 7) |
-X find-renames=40% | detect renames with a lower similarity threshold |
-s ours and -X ours sound alike but are very different: -s ours throws away all of the other branch's changes, while -X ours only decides conflicts. Use -X ours and -X theirs with care: they resolve conflicts without your judgement, so check the result.
Unrelated histories
Git refuses to merge two branches with no common ancestor: fatal: refusing to merge unrelated histories. This happens when you create a repository on a hosting service with a README, create another locally, and try to pull one into the other, or when combining two separate projects.
git merge --allow-unrelated-histories other/main
merges them anyway, treating the base as empty. Expect conflicts in files both sides created, such as README.md. Article 42 covers combining projects with subtree merges.
Merges in the history
git log --first-parentfollows only each merge's first parent: the branch you merged into. Onmain, that's one entry per merge, without the merged branches' commits.git log --mergeslists only merge commits, and--no-mergesleaves them out.git show <merge>shows a combined diff, often empty for a clean merge (article 10).git diff <merge>^1 <merge>, orgit show --first-parent <merge>, shows everything the merge brought in.<merge>^1is the first parent and<merge>^2the second, the tip of the merged branch (article 29).
What's current
ortis the default merge strategy, andrecursiveis now an alias for it.merge.ffandpull.fflet you choose fast-forward behaviour as a default. When your branch and its upstream have diverged,git pullrefuses to continue until you choose between merging and rebasing (article 19).- Hosting services can enforce a merge method per repository, and many require pull requests to be up to date with
mainfirst.
Common questions
Should I use merge commits, squash merges or rebase merges?
It's a team choice. Merge commits keep exact history and make reverting a whole feature easy. Squash merges keep main to one commit per change, and lose the branch's commits. Rebase merges keep the commits but give a straight line. All are fine if used consistently.
Which way round does git merge go?
It merges the named branch into the current branch. Only the current branch moves. The other branch is unchanged.
Why is there a "Merge branch 'main' into feature" commit in my branch?
You merged main into your feature branch, often through git pull with diverged branches. That's harmless. Some teams prefer rebasing the feature onto main instead, to keep the branch's history linear (article 14).
Is it safe to merge a branch twice? Yes. A real merge records what was merged, so merging again brings in only newer commits. Squash merges are different: Git doesn't record them, so a second squash merge tries to apply the same changes again.
Summary
git merge <branch>brings another branch into the current one. Start with a clean working tree.- If the current branch has no new commits, Git fast-forwards. Otherwise it creates a merge commit, comparing both tips with their merge base.
--no-ffalways creates a merge commit,--ff-onlyrefuses anything but a fast-forward, andmerge.ffsets the default.--squashstages the branch's changes for one ordinary commit. Git then doesn't consider the branch merged.--no-commitstops before committing,--abortcancels a merge in progress, andgit reset --hard ORIG_HEADundoes an unpushed merge.ortis the default strategy.-X ours/-X theirsdecide conflicts only, while-s oursdiscards the other branch's changes.
Practice questions
main is at commit B. feature was created from B and has commits C and D. What does git merge feature on main do, and what does git merge --no-ff feature do instead?
Answer
git merge feature fast-forwards: main moves to D, and no new commit is created, because B is an ancestor of D. git merge --no-ff feature creates a merge commit M with parents B and D, so the history records that C and D arrived together.
You ran git merge --squash feature and committed. A week later, git branch -d feature refuses. Why, and is it safe to delete?
Answer
A squash merge creates an ordinary commit with one parent, so feature's commits aren't ancestors of main, and Git considers the branch unmerged. If nothing was committed on feature after the squash, its changes are all in main, and git branch -D feature is safe. If you're unsure, git diff main feature shows what differs between the two tips, which is nothing if main hasn't moved since, though later work on main also shows up. git log main..feature doesn't help here: it lists all the branch's commits even after a squash.
What's the difference between git merge -X theirs other and git merge -s ours other?
Answer
-X theirs runs a normal merge, combining both branches' changes, and only where they conflict does it pick other's side. -s ours records a merge commit but keeps the current branch's files exactly as they are, ignoring every change from other. Neither is the opposite of the other: there is no -s theirs.
Previous: Git Branches: Creating, Switching, Listing, Renaming and Deleting
Next: Resolving Merge Conflicts: Conflict Markers, ours and theirs, Merge Tools and rerere

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.