Sub topic 5
Reading conflict markers
This chapter assumes you can commit, create and switch branches, and run git merge. It also assumes you can open a file in a text editor. You will learn what each marker line in a conflicted file means and how to tell which side of a conflict came from which branch. You will also learn what an extra base section tells you and how to spot markers you forgot to remove.
Where markers come from
When a merge cannot combine two changes on its own, Git does two things. It writes both versions into the file in your working tree, separated by marker lines. It also records the file as unmerged in the index and stops the merge, leaving you to finish it.
The markers are plain text. Git does not read them back later. If you commit a file that still contains them, Git commits them like any other text. Resolving a conflict therefore means editing the file until the markers are gone, then staging it.
To follow along, create the conflict from the earlier chapters:
git init -b main demo && cd demo
printf 'timeout = 10\n' > config.txt
git add config.txt && git commit -m "Add config"
git switch -c feature
printf 'timeout = 60\n' > config.txt
git commit -am "Raise timeout to 60"
git switch main
printf 'timeout = 30\n' > config.txt
git commit -am "Raise timeout to 30"
git merge featureGit reports CONFLICT (content): Merge conflict in config.txt. If you open config.txt, you will see this:
<<<<<<< HEAD
timeout = 30
=======
timeout = 60
>>>>>>> featureThe three marker lines
A conflict region is bounded by three kinds of line. Each is a run of seven identical characters at the start of a line.
| Marker | Meaning |
|---|---|
| `<<<<<<< HEAD` | Start of the conflict. The text after it labels the first side. |
| `=======` | Divider between the two sides. |
| `>>>>>>> feature` | End of the conflict. The text after it labels the second side. |
Everything between <<<<<<< and ======= is the version from the branch you are on. Everything between ======= and >>>>>>> is the version from the branch you are merging in. In the example, you were on main, so timeout = 30 is yours and timeout = 60 came from feature.
The labels name the source of each side. HEAD is the commit you currently have checked out. The second label is whatever you passed to git merge. If you merged by commit ID or a remote-tracking branch such as origin/feature, the label shows that name instead.
Only the lines between the markers are in dispute. Lines outside any marker block merged cleanly, and Git has already applied them.
A conflict can hold more than one line
A region can contain many lines on either side, and one side can be empty. An empty side means that branch deleted those lines while the other branch changed them. Here is an example in a settings file:
retries = 3
<<<<<<< HEAD
timeout = 30
log_level = debug
=======
>>>>>>> featureIn this case HEAD kept two lines, and feature removed them. The ======= line is directly followed by the closing marker, so the second side is empty.
A file can also contain several separate conflict regions. Git marks each region on its own, and you must resolve every one. Work through the file from top to bottom and treat each region independently.
Adding the base with diff3 and zdiff3
By default, markers show only two sides. That is often not enough to decide what is right. If you see timeout = 30 and timeout = 60, you cannot tell whether one side changed a value and the other left it alone, or whether both changed it. The common ancestor answers that question.
Set the conflict style so Git includes the ancestor's text:
git config --global merge.conflictStyle zdiff3zdiff3 needs Git 2.35 or later. On older versions, use diff3. If you already have a conflict open, you can redo the checkout with the new style using git checkout --conflict=zdiff3 -- config.txt. Be aware that this discards any edits you have made to that file.
The same conflict now looks like this:
<<<<<<< HEAD
timeout = 30
||||||| a1b2c3d
timeout = 10
=======
timeout = 60
>>>>>>> featureA new section sits between ||||||| and =======. It is the text from the merge base, the commit both branches started from. The label after ||||||| identifies that base. It may be a commit ID, or the words merged common ancestors when Git had to build a virtual base from several candidates.
With three versions in view, you can read the conflict as a pair of changes. The base said 10. One side moved it to 30, and the other moved it to 60. Both sides changed the same line to different values. Only a person can decide which value is correct, or whether a different one is better.
The difference between the two styles matters on larger conflicts. diff3 shows each side in full, including lines both sides share. zdiff3 moves lines that are identical on both sides to outside the conflict block, so the region shrinks to the lines that differ. That makes zdiff3 easier to read, which is why it is the better choice when your Git supports it.
Reading a conflict in practice
A reliable routine for each region:
- Read the two sides and the base if you have one. Work out what each branch was trying to do, not only what text it wrote.
- Decide what the result should be. It may be one side, the other, a combination, or something new.
- Replace the whole block, including all marker lines, with that result.
- Re-read the surrounding lines. A clean merge elsewhere in the file can still depend on the lines you just changed.
Suppose a function call changed on both sides:
<<<<<<< HEAD
connect(host, timeout=30)
||||||| a1b2c3d
connect(host)
=======
connect(host, retries=5)
>>>>>>> featureBoth sides added a keyword argument to the same call. The correct result keeps both:
connect(host, timeout=30, retries=5)Neither side's text appears unchanged in the result. Choosing one side would have silently dropped the other branch's work.
Which side is "ours"
Git's own wording calls the first side "ours" and the second side "theirs". In a merge, "ours" is the branch you are on and "theirs" is the branch you are bringing in. The labels on the marker lines follow that order.
This meaning reverses during a rebase. A rebase replays your commits on top of another branch. The branch you are rebasing onto counts as "ours" and the commit being replayed counts as "theirs". Markers read the same way, but the sides hold the opposite content from what you might expect. The rebase chapter covers this in detail. For now, remember that the top section is not always your own work.
Markers you can miss
Markers are easy to leave behind, especially in large files. Two checks catch most mistakes.
First, search for the marker lines. This pattern matches them only at the start of a line:
grep -nE '^(<<<<<<<|=======|>>>>>>>)' config.txtSecond, ask Git to check for unresolved markers across your changes:
git diff --checkgit diff --check reports leftover conflict markers and whitespace errors. Run it before you stage a file.
Two edge cases deserve care:
- Files that contain
=======legitimately. Some formats use a line of equals signs, such as reStructuredText or Markdown headings. A match on=======alone does not mean a conflict. Look for the matching<<<<<<<and>>>>>>>lines. - Marker length. Seven characters is the default. A project can change it with the
conflict-marker-sizeattribute in.gitattributes, usually to avoid confusion with files that already contain such lines. If you see longer markers, that setting is the likely reason.
Exercise
You are on main and run git merge feature. A file contains a block with an opening marker labelled HEAD, the line "color = blue", a ||||||| section containing "color = red", a ======= divider, the line "color = green", and a closing marker labelled feature. Which branch changed the value to green, what was the original value, and what does the ||||||| section tell you that a two-sided block would not?
Show answer
The feature branch changed it to green, because the section after ======= is the merged-in branch. The original value was red, from the merge base. A two-sided block shows only blue and green. The base section shows that both branches changed the same original line to different values, so neither side is simply untouched, and you must decide on the final value yourself.
Exercise
You resolve a conflict in config.txt by editing it, run git add config.txt, and then notice a line starting with ======= is still in the file. Which commands would have warned you before staging, and why did git add not warn you?
Show answer
Running git diff --check, or grep -nE '^(<<<<<<<|=======|>>>>>>>)' config.txt, would have found the leftover marker. git add does not inspect file contents for markers. It only records the file as resolved in the index, so the marker would be committed as ordinary text.