A git merge conflict happens when two branches changed the same lines of the same file and Git cannot decide which version wins, so it stops and hands the decision to you. It marks the disputed spot in the file with three lines of markers, pauses the merge, and waits. To resolve it you open the file, pick the correct combination of the two versions, delete the markers, stage the file with git add, and finish with git commit (for a merge) or git rebase --continue (for a rebase). If you would rather walk away, git merge --abort puts everything back exactly as it was. This guide covers the whole loop end to end, plus whole-file shortcuts, merge tools, and how to stop the same conflict from coming back.
What you’ll accomplish
- Understand what a 3-way merge is and why the conflict markers appear.
- Read a conflicted file and tell the two sides apart with confidence.
- Resolve conflicts by editing, by whole-file selection, or with a merge tool.
- Finish a conflicted merge or rebase correctly, and verify no markers were left behind.
- Abort cleanly when the merge is going nowhere, and set up
rerereso a recurring conflict resolves itself.
Prerequisites
- Git 2.x on your machine (
git --version). Conflict handling is core Git; nothing extra to install. - A repository with at least two branches whose histories have diverged. The step-by-step section shows you how to create one on purpose.
- Comfort with the basics:
git status,git add, andgit commit. If you can stage and commit a change, you have everything you need here. - A text editor you are comfortable with. Optionally a merge tool (VS Code, Meld, KDiff3, vimdiff) if you prefer a visual three-pane view.
How it works
When you merge branch B into branch A, Git does not compare the two branch tips directly. It finds their common ancestor — the last commit both branches share — and performs a 3-way merge: for every changed line it looks at three versions, the base (ancestor), ours (your current branch, HEAD), and theirs (the branch you are merging in).
The rule is simple. If a line changed on only one side relative to the base, Git takes that change automatically — there is nothing to decide. If a line changed on both sides, and the two changes differ, Git cannot know which one you meant. It writes both versions into the file, surrounded by markers, and stops. Those markers are the conflict:
<<<<<<< HEAD
region = 'eu'
=======
region = 'ca'
>>>>>>> theirs
Everything between <<<<<<< HEAD and ======= is your side (ours). Everything between ======= and >>>>>>> theirs is the incoming side (theirs). Your job is to replace that entire block — markers included — with the single correct version, which might be one side, the other, or a hand-written blend of both.
Step-by-step
1. Reproduce a conflict on purpose. The fastest way to get comfortable is to create one in a throwaway repo. Two branches edit the same line, then you merge.
mkdir conflict-demo && cd conflict-demo
git init -q
printf 'region = "us"\n' > config.txt
git add config.txt && git commit -qm "initial config"
git switch -c feature
printf 'region = "eu"\n' > config.txt
git commit -qam "feature: set region to eu"
git switch main
printf 'region = "ca"\n' > config.txt
git commit -qam "main: set region to ca"
2. Trigger the merge and read the failure. Merge feature into main. Git stops with a clear message.
git merge feature
Auto-merging config.txt
CONFLICT (content): Merge conflict in config.txt
Automatic merge failed; fix conflicts and then commit the result.
3. Ask Git what is conflicted. git status is the map. The conflicted files are listed under “Unmerged paths” as both modified.
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: config.txt
no changes added to commit (use "git add" and/or "git commit -a")
4. Open the file and read the markers. The conflicted region is wrapped in the three markers described above.
<<<<<<< HEAD
region = "ca"
=======
region = "eu"
>>>>>>> feature
The HEAD side is what is on main (ours). The feature side is what is coming in (theirs). Decide what the resolved line should actually be.
5. Edit to the intended result, then remove every marker. Replace the whole block — from <<<<<<< through >>>>>>> — with the final content. Say you want the incoming value:
region = "eu"
There must be no <<<<<<<, =======, or >>>>>>> left anywhere in the file. Leftover markers are the single most common way to ship a broken merge.
6. Stage the resolved file. git add is how you tell Git “this file is settled”. It also clears the file from the unmerged list.
git add config.txt
git status
On branch main
All conflicts fixed but you are still merging.
(use "git commit" to conclude the merge)
Changes to be committed:
modified: config.txt
7. Conclude the merge with a commit. Because a merge in progress already has a prepared message, a bare git commit finishes it.
git commit --no-edit
[main 4f2a9c1] Merge branch 'feature'
Whole-file shortcut: take one side entirely. When you know an entire file should be one version — not a line-by-line blend — skip the editor. --ours keeps your current branch’s version; --theirs takes the incoming one. Then stage as usual.
git checkout --theirs config.txt # take the incoming version wholesale
git add config.txt
Use a merge tool for messy conflicts. For large or tangled conflicts, a three-pane view is easier than raw markers. git mergetool launches your configured tool on each conflicted file and stages the result when you save and quit.
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait $MERGED'
git mergetool
See the base with diff3 style. By default the markers show only ours and theirs. The diff3 (or zdiff3) style adds the original base text between them, which often makes the right resolution obvious. Turn it on globally:
git config --global merge.conflictstyle zdiff3
<<<<<<< HEAD
region = "ca"
||||||| merged common ancestor
region = "us"
=======
region = "eu"
>>>>>>> feature
Abort and start over. If the merge is going sideways, back out completely. This restores the working tree and index to exactly the state before you ran git merge.
git merge --abort
git status # "nothing to commit, working tree clean"
Validate it works
Confirm the tree is clean and the merge landed. After committing, git status should report a clean tree, and the log should show your merge commit.
git status
git log --oneline -1
nothing to commit, working tree clean
4f2a9c1 Merge branch 'feature'
Prove no conflict markers survived. git diff --check scans for leftover markers (and whitespace errors) across the whole tree. Silence means clean. As a belt-and-suspenders check, grep the tree.
git diff --check
git grep -nE '^(<<<<<<<|=======|>>>>>>>)' || echo "no markers found"
no markers found
List the merge commits. git log --merges shows only merges, a quick way to confirm the merge is recorded in history.
git log --merges --oneline -1
4f2a9c1 Merge branch 'feature'
Troubleshooting
You committed the conflict markers. If markers made it into a commit, the code is likely broken. Fix the file properly, then amend if the merge commit is the tip and unpushed (git commit --amend), or make a follow-up commit if it is already shared. Add git diff --check to your pre-commit hook or CI so this cannot happen again.
You picked the wrong side. Resolved to --theirs when you wanted --ours? If you have not committed yet, re-run the resolution: git checkout --ours <file> (or edit by hand) and git add again. If you already committed the merge and nothing was pushed, git reset --hard HEAD~1 drops the merge commit so you can redo it. Do not hard-reset shared history.
The conflict is during a rebase, not a merge — and git commit does nothing useful. A rebase replays commits one at a time, so each conflict belongs to a specific commit being replayed. Resolve and git add as usual, but finish with git rebase --continue, not git commit. Use git rebase --abort to bail, and git rebase --skip to drop the commit currently being applied.
git add config.txt
git rebase --continue
A binary file conflicts. Git cannot merge inside binaries (images, compiled assets, archives), so there are no in-file markers to edit — you must choose one whole file. Pick a side explicitly and stage it.
git checkout --theirs assets/logo.png
git add assets/logo.png
The same conflict keeps coming back. Repeated rebases or long-lived branches can force you to resolve the identical conflict again and again. Enable rerere (reuse recorded resolution): Git remembers how you resolved a given conflict and replays that resolution automatically next time it sees the same one.
git config --global rerere.enabled true
When this is the wrong approach
Resolving conflicts is a skill, but the best conflict is the one that never happens:
- Reaching for
--theirsor--ourson a whole file to make the error go away. Whole-file selection is right only when you truly want one entire version. Used to dodge a real merge, it silently throws away the other side’s work. When lines genuinely need to coexist, edit them together. - Letting branches drift for weeks. The longer a branch lives away from
main, the more the two diverge and the worse the conflicts. Smaller, shorter-lived branches and frequentgit pull --rebase(or merges frommain) keep conflicts small and rare. Prevention beats resolution. - Fighting a conflict you do not understand. If you cannot tell which side is correct, stop and talk to whoever wrote the other change before you guess. A wrong resolution that compiles is worse than an honest question — it hides a bug in a merge commit no one will think to inspect.
For related history-navigation skills, see git bisect: find the commit that introduced a bug, and to undo a bad merge or recover work after a mistaken reset, see recover a deleted git branch or lost commit with git reflog. The authoritative references are the official git merge documentation and the Basic Branching and Merging chapter of the Git book.
If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.