Merge Conflict After AI Edits — Resolution Guide

Your branch + AI edits conflict with main. Resolve without losing AI improvements.

You let Claude Code, Cursor, or Codex run for half an hour, six files of edits land on disk, and the moment you git pull --rebase origin main the terminal fills with CONFLICT (content): Merge conflict in src/.... The painful part isn’t the conflict itself — it’s that the agent re-ordered imports, normalized whitespace, and changed real logic in the same files, so you can’t tell which hunks are noise and which are semantic conflicts.

This guide gives one path: freeze the AI changes as a comparable commit, let git absorb the pure-format conflicts automatically, then hand-resolve only the real semantic ones.

Common causes

Ordered by how often each pattern shows up in agent-driven merge conflicts.

1. AI edited files that main also moved

The most common case. You forked an hour ago; in that window a teammate pushed three commits touching the same src/server/auth.ts. The agent had no idea remote moved and rewrote the file from your local state.

CONFLICT (content): Merge conflict in src/server/auth.ts
Auto-merging src/server/auth.ts

How to spot it: git log origin/main --since="2 hours ago" -- <file> shows commits on the same file from someone else.

2. Imports auto-organized into a different order

Cursor and Copilot often run eslint --fix or Prettier on save and reorder imports. If the agent’s grouping differs from the team’s config, the entire import block gets rewritten and you get a giant-looking conflict that’s pure ordering noise.

<<<<<<< HEAD
import { z } from "zod";
import { db } from "@/lib/db";
import type { User } from "@/types";
=======
import type { User } from "@/types";
import { db } from "@/lib/db";
import { z } from "zod";
>>>>>>> ai-branch

How to spot it: Both sides of the hunk contain the same set of imports, only the order or blank lines differ.

3. Whitespace and line endings rewritten

Aider and Windsurf often have the model regenerate whole functions. The model sometimes flips tabs to spaces, CRLF to LF, or restyles semicolons. git diff looks like the whole file changed; git diff -w is almost empty.

How to spot it: git diff --stat shows large insertions/deletions but git diff -w --stat drops to single digits.

4. AI moved code across files; main moved the same code elsewhere

The agent decided to move formatDate from utils/format.ts into lib/date.ts. At the same time main moved the same function to shared/format.ts. The rebase can’t reconcile two deletes plus two adds and raises an add/add conflict.

CONFLICT (add/add): Merge conflict in lib/date.ts

How to spot it: git status shows both both added and deleted by us / them entries.

5. AI fixed a bug; main fixed the same bug differently

Both sides are correct, but the implementations diverge. You asked the agent to fix a race condition with a mutex; a teammate landed an atomic-based fix. Tests pass either way, but the code conflicts.

How to spot it: Each side of the conflict passes the test suite in isolation — the implementations are simply different.

6. Lockfile or generated-file conflicts

package-lock.json, pnpm-lock.yaml, migrations/*.sql, schema.prisma — these always conflict in hundreds or thousands of lines. There’s a dedicated guide: AI lockfile conflicts.

How to spot it: The conflicting path is a lockfile, migration, or anything under a generated/ directory.

Shortest path to fix

In order. Every step is reversible — worst case, git rebase --abort returns you to Step 1.

Step 1: Freeze the AI edits into a single commit

If the agent’s changes are still in your working tree, commit them so they become a comparable, resettable object.

git add -A
git commit -m "WIP: AI edits before rebase"
git rev-parse HEAD  # save this SHA — you'll cherry-pick or diff against it

Then create a safety branch:

git branch backup/ai-edits-$(date +%Y%m%d-%H%M)

If the rebase goes sideways, git checkout backup/... brings you back in one command.

Step 2: Let git absorb the format-only conflicts with -X

Many “conflicts” are import ordering, whitespace, or line endings. Bias git toward one side before resolving:

# prefer remote (main) formatting but keep local (AI) logic
git pull --rebase -X theirs origin main

# or the opposite — keep local in full, take remote only for non-conflicting hunks
git pull --rebase -X ours origin main

-X theirs/ours only chooses at the hunk level — it does not drop AI’s net-new code. Conflict count usually drops 50-70%.

To absorb whitespace only:

git rebase --rebase-merges -X ignore-all-space origin/main

Step 3: Bucket the remaining conflicts by type

git status --short | grep "^UU"  # files still in conflict

Handle each bucket differently:

TypeResolution
Pure import / formattinggit checkout --theirs <file> then run your local formatter
Lockfilegit checkout --theirs <file> then npm install to regenerate
Real logic conflictHand-edit using the three-way diff below

Step 4: Three-way diff the real logic conflicts

For files with semantic conflicts, open a three-way view:

git mergetool --tool=vimdiff  # or vscode / meld

Or pull each version manually:

git show :1:src/server/auth.ts > /tmp/base.ts    # common ancestor
git show :2:src/server/auth.ts > /tmp/ours.ts    # main side
git show :3:src/server/auth.ts > /tmp/theirs.ts  # AI side
diff -u /tmp/base.ts /tmp/ours.ts    # what main actually changed
diff -u /tmp/base.ts /tmp/theirs.ts  # what AI actually changed

Only after you see “what main changed” and “what AI changed” as two independent diffs can you decide how to merge — instead of blindly picking a side.

Step 5: Run the full check before continuing the rebase

git add <resolved files>
npm test && npm run lint && npm run build
git rebase --continue

If you want out at any point:

git rebase --abort  # back to the commit from Step 1

Prevention

  • Run git pull --rebase origin main before letting the agent start; shrink the divergence window
  • Put rules in CLAUDE.md / .cursorrules / AGENTS.md: “read the file first, don’t reorder imports, don’t reformat whitespace”
  • Keep AI commits small and frequent — one task per commit, so a conflict costs at most one commit of work
  • Add a repo-root .editorconfig plus a pre-commit formatter hook so AI output matches team style automatically
  • Tag lockfiles and generated files with merge=ours or linguist-generated so rebase skips manual diffing
  • For large refactors, have the agent write a plan first; approve the file list before letting it touch anything

Tags: #AI coding #Debug #Troubleshooting