Skip to content
BENIBIX Learn

Git Merge and Conflicts

Level 2 · Branches and merges

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 contentsLesson 5 of 13

After this lesson you can

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

14 mincovers 4 glossary entries

Worth reading first: Git Branches and HEAD

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

MergeWhen it happensWhat main's history shows
Fast-forwardmain has no new commitsYour commits, in one straight line
Merge commitBoth branches moved onEvery commit, plus one joining commit
Squash mergeYou ask for itOne new commit for the whole branch

Which one to pick

  1. 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. 2

    Merge commit

    When you want to keep every step, and see where each branch joined.

  3. 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.
One branch, three ways back. Only the merge commit keeps both lines in the history.

Worked 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.

The words for this lesson4

Check yourself

What does a merge conflict mean?

Finished this lesson?

Marking it complete also marks its 4 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.