Branches · lesson 4 of 7
Commands and ideas in this lesson
A<<<<<<< ======= >>>>>>>conflict markers
Bgit add, git commitmark it settled and finish
Cgit merge --abortback to before the merge
D--ours, --theirstake one side's whole file

Merge conflicts

Last updated

Ana and Joaquin both changed the vinegar in the adobo, differently. When the branches meet, Git can't choose for them, so it stops and asks. In this lesson you'll read the conflict markers, settle the conflict by hand, finish the merge, and back out of one when you'd rather not.

By the end of this lesson you can

  • Explain why a conflict happens
  • Read conflict markers and settle a conflict by editing the file
  • Finish a merge with git add and git commit, or abandon it with git merge --abort
  • Take one side for a whole file with git restore --ours or --theirs

Why conflicts happen

On his branch, Joaquin changed 1/3 cup vinegar to 1/2 cup cane vinegar. On main, Ana changed the same line to 1/3 cup coconut vinegar. Both are changes from the merge base, to the same line. Git has no way to know which is right, or whether both are partly right, so it doesn't guess.

It merges everything it can, then stops with the merge unfinished, writes both versions into the file, and tells you:

Terminal
git merge more-vinegar
Auto-merging adobo.md
CONFLICT (content): Merge conflict in adobo.md
Automatic merge failed; fix conflicts and then commit the result.

A conflict isn't an error, and nothing is broken. It's Git asking a person to decide. git status lists the file under "Unmerged paths" as both modified.

Reading the markers

adobo.md, during the merge
- 1/2 cup soy sauce
<<<<<<< HEAD
- 1/3 cup coconut vinegar
=======
- 1/2 cup cane vinegar
>>>>>>> more-vinegar
- 1 head garlic, crushed
  • <<<<<<< HEAD starts the conflict. Below it is your side: the branch you're on (HEAD).
  • ======= divides the two sides.
  • Below it, down to >>>>>>> more-vinegar, is their side: the branch you're merging in.

To settle it, edit the file so it says what it should say: one side, the other, or a mix of both (Lola Remy says: half a cup, coconut). Delete all three marker lines. Then:

  1. git add adobo.md marks the conflict settled;
  2. git commit finishes the merge, making the merge commit.

Settle the vinegar

Merge Joaquin's branch, settle the conflict as Lola Remy wants (1/2 cup coconut vinegar), and finish the merge. When you open the file with edit adobo.md, the sandbox shows each conflict with buttons to keep one side or both, as many editors do; you can also just edit the text.

Git sandbox
  1. Merge more-vinegar into main and see the conflict
  2. Make the line say - 1/2 cup coconut vinegar, with no markers left
  3. Mark it settled and finish the merge
  4. Check the commit has Lola Remy’s line

Working folder

no changes

Staging area

nothing staged

Repository

11 commits on main

You are in ~/kusina on main. Joaquin’s branch more-vinegar changes the same line of adobo.md as your last commit.

Commit graph

  1. 4d924ec HEAD →main Use coconut vinegar in the adobo
  2. 588df20 more-vinegar Use more vinegar in the adobo
  3. 67e435d Cover the leche flan with foil
  4. ba04ca2 Add lumpiang shanghai recipe
  5. 98a5edc Add a script that builds the index page
  6. d078487 Make the sinigang steps clearer
  7. 145dc1e Add pancit bihon recipe
  8. 52d1f4d Say how much salt goes in the adobo
  9. 1c17fad Add a README
  10. 9c53888 Add leche flan recipe
  11. 1f359a2 Add sinigang recipe
  12. 601c654 Add adobo recipe

In a real terminal, git commit with no -m during a merge opens your editor with the message Merge branch 'more-vinegar' ready, so you only save and close. The sandbox needs -m, or git commit --no-edit to take Git's message as it is.

Backing out, and taking one side

Back out. If a conflict is more than you want to deal with now, git merge --abort puts everything back as it was before git merge. git merge --abortCareful: changes files or history, but Git can usually get it back: it throws away any settling you've done so far in this merge.

Take one side for a whole file. During a conflict, git restore --ours adobo.md puts your side's version of the file in place, and git restore --theirs adobo.md theirs. Then git add and git commit as usual. (Older instructions say git checkout --ours.)

Use a merge tool. Editors such as VS Code show conflicts with "Accept current", "Accept incoming" and "Accept both" buttons above each one, like the sandbox's. "Current" is your side, "incoming" theirs.

Git sandbox
  1. Start the merge, then back out with git merge --abort
  2. Merge again, and this time take Joaquin’s whole file with --theirs, then finish

Working folder

no changes

Staging area

nothing staged

Repository

11 commits on main

You are in ~/kusina on main. Joaquin’s branch more-vinegar changes the same line of adobo.md as your last commit.

Spot the trap: committing the markers

Spot the trapAna

Ana settles one conflict in adobo.md, runs git add . and git commit. She didn’t notice a second conflict lower down in the same file.

What happens if…

Check what you learned

When does Git stop a merge with a conflict?
Between <<<<<<< HEAD and ======= is…
You’ve edited the file and removed the markers. What tells Git the conflict is settled?

Sources

Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.

All sources for Simple Git