Gitastic
Path › Part 2: Branching › Lesson 6 of 16
06

Merge conflicts, calmly

Git asks you a question. You answer it. That is all a conflict is.

1 Learn the idea

A merge conflict looks scary, but nothing is broken and nothing is lost. It simply means: both branches changed the same part of the same file, and git refuses to guess which version you want.

Reading the markers

<<<<<<< HEAD
Soup of the day: lentil
=======
Soup of the day: pumpkin
>>>>>>> autumn
  • Between <<<<<<< HEAD and =======: your version (the branch you are on).
  • Between ======= and >>>>>>> autumn: their version (the branch being merged in).
  • Your job: edit the file into the version you want and delete all marker lines.

The recipe

  1. git status — see which files are "both modified".
  2. Open each file, pick/combine the content, remove the markers.
  3. git add <file> — tells git "this one is resolved".
  4. git commit — finishes the merge with a merge commit.

2 Watch it happen

Resolving a real conflict

Press Play or use → to step through. Watch the graph as each command runs.

a6e5d67 Initial menua6e5d67Initial menue32c70b Autumn soupe32c70bAutumn soupbdd27cd Winter soupbdd27cdWinter soupBranch "autumn" — a movable pointerautumnHEAD = where you are. It points at the current branch.HEAD →Branch "main" — a movable pointer (current branch: moves when you commit)main
Working directoryyour folder / editor
menu.txt
git add →← git restore
Staging areathe next snapshot (index)
menu.txt
git commit →← restore --staged
RepositoryHEAD → bdd27cd
menu.txt
terminal · simulated, no real repository is touched
Read this demo as text
  1. $ git log --oneline --all

    Both main and autumn changed the same line of menu.txt — tomato became lentil on one side, pumpkin on the other.

  2. $ git merge autumn

    CONFLICT! Git merged what it could, but stops at menu.txt and asks you to decide. The merge is paused, not failed.

  3. $ git status

    git status is your map during a merge: it lists the unmerged paths ("both modified") and tells you what to do next.

  4. $ cat menu.txt

    The file now contains both versions, wrapped in conflict markers: yours (HEAD) on top, theirs (autumn) below.

  5. $ edit menu.txt "Soup of the day: pumpkin & lentil"

    We write the version we actually want — here, a combination — and remove every marker line.

  6. $ git add menu.txt

    git add marks the file as resolved. Look at the staging area: the conflict is gone.

  7. $ git commit -m "Merge autumn menu"

    The merge commit is created with two parents. Conflict resolved — nothing was lost along the way.

  8. $ git log --oneline

    History shows both soup commits joined by our merge commit.

  • Conflicts only happen when both sides changed the same lines (or one edited a file the other deleted).
  • Changes to different files, or different parts of a file, merge automatically.
  • Small, frequent merges mean small, easy conflicts.
git statusSee which files are in conflict
git add <file>Mark a conflicted file as resolved
git commitConclude the merge
git merge --abortCancel and go back to before the merge

3 Check your understanding

Question 1

When does git report a merge conflict?

Question 2

In a conflicted file, what is between <<<<<<< HEAD and =======?

Question 3

You edited the file and removed the markers. What tells git the conflict is resolved?

Question 4

Halfway through resolving, you realise you merged the wrong branch. Best move?

4 Practice for real

Your mission

Settle the theme debate

Merge dark-mode into main. It will conflict on theme.txt. Resolve it so the file says theme: dark, and finish the merge.

39cb02d Initial theme click to insert the hash into the terminal39cb02dInitial themedbde502 Switch to dark theme click to insert the hash into the terminaldbde502Switch to dark…f55b947 Try sepia theme click to insert the hash into the terminalf55b947Try sepia themeBranch "dark-mode" — a movable pointerdark-modeHEAD = where you are. It points at the current branch.HEAD →Branch "main" — a movable pointer (current branch: moves when you commit)main
Working directoryyour folder / editor
theme.txt
git add →← git restore
Staging areathe next snapshot (index)
theme.txt
git commit →← restore --staged
RepositoryHEAD → f55b947
theme.txt
terminal · simulated, no real repository is touched
Type commands below. `help` lists everything. Tab completes, ↑ recalls. Click a commit to paste its hash.
~/project (main) $