Skip to main content

Git · 5 common causes

Merge conflict

Updated

Both branches changed the same lines, so Git stopped and left both versions in the file for you to decide between.

The exact message

$ git merge feature/checkout
Auto-merging src/app.js
CONFLICT (content): Merge conflict in src/app.js
Automatic merge failed; fix conflicts and then commit the result.

<<<<<<< HEAD
const timeout = 5000;
=======
const timeout = 30_000;
>>>>>>> feature/checkout

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is Merge conflict.

How to fix it

Cause 1 of 5

Both sides edited the same lines.

git status lists the conflicted files. In each one, find the <<<<<<< HEAD, ======= and >>>>>>> markers and edit that region into the code you actually want — often a combination rather than one side wholesale. Delete all three marker lines, git add the file, then git commit (or git rebase --continue).

Cause 2 of 5

One side deleted a file the other side edited.

Git reports it as 'deleted by us' or 'deleted by them' and leaves the surviving version in the working tree either way — the conflict message names which one. To keep the file, git add <file>; to accept the deletion, git rm <file>. Do not reach for git checkout --ours/--theirs here: only one side has a version of this file, so the other name fails with 'path does not have their version'.

Cause 3 of 5

A generated file such as a lockfile conflicts.

Do not hand-merge a lockfile. Take either side and regenerate it: git checkout --theirs package-lock.json then npm install, and commit the result. A hand-edited lockfile produces an install nobody else can reproduce.

Cause 4 of 5

The merge is more tangled than expected and you want out.

git merge --abort — or git rebase --abort during a rebase — restores the exact state you started from. It is safe and specifically designed for this, so use it rather than pushing through a merge you do not understand.

Cause 5 of 5

You cannot tell what each side intended.

git log --merge -p <file> shows only the commits from both sides that touched the conflicted file. git checkout --conflict=diff3 <file> rewrites the markers to include the common ancestor as well, which usually makes the intent of each change obvious.

Stopping it coming back

Merge or rebase from the main branch often so each conflict is small, and keep pull requests narrow — a conflict in one file is a two-minute job, a conflict across forty is an afternoon.

Merge conflict — common questions

Which side is ours and which is theirs?
In a merge, ours is the branch you are on and theirs is the branch being merged in. In a rebase the labels swap, because your commits are being replayed onto the other branch — which is why checking with diff3 markers beats trusting the label.
How do I redo a conflict resolution I got wrong?
Before committing, git checkout --merge <file> restores the conflict markers so you can start that file again. After committing, git revert the merge commit or reset to the pre-merge commit found in git reflog.
Can Git resolve simple conflicts for me?
It already resolves everything it safely can — you only see what it could not. Enabling rerere (git config --global rerere.enabled true) makes Git remember how you resolved a conflict and reapply it if the same one recurs, which pays off during long rebases.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor