World map

 

BM: Baca dalam Bahasa Melayu

World 3: Parallel Universes

Read conflict markers

You will learn: why a merge stops, and how to read Git's conflict markers.

The problem

git merge cake prints: Auto-merging index.html. CONFLICT (content): Merge conflict in index.html. Automatic merge failed. Terminal output: $ git merge cake Auto-merging index.html CONFLICT (content): Merge conflict in index.html Automatic merge failed; fix conflicts and then commit the result.

You merge cake into main, and Git stops. CONFLICT! Did you break your diary?

Watch

main and cake each have a new commit. The merge stops: a red Conflict badge with a warning sign appears under HEAD.
6d3a9b1a8feb63270a76e0c66136013a957

Nothing broke. Both branches wrote a different day 2 on the same line. That’s a conflict: Two changes hit the same spot. You pick what stays.. Git can’t guess, so it asks you.

Watch

index.html now holds both day 2 lines, between three marker lines. Terminal output: $ git merge cake Auto-merging index.html CONFLICT (content): Merge conflict in index.html Automatic merge failed; fix conflicts and then commit the result. $ cat index.html <h1>My Diary</h1> <p>Day 1: I learned Git.</p> <<<<<<< HEAD <p>Day 2: I learned branches.</p> ======= <p>Day 2: I baked a cake.</p> >>>>>>> cake

Git wrote both versions into the file. conflict markers: The lines <<<<<<<, ======= and >>>>>>> that Git puts around a clash. fence them in. Your line sits on top. Theirs sits below.

Try it

Find the fences.

Tap the three marker lines.

~/diary $ git merge cake~/diary $ cat index.html

0 of 3 found

Predict

Which day 2 line came from your branch, main?

Pick one answer.

Check

git status says: You have unmerged paths. Under Unmerged paths: both modified: index.html. Terminal output: $ git merge cake Auto-merging index.html CONFLICT (content): Merge conflict in index.html Automatic merge failed; fix conflicts and then commit the result. $ git status On branch main You have unmerged paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort the merge) Unmerged paths: (use "git add <file>..." to mark resolution) both modified: index.html no changes added to commit (use "git add" and/or "git commit -a")

Lost mid-merge? Run git status. It lists the files to fix as both modified. It even names your next steps.

You did it!

You can now read conflict markers without panic.

Go deeper (optional)

During a conflict, git diff shows both sides at once. Want to see the line before either change? Run git config --global merge.conflictStyle diff3. Then a part starting with ||||||| shows the old base line.

git diff during the conflict: a combined diff with both day 2 lines and the markers. Terminal output: $ git merge cake Auto-merging index.html CONFLICT (content): Merge conflict in index.html Automatic merge failed; fix conflicts and then commit the result. $ git diff diff --cc index.html index efaaee0,f4c5ff0..0000000 --- a/index.html +++ b/index.html @@@ -1,3 -1,3 +1,7 @@@ <h1>My Diary</h1> <p>Day 1: I learned Git.</p> ++<<<<<<< HEAD +<p>Day 2: I learned branches.</p> ++======= + <p>Day 2: I baked a cake.</p> ++>>>>>>> cake