What Does Merging Mean in Git?
A complete, no-assumptions walkthrough of how Git combines separate lines of work back into one — the different merge types, why conflicts happen, and how to resolve them with confidence.
Imagine two co-authors writing a book together. Each of them takes a private copy of the manuscript home, rewrites different chapters over the weekend, and on Monday they need to combine both sets of edits into a single, final manuscript. If they worked on completely different chapters, combining the two copies is easy — just stitch the pages together. But if they both rewrote the same paragraph on page 42 in different ways, someone has to sit down and decide which version — or which blend of both — becomes the real page 42. Merging in Git is exactly this process of stitching two separate lines of work back into one, automatically when possible, and asking for your judgment when it isn’t.
1What Is Merging?
Let’s build a precise definition before diving into mechanics.
The Simple Definition
Merging is the process of combining the changes from one branch into another, producing a single unified history that contains the work from both. Most commonly, this means taking the finished work from a feature branch and folding it back into the main branch, so the rest of the team can benefit from it.
Think of two rivers flowing separately down a mountain and eventually joining into one wider river downstream. Each river carried its own water (its own commits) the whole way down, but from the merge point onward, they flow together as a single stream carrying both histories forward.
Why This Concept Exists
Branching lets multiple people (or multiple features) develop in complete isolation, which is fantastic for avoiding interference while work is in progress. But isolated work is only useful once it’s brought back together — a feature sitting forever on its own branch never reaches users. Merging is the deliberate, controlled step that reunites that isolated work with the rest of the project.
Merging never deletes or discards the original branch’s history — it adds the new commits into the target branch while preserving exactly how and when each change was made.
2The Two Branches Involved
Every merge always involves exactly two roles, even if the underlying branches have many commits each.
Target Branch
The branch you are currently on when you run the merge — this is the branch that will end up containing the combined result.
Source Branch
The branch whose changes you want to bring in. Its own history stays untouched by the merge — only the target branch is updated.
Common Ancestor
The last commit both branches shared before they diverged — Git uses this as the reference point to figure out what actually changed on each side.
Merge Commit
A special commit created by the merge itself, which — unlike a normal commit — has two parent commits instead of one, recording that two histories joined here.
graph LR
A[Common Ancestor] --> B[Main branch continues]
A --> C[Feature branch continues]
B --> M[Merge Commit]
C --> M
3How Git Decides What to Merge
Understanding the “common ancestor” from Chapter 2 is the key to understanding everything Git does during a merge.
Find the common ancestor
Git looks back through both branches’ histories to find the most recent commit they both share — the point where the two lines of work first split apart.
Compare each branch against that ancestor
Git calculates exactly what changed on the target branch since that point, and separately what changed on the source branch since that same point.
Combine the two sets of changes
Wherever the two branches touched different parts of the project, Git applies both sets of changes automatically, with no need for you to intervene.
Flag any overlapping edits
If both branches changed the exact same lines of the exact same file in different ways, Git can’t safely guess which version is correct, so it pauses and asks you to resolve the overlap — this is a “conflict,” covered in Chapter 5.
Record the result as a new commit
Once every change has been combined (and any conflicts resolved), Git creates the merge commit described in Chapter 2, permanently recording that these two histories are now one.
4The Two Main Types of Merge
Not every merge produces a merge commit. Which type happens depends entirely on whether the branches actually diverged.
Fast-Forward Merge
If the target branch hasn’t received any new commits since the source branch split off from it, there’s nothing to actually combine — the source branch is simply “ahead.” In this case, Git can just slide the target branch’s pointer forward to match the source branch, in a straight line, with no new merge commit needed at all.
Three-Way Merge
If both branches have moved forward independently since they split — which is the far more common real-world case on an active team — Git must compare all three points (the common ancestor, plus each branch’s tip) to work out the combined result, and it records that combination as a proper merge commit with two parents.
| Merge Type | When It Happens | Creates a Merge Commit? |
|---|---|---|
| Fast-Forward | Target branch has no new commits since the split | No — pointer simply moves forward |
| Three-Way | Both branches have new, independent commits | Yes — a merge commit with two parents is created |
A fast-forward “merge” isn’t really combining two histories at all — it’s just catching the target branch up to where the source branch already is. The real combining logic only kicks in for three-way merges.
graph TD
subgraph "Fast-Forward"
A1[main at C1] --> A2[feature adds C2, C3] --> A3[main pointer simply moves to C3]
end
subgraph "Three-Way"
B1[main adds C4]
B2[feature adds C5]
B1 --> B3[Merge Commit combines C4 and C5]
B2 --> B3
end
5Merge Conflicts — Why They Happen
This is the single most intimidating part of merging for beginners, so let’s demystify it completely.
A merge conflict happens only when both branches changed the exact same lines of the exact same file in genuinely different ways since they split from their common ancestor. Git is very good at automatically combining changes that touch different areas, but it has no way of knowing which of two competing edits to the same spot is “correct” — that judgment call belongs to a human. When this happens, Git pauses the merge partway through, marks the conflicting section directly inside the affected file, and waits for you to decide how the final version should read.
Picture two editors independently rewriting the same sentence of an essay in different ways. A good editing assistant can happily combine edits to two separate sentences without asking anyone, but the moment both editors touch the exact same sentence, the assistant has to stop and ask which version — or what blend — should be kept.
A conflict is not a mistake or a bug — it’s Git correctly recognizing a genuine ambiguity that only a person can resolve. Seeing one simply means two people happened to edit the same spot.
6Resolving a Conflict, Step by Step
Once you know what’s happening, resolving a conflict is a calm, mechanical process rather than a scary one.
Git pauses and lists the affected files
Instead of finishing the merge, Git stops midway and tells you exactly which files contain unresolved overlaps, leaving everything else already combined.
Open the file and find the markers
Inside each conflicted file, Git inserts clear markers showing exactly where your target branch’s version and the incoming branch’s version disagree, side by side.
Decide on the final content
You edit the file by hand to keep one version, the other, a blend of both, or something entirely new — then remove the markers so only clean final content remains.
Mark the file as resolved
You tell Git that this particular file’s conflict has been settled, which moves it out of the “unresolved” list.
Complete the merge
Once every conflicted file has been resolved, you finalize the merge, and Git creates the merge commit exactly as it would have if no conflict had occurred.
You can always step away from a merge conflict and abandon it entirely, returning your branch to exactly how it looked before the merge began — nothing is permanently lost until you deliberately finish and commit the resolution.
7Merging vs. Rebasing
Merging isn’t the only way to combine branches — rebasing is a common alternative worth understanding by contrast.
Preserves True History
Keeps a complete, honest record of when each branch actually diverged and reunited, including a visible merge commit marking the join.
Non-Destructive
Never rewrites or replaces any existing commit — it only ever adds new ones, making it safe even on branches other people are using.
Rewrites History
Instead of joining two branches with a merge commit, rebasing replays one branch’s commits on top of the other, producing a straight, linear history.
Riskier on Shared Branches
Because it rewrites commit history, rebasing a branch others are already using can cause serious confusion and is generally reserved for private, unshared work.
Neither approach is universally “better” — merging favors an honest, traceable record of exactly what happened, while rebasing favors a cleaner, easier-to-read straight-line history at the cost of rewriting the past. Many teams use merging for shared branches and reserve rebasing for tidying up their own private, not-yet-shared work.
8Advantages and Trade-offs
Merging is the default, foundational way Git combines work, but it comes with its own set of considerations.
Advantages
- Preserves a complete, accurate record of how and when branches diverged and reunited.
- Safe to use on shared branches since it never rewrites existing history.
- Handles the vast majority of changes automatically, without any manual intervention.
- Conflict markers make disagreements explicit and easy to locate inside affected files.
Disadvantages / Trade-offs
- Frequent three-way merges can leave a project history that’s harder to read in a straight line.
- Conflicts, while manageable, can feel intimidating to developers new to Git.
- Large, long-lived branches tend to accumulate more conflicts the longer they stay unmerged.
9Best Practices and Common Mistakes
A short, practical checklist for keeping merges smooth and conflicts rare.
Merge and update often
Regularly bringing the latest target-branch changes into your feature branch keeps the eventual final merge small and manageable.
Read conflict markers carefully
Take the time to understand what each side of a conflict was trying to accomplish before choosing how to resolve it.
Letting branches live for months
The longer a branch stays separate from the main line of work, the more likely it is to pile up painful, tangled conflicts.
Resolving conflicts blindly
Simply picking “my version” everywhere without reading the other side risks silently discarding a teammate’s important fix.
Problem
A developer keeps a feature branch open for weeks without ever merging in the latest changes from the main branch.
Why It’s Harmful
By the time they finally attempt the merge, dozens of unrelated changes have piled up on both sides, producing a huge, confusing wall of conflicts that’s difficult to resolve correctly.
Correct Approach
Regularly merge the main branch into your feature branch throughout development, resolving small conflicts as they appear rather than facing one enormous conflict at the very end.
10Real-World Examples
Merging sits at the center of nearly every collaborative software workflow in the industry today.
Pull Request Workflows
On platforms like GitHub and GitLab, clicking the “Merge” button on an approved pull request performs exactly the merge process described in this post, folding a feature branch into the main branch.
Release Branch Integration
Many teams periodically merge a stable release branch back into their main development branch to keep hotfixes and ongoing feature work synchronized.
Large Open-Source Projects
Projects like the Linux kernel process thousands of merges from contributors worldwide, relying on the exact same conflict-detection mechanics covered in Chapters 5 and 6, just at a much larger scale.
11Frequently Asked Questions
No. Merging only combines the history into the target branch — the source branch still exists afterward and can be deleted separately, purely for tidiness, once you no longer need it.
No — most merges complete automatically with no conflict at all, since Git only asks for help when both branches genuinely changed the exact same lines.
Yes, a completed merge can be reversed, though it’s generally cleaner to abandon an in-progress, unfinished merge before it’s committed rather than undoing one afterward.
Technically it’s the simplest possible case — since there’s nothing to combine, Git just moves the branch pointer forward, with no merge commit created.
Some teams prefer a perfectly linear history for readability and choose rebasing instead, but this trades away the honest record of exactly when and how branches diverged, which merging preserves.
12Summary and Key Takeaways
Merging is how Git reunites separate lines of development into one shared history. By comparing each branch against their last common ancestor, Git can combine most changes automatically, and only pauses to ask for human judgment when both branches genuinely edited the same content in conflicting ways. Understanding the difference between a simple fast-forward and a true three-way merge — and knowing that a conflict is a normal, manageable event rather than an error — turns merging from an intimidating obstacle into a routine, predictable part of everyday development.
Key Takeaways
- Merging combines, never deletes — the source branch’s history is added to the target, not erased.
- The common ancestor is the key — Git compares both branches against their last shared commit to figure out what’s new.
- Two merge types exist — fast-forward simply moves a pointer, while a three-way merge creates a real merge commit.
- Conflicts are normal — they only happen when both branches genuinely changed the same lines, and they’re always resolvable by hand.
- Nothing is lost mid-conflict — an unfinished merge can always be safely abandoned before it’s committed.
- Merge vs. rebase — merging preserves true, honest history; rebasing trades that for a cleaner, linear look.
- Merge often, not rarely — frequent, small merges avoid the painful pile-up of conflicts that long-lived branches accumulate.