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:
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
- 1/2 cup soy sauce <<<<<<< HEAD - 1/3 cup coconut vinegar ======= - 1/2 cup cane vinegar >>>>>>> more-vinegar - 1 head garlic, crushed
<<<<<<< HEADstarts 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:
git add adobo.mdmarks the conflict settled;git commitfinishes 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.
- Merge
more-vinegarinto main and see the conflict - Make the line say
- 1/2 cup coconut vinegar, with no markers left - Mark it settled and finish the merge
- 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
4d924ecHEAD →main Use coconut vinegar in the adobo588df20more-vinegar Use more vinegar in the adobo67e435dCover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add 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.
- Start the merge, then back out with
git merge --abort - 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
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
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.
- Git documentation: git-merge, How conflicts are presented: the markers, and how to resolve
- Git documentation: git-restore, --ours and --theirs: checking out one side for unmerged paths
- Pro Git: Basic Merge Conflicts: resolving and marking resolved with git add
- Pro Git: Advanced Merging: aborting a merge