Merging Branches
git merge integrates the changes from one branch into another by creating a new snapshot that combines both histories. What actually happens under the hood depends entirely on the relationship between the two branches' commit graphs: if one branch is simply ahead of the other with no divergent commits, Git performs a fast-forward merge; if both branches have unique commits since they diverged, Git performs a true three-way merge, producing a new merge commit with two parents. Understanding which case applies explains a lot of behavior that otherwise seems inconsistent, such as why merging sometimes produces an extra 'Merge branch' commit and sometimes doesn't.
Cricket analogy: Whether a merge is a fast-forward or a true three-way merge is like whether a chasing team just needs to match the target (simple ahead) or both sides batted separately and the result must be reconciled from two innings' scorecards, like an Ashes Test with a follow-on decision.
Fast-forward merges
A fast-forward merge happens when the branch you are merging into (say, main) has had no new commits since the feature branch diverged from it — meaning main's tip is a direct ancestor of the feature branch's tip. In this case there is nothing to actually combine; Git simply moves the main pointer forward to match the feature branch's latest commit. No new commit is created, and the resulting history looks perfectly linear, as though the work had been done directly on main all along.
Cricket analogy: A fast-forward merge is like a team chasing a target that hasn't moved since the last update — since main hasn't added any runs of its own, the scoreboard simply jumps to match the feature branch's total with no extra over needed to reconcile anything.
Three-way merges and merge commits
When both branches have diverged — each has commits the other lacks — Git cannot simply move a pointer. Instead it identifies the common ancestor (the merge base) and performs a three-way comparison between that ancestor, the tip of the current branch, and the tip of the branch being merged in. Git then creates a new merge commit with two parent pointers, recording that this snapshot unifies both lines of history. This merge commit is what shows up as the diamond shape in git log --graph output, and it is what future commands like git log --first-parent can use to distinguish mainline history from feature work that was merged in.
Cricket analogy: This is like the third umpire reviewing footage from both the bowler's end and square leg to reconstruct a run-out — comparing the last agreed frame (merge base) with both camera angles (branch tips) to produce one definitive ruling (merge commit) citing both sources.
# Fast-forward case: main has no new commits since feature/logging diverged
$ git switch main
$ git merge feature/logging
Updating 7d6e5f4..9f8e7d6
Fast-forward
src/logging/setup.py | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
# Three-way case: main advanced independently since divergence
$ git switch main
$ git merge feature/rate-limiting
Merge made by the 'ort' strategy.
src/middleware/rate_limit.py | 40 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 40 insertions(+)
$ git log --oneline --graph -3
* e4f5a6b (HEAD -> main) Merge branch 'feature/rate-limiting'
|\
| * 9f8e7d6 (feature/rate-limiting) Add token-bucket limiter
* | 3c2b1a0 Fix flaky integration test
|/
* 7d6e5f4 Initial API scaffold
# Force a merge commit even when a fast-forward would be possible
$ git merge --no-ff feature/loggingMany teams deliberately pass --no-ff when merging feature branches into main, even when a fast-forward is possible, so that every feature retains a visible merge commit in history — making it easy to see and later revert an entire feature as one unit.
If a merge produces conflicts, Git pauses mid-merge with the repository in a special conflicted state; committing prematurely without resolving all conflict markers (<<<<<<<, =======, >>>>>>>) will bake broken code directly into your history.
git mergecombines the history of one branch into another currently checked-out branch.- A fast-forward merge simply advances the branch pointer when no divergence has occurred — no new commit is created.
- A three-way merge occurs when both branches have unique commits, and produces a merge commit with two parents.
--no-ffforces a merge commit even when a fast-forward would otherwise be possible, preserving feature boundaries in history.- The merge base (common ancestor) is what Git uses to compute a three-way diff during merging.
- Unresolved conflicts pause the merge; committing without resolving them writes broken, conflict-marked code into history.
Practice what you learned
1. When does Git perform a fast-forward merge instead of creating a merge commit?
2. How many parent commits does a typical three-way merge commit have?
3. What does `git merge --no-ff` do?
4. What is the 'merge base' in a three-way merge?
5. How can you safely back out of a merge that has entered a conflicted state before resolving anything?
Was this page helpful?
You May Also Like
Resolving Merge Conflicts
Learn how Git detects merge conflicts, how to read conflict markers, and the practical workflow for resolving them safely and finishing the merge.
Rebasing vs Merging
Compare git rebase and git merge as two different strategies for integrating branch changes, and learn when a linear history is worth the trade-offs of rewriting commits.
Creating and Switching Branches
Master the day-to-day commands for creating, switching, listing, and deleting branches, including the modern git switch/git restore split from git checkout.
git revert vs git reset
Two different ways to undo changes in Git — revert creates a new commit that inverses a prior one, while reset moves the branch pointer and rewrites history — and when to use each safely.
Related Reading
Related Study Notes in Software Engineering
Browse all study notesMicroservices Study Notes
Software Architecture · 30 topics
Software EngineeringTesting & TDD Study Notes
Software Testing · 30 topics
Software EngineeringDesign Patterns Study Notes
Software Design · 30 topics
Software EngineeringSoftware Engineering Study Notes
Python · 40 topics
Software EngineeringSystem Design Study Notes
Architecture · 40 topics