Resolving merge conflicts

Beginner · 5 min

Read conflict markers, choose or combine the two sides, and finish or abort a merge, including the mistake of committing the markers themselves.

Before thisMerging: fast-forward and three-way

Philomatics · 5 min

Resolving merge conflicts

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by PhilomaticsReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A conflict happens when both branches changed the same lines differently, so Git cannot choose and asks you to.
  • Git marks the file with <<<<<<<, ======= and >>>>>>> lines. Everything between the first two is your side, and between the last two is theirs.
  • To resolve, edit the file to the content you want, delete the marker lines, run git add on it, then git commit.
  • git merge --abort returns you to the state before the merge, so starting over is always safe.
  • Git does not check that you removed the markers. git diff --check finds leftovers, and the project's own tests find the rest.

When Git has to ask

A three-way merge combines two branches by applying each side’s changes to their common ancestor. If the changes touch different parts of a file, Git merges them silently. If both sides changed the same lines in different ways, Git can’t know which you meant. It stops and hands the decision to you. That’s a merge conflict. It isn’t an error, and nothing is broken. It’s Git asking a question only you can answer.

I made one on purpose. One branch, style, changed how show prints the note number, from 1. milk to 1) milk. On main, I changed the same line to [1] milk.

$ git merge style
Auto-merging notes.py
CONFLICT (content): Merge conflict in notes.py
Automatic merge failed; fix conflicts and then commit the result.

Where you are mid-merge

$ 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:   notes.py

The merge is paused. Git has merged everything it could and left the conflicted file for you. In short format it’s UU notes.py. To list conflicted files only, git diff --name-only --diff-filter=U printed notes.py. And git log --oneline --merge shows just the commits on each side that touch the conflict, which is handy for working out why each side changed the line.

Reading the markers

Open the file:

def show():
    with open(NOTES) as f:
        for number, line in enumerate(f, start=1):
<<<<<<< HEAD
            print(f"[{number}] {line}", end="")
=======
            print(f"{number}) {line}", end="")
>>>>>>> style
  • Between <<<<<<< HEAD and ======= is your side, the branch you’re on.
  • Between ======= and >>>>>>> style is their side, the branch you’re merging.

Everything outside the markers merged cleanly. You have three choices: keep yours, keep theirs, or write something that combines both. Often the answer is a mix, which is why Git doesn’t guess.

Resolving

  1. Edit the file until it holds exactly what you want. Delete the three marker lines too, since they are not comments, just text Git inserted. I kept my version:
        for number, line in enumerate(f, start=1):
            print(f"[{number}] {line}", end="")
  1. Mark it resolved with git add notes.py. That is how you tell Git you’re done with this file. Status now says “All conflicts fixed but you are still merging.”

  2. Conclude the merge with a commit. git commit --no-edit accepts the prepared message:

$ git commit --no-edit
[main 149e1cb] Merge branch 'style'

The graph shows the two branches rejoining, as in the last lesson. Git also refuses to commit while a file is unresolved. When I tried, it said “error: Committing is not possible because you have unmerged files.”

Backing out

If the conflict is more than you want to deal with right now, you can undo the whole merge:

$ git merge --abort
$ git status -s

Empty status: you’re back where you were before git merge, your own uncommitted work apart. I did this, then re-ran the merge to resolve it. Aborting is safe, and the right instinct when a conflict looks unfamiliar.

Seeing the original line

Sometimes you can’t tell who is right without seeing what the line said before either of you touched it. Git can include it. I set merge.conflictStyle to zdiff3 for one merge:

<<<<<<< HEAD
            print(f"[{number}] {line}", end="")
||||||| 3702e9b
            print(f"{number}. {line}", end="")
=======
            print(f"{number}) {line}", end="")
>>>>>>> style

The middle section, after |||||||, is the common ancestor. Now it’s clear that both sides replaced the same original line. I set it with git config merge.conflictStyle zdiff3 and removed it afterwards; turn it on globally if you like it.

Taking one whole side

To keep one side’s version of an entire file and skip the editing, use --ours or --theirs, then git add:

$ git restore --theirs notes.py
$ grep -n "number" notes.py
14:        for number, line in enumerate(f, start=1):
15:            print(f"{number}) {line}", end="")

--theirs gave the style branch’s version, and git checkout --ours notes.py gave mine. Both replace the whole file, not only the conflicting hunk, so any non-conflicting changes from the other side are lost from that file. Use them for files you know you want one side of entirely.

The mistake Git won’t stop

Here is the dangerous part. Git checks that you’ve run git add, not what is in the file. I merged again, ran git add notes.py without editing, and committed:

$ git diff --cached --check
notes.py:15: leftover conflict marker
notes.py:17: leftover conflict marker
notes.py:19: leftover conflict marker
$ git commit -qm "oops markers"
$ python3 notes.py show
    <<<<<<< HEAD
IndentationError: expected an indented block

The commit went through, with markers inside a Python file, and the program stopped working. Before you git add, run git diff --check, which catches leftover markers (I ran it on the staged version with --cached). And run your tests or the program before concluding a merge.

What the video covers

Philomatics’ “Never fear merge conflicts again - git merge/pull tutorial” is about five minutes and was published around 2024. I had no transcript. The title says it covers merge conflicts and mentions pull, which gets its own lesson in the remotes group. It is in English. The Modern SDE’s lecture on conflict tools is linked for --ours, --theirs and the combined diff.

Where this fits

Merging joins two lines of history with a knot. There’s another way to combine work, which replays your commits on top of the other branch so there is no knot. Next: rebasing a branch.

Check yourself

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

    Your version, the lines from the branch you are merging into. The lines between ======= and >>>>>>> come from the branch being merged.

  2. 02You have fixed the file. What tells Git the conflict is resolved?

    Running git add on the file. Then git commit finishes the merge. Git does not look at the file's content to decide.

  3. 03You started a merge and do not know how to continue. How do you get back to where you were?

    git merge --abort restores the branch and working directory to the state before the merge began.

  4. 04How could a conflict marker end up committed, and how do you catch it?

    git add accepts the file with markers still inside it. git diff --check (or --cached) reports leftover conflict markers, and running the code or tests would fail.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark