# Git Merge and Conflicts

> How Git joins one branch into another. Fast-forward, merge commit and squash merge side by side, and how to fix a merge conflict by hand.

**Course:** [Git and GitHub](https://learn.benibix.com/learn/development-version-control)  
**Time:** 14 min

**After this lesson you can:**

- Tell the three kinds of merge apart
- Pick a merge for your team
- Fix a merge conflict

## How Git joins two branches

A merge joins the work from one branch into another. You stand on the branch that should get the work, usually main, and run git merge with the other branch's name. Git picks one of two ways to do it, depending on whether main moved on while you worked. A third way, the squash, is one you ask for.

If main has no new commits since your branch split off, there is nothing to combine. Git just slides the main label forward to your newest commit. That is a fast-forward merge. No new commit is made, and the history stays one straight line.

If both branches have new commits, Git makes a merge commit. It is a commit with two parents, one from each line, and it ties them together. The history keeps every small step from the branch, plus the point where the two lines met.

A squash merge turns all the commits on a branch into one new commit on main. Twelve small steps like fix typo and try again become one tidy commit, such as Add a dark mode switch. Main's history stays short. The cost is that the small steps are not kept on main.

A merge conflict happens when both branches changed the same lines in different ways. Git cannot guess which one is right, so it stops halfway and asks you. Conflicts are normal. They do not mean anything is broken, only that two people touched the same spot.

Git marks the spot inside the file with three marker lines. Your side sits between the line of less-than signs and the line of equals signs. The other branch's side sits below, down to the line of greater-than signs. Edit the file to keep what should stay, delete all three markers, then add the file and commit.

If a conflict looks too big, git merge --abort puts everything back the way it was before you started. Nothing is lost. Pulling in main often, and keeping branches small, is what keeps conflicts rare and short.

The classic mistake is to commit a file with the markers still in it. The code then breaks, often in an odd way far from the conflict. Before you commit, search the file for seven less-than signs in a row. Most code editors also colour the two sides, so a leftover marker is easy to spot.

## The three ways to merge

| Merge | When it happens | What main's history shows |
| --- | --- | --- |
| Fast-forward | main has no new commits | Your commits, in one straight line |
| Merge commit | Both branches moved on | Every commit, plus one joining commit |
| Squash merge | You ask for it | One new commit for the whole branch |

## Which one to pick

1. **Fast-forward.** You do not pick it. Git does it on its own when main has not moved, and it is always safe.
2. **Merge commit.** When you want to keep every step, and see where each branch joined.
3. **Squash merge.** When a branch is full of small fix-up commits, and main should read as one change per feature.

```console
$ git merge red
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
$ cat index.html
<<<<<<< HEAD
<h1>Blue shop</h1>
=======
<h1>Red shop</h1>
>>>>>>> red
```
Both branches changed the same line. Your side is under HEAD, the red branch's side is under the equals signs.

![A main branch runs along the top. A feature branch splits off it, gets two commits, and comes back three ways. Fast-forward: main had no new commits, so its label just slides forward to the feature's last commit. Squash merge, lit up: the two feature commits become one new commit on main. Merge commit: a new commit with two parents joins both lines.](https://learn.benibix.com/diagrams/dev3-branch-merges.svg)

### Example: Jack and Emma edit one heading

Jack changes the shop's heading to Blue shop on main. Emma, on her own branch, changes the same line to Red shop.

Her merge stops with 1 conflict in index.html. They agree on Red shop, so she deletes Jack's line and the 3 marker lines, then commits. It takes 2 minutes.

**Words in this lesson:** [Merge](https://learn.benibix.com/term/development/merge), [Fast-Forward Merge](https://learn.benibix.com/term/development/fast-forward-merge), [Squash Merge](https://learn.benibix.com/term/development/squash-merge), [Merge Conflict](https://learn.benibix.com/term/development/merge-conflict)

> **The one thing to remember.** A fast-forward only moves a label. A merge commit keeps every step, and a squash merge keeps one commit for the whole branch.

**Question:** What does a merge conflict mean?

**Answer:** Two branches changed the same lines in different ways. Git can join changes to different lines on its own. When both sides changed the same lines, it cannot know which is right, so it stops and asks you to choose.

---

Source: https://learn.benibix.com/learn/development-version-control/git-merge-and-conflicts
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
