Sub topic 5
Reading conflict markers
This chapter assumes you can create branches, commit, and run git merge. You should also know why a merge can stop with a conflict, which the previous chapter covered. Here you learn to read what Git writes into a conflicted file.
What Git writes into the file
When both branches changed the same lines, Git can't choose between them. It stops the merge and edits the file in your working tree. It puts both versions in the file, wrapped in marker lines. The markers are ordinary text. Git does not treat them specially until you stage the file.
Suppose main set timeout = 30 and feature set timeout = 60 on the same line. After git merge feature, config.txt looks like this:
<<<<<<< HEAD
timeout = 30
=======
timeout = 60
>>>>>>> featureThe three markers
| Marker | Meaning |
|---|---|
| `<<<<<<< HEAD` | Start of the conflict. The lines that follow come from the branch you are on. |
| `=======` | Divider between the two versions. |
| `>>>>>>> feature` | End of the conflict. The lines above it, back to the divider, come from the branch you are merging. |
In a merge, HEAD is the branch you ran git merge from. The name after >>>>>>> is the branch you merged in. In this example, "ours" is timeout = 30 and "theirs" is timeout = 60.
Several conflicts in one file
A file can hold many conflict blocks. Each block is independent, and the text between blocks merged cleanly. You do not need to touch that text. Work through the blocks one at a time and leave non-conflicting regions alone.
What the result should be
The final file must contain the content you want, and no marker lines. You can keep one side, keep the other, or combine them. For example, you might decide the right value is timeout = 45. In that case, delete all five lines of the block and write that single line.
timeout = 45Common mistakes
- Deleting the content but leaving a marker line behind. The file still looks conflicted to anyone reading it.
- Keeping both versions by removing only the markers. This often produces duplicate or contradictory lines, such as two
timeoutsettings. - Assuming
HEADalways means your own work. That holds for a merge. During a rebase the sides swap, as a later chapter explains.
Before you stage a file, search it for leftover markers:
grep -nE '^(<<<<<<<|=======|>>>>>>>)' config.txtIf the command prints nothing, no marker lines remain. The command only checks for markers. It does not check that the code is correct, so build and test as well.
Exercise
In a merge of feature into main, started from main, a block reads <<<<<<< HEAD, retries = 3, =======, retries = 5, >>>>>>> feature. Which branch made the retries = 5 change, and what must the final file contain?
Show answer
The feature branch made it, because it sits between ======= and >>>>>>> feature. The final file must contain your chosen content (retries = 3, retries = 5, or another value) and none of the five marker-block lines.