Blog
fix git merge conflictsgit merge conflictresolve merge conflict

How to Fix Git Merge Conflicts (and Git Messes) with AI

Step-by-step help to fix git merge conflicts, plus prompts to paste into an AI assistant, what to check, and how to undo common git mistakes safely.

Portrait of Luis Chavez-Mattos
Edited by Luis Chavez-Mattos, Director, Product··RSS

For Engineering

Extreme macro of two overlapping paper printouts with one small crimson highlighter stroke in sharp focus against soft grey blur.

How do you fix a git merge conflict?

To fix a git merge conflict, run git status to see which files conflict, open each one, and find the sections between <<<<<<<, =======, and >>>>>>>. Decide what the final code should be, delete the marker lines, save the file, run git add <file>, and finish with git commit (or git rebase --continue if you were rebasing). An AI assistant can explain each conflict and propose a resolution, but you still have to check the result and run the tests.

TL;DR

  • A merge conflict means two branches changed the same lines (or one deleted what the other edited), and git needs a human to choose. Nothing is broken.
  • Run git status first. It lists conflicted files under "Unmerged paths" and tells you the next command.
  • You can bail out safely at any point with git merge --abort or git rebase --abort, which returns you to where you started.
  • AI is good at explaining a conflict and drafting a merged version. Give it both sides plus what each change was meant to do, and it gets much better.
  • Never trust the AI's merge without checking it. Search for leftover markers, run the build and tests, and read the final diff.
  • Be careful with AI-suggested commands that destroy work: git reset --hard, git push --force, and git clean -fd. Understand them before you run them.
  • Check your company's policy before pasting proprietary code into any AI tool, and never paste secrets.

What does a merge conflict look like?

When git cannot combine changes on its own, it stops and writes both versions into the file. You will see something like this:

<<<<<<< HEAD
const timeout = 30;
=======
const timeout = 60;
>>>>>>> feature/longer-timeout

Read it in three parts:

  • Everything between <<<<<<< HEAD and ======= is your current branch's version (in a merge, the branch you are on).
  • Everything between ======= and >>>>>>> feature/longer-timeout is the incoming version from the other branch.
  • The correct answer is whatever the file should say once both intentions are respected. It might be one side, the other, or a mix.

Conflicts happen when you run git merge, git pull (which merges by default), git rebase, git cherry-pick, or git stash pop. The steps below work for all of them, with small differences noted.

How do you resolve a merge conflict step by step?

  1. Stop and read the message. Git prints something like CONFLICT (content): Merge conflict in src/billing.js and Automatic merge failed; fix conflicts and then commit the result. You are in the middle of an operation, not in a broken state.

  2. List the conflicted files. Run:

    git status
    

    Files under "Unmerged paths" (marked "both modified", "deleted by us", and so on) need your attention. Files already listed under "Changes to be committed" merged cleanly.

  3. Open one file and find the markers. Your editor probably highlights them and offers buttons such as "Accept Current", "Accept Incoming", or "Accept Both". VS Code and JetBrains IDEs both do this. To search from the terminal:

    git grep -n "<<<<<<<"
    
  4. Understand what each side was trying to do. This is the step people skip. Look at the history of each branch:

    git log --oneline --merge -- path/to/file
    

    The --merge option shows commits from both sides that touched the conflicted files. Reading the commit messages usually tells you the intent.

  5. Write the final version. Edit the section so it contains exactly the code you want. Delete all three marker lines (<<<<<<<, =======, >>>>>>>). If both changes matter, combine them by hand.

  6. Check for leftover markers.

    git grep -n -E "^(<<<<<<<|=======|>>>>>>>)"
    

    It should print nothing for your resolved files.

  7. Run the code. Build it, run the tests, start the app. A file with no markers can still be wrong.

  8. Mark the file as resolved.

    git add path/to/file
    

    Repeat steps 3 to 8 for each conflicted file.

  9. Finish the operation.

    • Merge: git commit (git pre-fills a merge message you can accept).
    • Rebase: git rebase --continue. You may hit more conflicts as each of your commits is replayed. Repeat the steps for each.
    • Cherry-pick: git cherry-pick --continue.
  10. Push. For a merge, a normal git push works. After a rebase of a branch you already pushed, you will need git push --force-with-lease, which is safer than --force because it refuses to overwrite commits you have not seen.

If you get lost, back out and start over:

git merge --abort
git rebase --abort
git cherry-pick --abort

Use the one that matches what you were doing. Your branch returns to its state before the operation began.

A note on "ours" and "theirs"

git checkout --ours file and git checkout --theirs file take a whole file from one side. In a merge, "ours" is your branch and "theirs" is the incoming one. In a rebase, it flips: "ours" is the branch you are rebasing onto, and "theirs" is your own commit being replayed. This trips up experienced people too. When in doubt, open the file and read it.

Make conflicts easier to read

Git can show the original version alongside both sides, which makes intent much clearer:

git config --global merge.conflictStyle diff3

Newer git versions also offer zdiff3, a tidier variant. With either, the conflict gains a middle section starting with ||||||| showing what the code looked like before either branch changed it. That "before" is the most useful thing to give an AI assistant.

How do you use AI to resolve a merge conflict?

AI works best as a patient colleague who reads the conflict with you. It is weaker if you only say "fix this" with a pile of markers. Give it the conflict, the surrounding code, and the intent of each change.

First, collect the context. Commit messages and pull request descriptions for both branches are enough:

git log --oneline --merge -- path/to/file

Then paste this prompt, filled in:

I'm resolving a git merge conflict. Help me understand it, then propose a resolution.

Language / framework: [e.g., TypeScript, React]
What my branch was trying to do: [one or two sentences, or paste the commit message]
What the other branch was trying to do: [one or two sentences]

Here is the conflicted section of the file, with 20 lines of surrounding code:

[paste the code including the <<<<<<<, =======, >>>>>>> markers]

Please:
1. Explain in plain English what each side changed and why they collide.
2. Tell me whether both changes can coexist or if one must win. If one must win, say which and what you'd need to know to be sure.
3. Give me the final merged code with all conflict markers removed.
4. List anything in the merged result I should test, and anything you assumed.
Do not invent functions or variables that aren't in the code I pasted. If you're missing context, ask me.

The last two lines matter. They discourage the assistant from filling gaps with made-up code and make it list its assumptions so you know what to verify.

Many editors and AI coding tools can read your repository directly, so you may not need to paste anything. The same principle applies: tell the tool what each branch intended.

What should you check in the AI's answer?

Treat the output as a draft from a colleague who has not seen your whole codebase.

  • Markers are gone. Run the git grep command from step 6.
  • Nothing was dropped. Compare the AI's version to both sides. Models sometimes silently keep one side and lose a small change from the other, such as an added parameter or a new import.
  • Nothing was invented. Check that every function, variable, and import it uses exists. Run the code or the compiler.
  • The tests pass. If the conflicted code has no tests, run the feature by hand.
  • The final diff makes sense. After resolving, run git diff --cached (or look at the merge commit) and read it like a code review.
  • Config and data files get extra care. Conflicts in lock files (package-lock.json, yarn.lock) are usually better regenerated by running the package manager than merged by hand or by AI. Conflicts in database migrations need a human who knows the order they must run in.

If the assistant says it is unsure, believe it. Ask a teammate who owns that code.

How do you fix other common git messes with AI?

Conflicts are one kind of git panic. These are the others interns and new grads hit most. For each, the pattern is the same: describe what you see, paste the output of git status and git log --oneline -10, and ask for an explanation before commands.

The messWhat it usually meansTypical fix
"I committed to main by mistake"Your commit is on the wrong branch, not yet pushedgit branch my-feature then git reset --hard origin/main (this moves main back; the commit survives on my-feature)
"I want to undo my last commit but keep my work"Commit made too earlygit reset --soft HEAD~1
"I pushed a bad commit and others have it"Shared history; do not rewrite itgit revert <commit-sha> creates a new commit that undoes it
"You are in 'detached HEAD' state"You checked out a commit, not a branchgit switch -c new-branch-name to keep your work
"My push was rejected (non-fast-forward)"The remote has commits you lackgit pull --rebase (or git pull), resolve conflicts, push again
"I lost commits after a reset or rebase"Commits are still in git's log of where HEAD has beengit reflog, find the commit, git branch rescue <sha>
"I edited the wrong file and want it back"Uncommitted changesgit restore path/to/file (this discards your edits, so be sure)

Using the reflog as your safety net

git reflog lists every place HEAD has pointed recently, including before a bad reset or rebase. If you think you lost work, run it, find the line from before things went wrong, and create a branch there:

git reflog
git branch rescue HEAD@{3}

Git keeps these entries for a while (90 days by default), so most "lost" commits are recoverable. Uncommitted changes that you discarded are generally not.

A prompt for any git mess

I'm stuck in git and I don't want to lose work. Don't give me commands yet.

Here is the output of `git status`:
[paste]

Here is the output of `git log --oneline --graph --all -15`:
[paste]

What I was trying to do: [plain English]
What happened / the error message: [paste exact text]

First, explain what state my repo is in. Then give me the safest way to get where I wanted to be, step by step. For every command, tell me whether it can permanently discard work and how to undo it if it goes wrong. If there's a safer way to check first (like creating a backup branch), start with that.

Asking for the undo path for every command is the most useful habit here. Before any risky step, make a backup branch: git branch backup-before-fix. It costs nothing and keeps the current state reachable.

If you committed a password or API key

Do not start with git. Revoke or rotate the key first, because anyone who cloned or fetched the repo may already have it, and deleting the commit does not change that. Then tell your security team, who will have a process. Cleaning history is a secondary step, and company policy often dictates how.

Where should you not trust AI with git?

AI assistants are fluent and confident, including when they are wrong. Be especially careful here:

  • Destructive commands. git reset --hard, git clean -fd, git checkout ., git push --force, and git branch -D can permanently discard work or overwrite teammates' commits. If an assistant suggests one, ask what exactly it will delete and whether a safer command exists.
  • Force-pushing shared branches. Never force-push main or a branch others use. If the AI says it is fine, it does not know your team's rules.
  • Business logic conflicts. If two people changed pricing rules, permissions, or calculations, the question is which behavior the business wants. The AI cannot know. Ask the authors.
  • Commands it cannot see the effect of. The assistant is guessing from the text you pasted. If your output is incomplete, so is its diagnosis.
  • Version differences. Some commands (git switch, git restore, zdiff3) need relatively recent git versions. Run git --version if a command errors.

A good rule: let AI explain and draft, and make yourself read each command and say what it does before you press Enter.

Is it safe to paste code into an AI tool at work?

It depends on your employer. Many large companies approve specific AI tools and ban others, and some restrict what code or data can be shared. Before you paste anything:

  • Check your company's AI usage policy or ask your manager or security team which tools are approved.
  • Never paste secrets: API keys, passwords, tokens, connection strings, or customer data. Replace them with placeholders like YOUR_API_KEY.
  • Paste only the conflicted section and enough context, not whole repositories, when you are using an unapproved or consumer tool.
  • Prefer the approved coding assistant built into your editor, which can read the repository without you copying it around.

If no tool is approved, the non-AI steps above still work, and a senior teammate is often the fastest resource anyway.

How do you avoid merge conflicts in the first place?

You cannot eliminate conflicts, but you can make them smaller and rarer.

  • Pull from the main branch often. Run git pull --rebase origin main (or merge main into your branch) daily. Small, frequent conflicts are easier than one huge one after two weeks.
  • Keep branches short-lived. Small pull requests touch fewer shared lines.
  • Talk before touching shared files. If you are about to reorganize a file three people are editing, say so in chat.
  • Do not mix formatting changes with logic changes. A commit that reformats a whole file collides with everyone.
  • Use a formatter and linter the whole team shares, so whitespace differences do not create fake conflicts.
  • Turn on git rerere (git config --global rerere.enabled true). It records how you resolved a conflict and reapplies the same resolution if the same conflict reappears, which is handy when you rebase a long branch repeatedly.

Could a small internal tool help your team with git questions?

New hires often ask the same git questions, and the answers live in scattered wiki pages and old chat threads. With Remy, you can describe an internal help app in plain English, such as a searchable "how we do git here" page built from your team's own branching rules and recovery steps, with cited answers and a form for logging new questions. Remy builds and deploys it as a full-stack app, with sign-in, a database, and document search included. You own the code, so it is software your team keeps rather than another subscription. It will not resolve conflicts for you, but it can keep your team's conventions in one place.

Frequently Asked Questions

5 questions

Can I undo a merge after I've committed it?

If you have not pushed, run git reset --hard ORIG_HEAD right after the merge, or git reset --hard HEAD~1 if the merge commit is the latest one. Both discard uncommitted work, so check git status first. If you already pushed and others have the commit, use git revert -m 1 <merge-commit-sha> to create a new commit that reverses it.

Why do I keep getting the same conflict again and again?

This usually happens when you rebase a long-lived branch repeatedly, so git replays the same conflicting commits each time. Merging the main branch into your branch once, instead of rebasing, can end the repetition. Enabling git rerere also helps, since it remembers your earlier resolutions and reapplies them.

Is it okay to just pick "Accept Incoming" or "Accept Current" in my editor?

Only when you are sure one side should entirely replace the other. Those buttons discard the other side's changes, which may include a bug fix or new feature someone else needs. Read both sides, or ask the author, before choosing.

Should AI resolve conflicts in lock files and generated files?

Usually no. For files like package-lock.json, take one side (or the main branch's version) and rerun your package manager to regenerate the file so it matches your actual dependencies. The same applies to other generated output: rebuild it rather than merging it by hand or asking an AI to.

What if the conflict is in a file I don't understand?

Find out who changed it with git log --oneline --merge -- path/to/file or git blame, and ask them. An AI can explain the syntax, but the people who made the changes know why they made them, and that is what a correct resolution depends on.

How we source, write, and correct our articles. Editorial standards

Related articles

The AI Brief, presented by Remy
The week in AI, and what it means for your work. Every Friday.