Reading Git Merge Conflict Markers in a Notes or Code File
A block fenced by <<<<<<<, =======, and >>>>>>> is where Git could not combine two edits to the same lines. You choose what the text should say, delete the three marker lines, and tell Git you are done.
If you keep notes or code in a repository, these blocks can land in any text file. Here's what each line means, which side is which, and how to finish the merge. If you are new to the setup, start with version-controlling Markdown notes with Git.
Quick answer
When both sides changed the same area, Git cannot pick one side over the other, so it leaves what both sides did to that area in the file. A conflict looks like this:
If you have questions, please
<<<<<<< HEAD
open an issue
=======
ask your question in IRC.
>>>>>>> branch-aTo resolve it, decide whether to keep your text, the other text, or something new built from both. Then delete the markers, as GitHub's guide to resolving conflicts puts it: "Delete the conflict markers <<<<<<<, =======, >>>>>>> and make the changes you want in the final merge."
The three marker lines
The git-merge reference says the area where a pair of conflicting changes happened is marked with the markers `<<<<<<<`, `=======`, and `>>>>>>>`. Each one has a job:
<<<<<<<opens the block. Your side starts on the next line.=======divides the two sides.>>>>>>>closes the block. The label after it names the other side.
The same page says the part before the ======= is typically your side, and the part afterwards is typically their side. Everything outside the markers merged cleanly. Leave it alone.
The optional base section
The default format does not show what the original text said. If you want that, set merge.conflictStyle to diff3 or zdiff3. The diff3 style adds a fourth marker, |||||||, followed by the original text:
<<<<<<< HEAD
open an issue
||||||| base
ask a question
=======
ask your question in IRC.
>>>>>>> branch-aWith the base text in view, it's easier to see what each side actually changed. According to the docs, zdiff3 is similar to diff3 but removes matching lines on the two sides from the conflict region when those lines appear near the beginning or end of it, so the block you read is shorter.
Which side is yours
The Pro Git book walks through a merge of a branch named iss53 while master was checked out. It explains that the version in `HEAD`, the branch you had checked out when you ran the merge, is the top part of the block, everything above the `=======`, while the other branch is the bottom part.
So in a plain git merge:
- The text between
<<<<<<< HEADand=======is from the branch you are on. - The text between
=======and>>>>>>> branch-ais from the branch you merged in.
The docs say "typically", and that word matters. Don't assume the top is always your own work in every Git operation. Read the labels on the marker lines. If you want to compare the two versions line by line first, how to read a unified diff explains the + and - notation.
To see which files still have conflicts, run git status. The book notes that Git has paused the merge rather than creating a merge commit, and the status output lists the unmerged files.
Resolving a conflict step by step
- Find each block. Open the file and search for
<<<<<<<. A file can hold several blocks. - Decide what the text should say. Keep your side, keep the other side, or write a new version. GitHub's example keeps both ideas in one sentence: "If you have questions, please open an issue or ask in our IRC channel if it's more urgent."
- Delete every marker line. Remove
<<<<<<<,=======, and>>>>>>>, plus the|||||||line and the base text if your style shows them. The Pro Git book describes the finished result as having "the<<<<<<<,=======, and>>>>>>>lines completely removed." - Mark the file resolved. Run
git addon each file. Staging the file marks it as resolved. - Finish the merge. Use
git commitorgit merge --continue. The docs say the latter checks that a merge is in progress before callinggit commit.
When resolving prose, a word-level diff for comparing drafts can show exactly which words each side changed.
If you prefer a visual tool, git mergetool launches one and walks you through the conflicts. After you exit it, Git asks whether the merge was successful and, if you say yes, stages the file for you.
Common problems and fixes
Markers are left in the saved file
Git won't check your prose or code for leftover marker lines when you stage a file, so a forgotten ======= can be committed as ordinary text. Before you run git add, search the file once more for <<<<<<<, =======, and >>>>>>>. In a note this leaves odd lines in the middle of a paragraph; in code or config it can break the file.
You want to start over
If the merge went sideways, you don't have to untangle it. The docs say git merge --abort will abort the current conflict resolution process and try to reconstruct the pre-merge state. Run it, then try the merge again when you are ready.
The merge is still marked as in progress
If git status says you have fixed all conflicts but are still merging, the book's advice is to run git commit to conclude the merge. The default commit message lists the files that had conflicts, and you can edit it to explain how you resolved them.
Doing this with Carets
Carets is a fast, native notes and code editor for iPhone, iPad, and Mac that works on plain Markdown, text, and code files. A conflicted file is still just a text file, so it opens like any other, and syntax highlighting keeps code and config files readable while you edit out the marker blocks.
Here's the honest scope: Carets edits the files. It doesn't run Git commands. Run the merge, git add, and git commit in your usual Git tool, and use Carets for the part in the middle, reading each block and rewriting it. Because your notes stay plain text, they stay portable and easy to merge the next time. Organize them into files, projects, and tags, and open the same files on Mac, iPhone, or iPad. Get Carets on the App Store for iPhone, iPad, and Mac.
Frequently Asked Questions
What do <<<<<<<, =======, and >>>>>>> mean in a file?
They are the conflict markers Git writes when both sides changed the same area. The text between <<<<<<< and ======= is typically your side. The text between ======= and >>>>>>> is typically the other side.
What is the ||||||| line in my conflict?
It appears when merge.conflictStyle is set to diff3 or zdiff3. The line is followed by the original text both sides started from, so you can see what each side changed.
Can I undo a merge that produced conflicts?
Yes. git merge --abort aborts the conflict resolution process and tries to reconstruct the pre-merge state.