Resolving Git Merge Conflicts Without Breaking History

A conflict means two branches changed the same lines. Git marks them inline — understand the markers, pick the right side deliberately, and finish the merge cleanly.

What you'll see

Root causes

Both branches edited the same lines

Git cannot decide which change wins. The markers show HEAD (your side), then the incoming branch (theirs).

One side deleted a file the other modified

modify/delete conflicts appear in git status as 'deleted by them' / 'deleted by us' — you must explicitly choose keep or remove with git add or git rm.

Fix it

  1. List conflicted files and the marker regions
    git status --short && git diff --name-only --diff-filter=U
  2. Edit each file: keep/combine the correct code, delete ALL marker lines (<<<<<<<, =======, >>>>>>>)
    grep -rn '<<<<<<<\|>>>>>>>' . --include='*' | head  # find leftovers before staging
  3. Take one side wholesale where appropriate
    git checkout --ours path/file   # or --theirs, then git add path/file
  4. Stage resolutions and complete the merge
    git add -A && git commit   # or for a rebase: git rebase --continue
  5. Undo a bad merge before committing
    git merge --abort   # restores pre-merge state exactly

Field note

IDE conflict tools (VS Code's merge editor) are safer than hand-editing for large files. git rerere (reuse recorded resolution) remembers your conflict resolutions across repeated rebases: git config rerere.enabled true.

Common questions

What do the three conflict markers mean?

<<<<<<< HEAD starts your current branch's version, ======= separates the two, and >>>>>>> branch names the incoming version. Delete the markers and keep whichever code is correct — often a mix of both.

I committed files with conflict markers still in them. Now what?

Fix the file content, then amend the merge commit: git add file && git commit --amend. If already pushed, add a follow-up commit fixing the markers.

Ship it right the first time

Our most-documented failures, packaged as ready-to-ship starter kits: Docker, Kubernetes, and Terraform.

Browse the template store →

One-time. Yours to modify. Instant download from the NinjaOps template store.