Common causes
- Two branches edited the same lines of a file
- One branch deleted or renamed a file that the other modified
- A long-lived feature branch drifted far from main
- git pull merged remote commits into local changes to the same file
- Generated files such as lock files or built assets were committed and changed on both sides
- Different line endings or reformatting touched every line of a file
How to fix it
- List the conflicted files. Run git status. Files under 'Unmerged paths' marked 'both modified' or 'deleted by them' are the ones to fix.
- Edit the conflict markers. Open each file and find the <<<<<<<, ======= and >>>>>>> blocks. Keep the correct code, combine both sides if needed, and delete all three marker lines.
- Take one side for whole files. For a file where one version should win, run git checkout --ours path or git checkout --theirs path. During a rebase, ours and theirs are swapped: ours is the branch you are rebasing onto.
- Stage and finish. Run git add on each resolved file (or git rm for a deletion you accept), then git commit or git merge --continue. During a rebase use git rebase --continue.
- Abort if needed. git merge --abort (or git rebase --abort) returns the branch to its state before the merge. Use it when you want to start over or merge later.
- Regenerate lock files. For package-lock.json or composer.lock conflicts, take one side, then run npm install or composer update --lock to regenerate it, and commit the result.
Terminal
git status # see unmerged paths
# edit files, remove <<<<<<< ======= >>>>>>> markers
git add src/app.js
git commit # or: git merge --continue
# start over
git merge --abort How to stop it happening again
- Merge or rebase main into feature branches often so they stay close
- Keep pull requests small and short-lived
- Add a .gitattributes with consistent line endings and agree on a formatter
- Do not commit generated or build files; regenerate them instead