Dev tools

Git Merge Conflict: How to Resolve It Step by Step

A git merge conflict happens when two branches change the same lines. How to read the markers, resolve them in the terminal or VS Code, and avoid them.

By OmniAI Editorial · Nov 22, 2026 · 10 min read

A git merge conflict happens when two branches change the same lines of the same file, or when one branch edits a file the other deletes, and Git can't decide which version to keep. Git stops the merge, marks the conflicting parts of each file with <<<<<<<, ======= and >>>>>>>, and waits for you. To resolve it, open each conflicted file, keep the code you want, delete the markers, stage the file with git add, and finish with git commit or git merge --continue. If you'd rather start over, git merge --abort puts everything back the way it was before the merge.

This guide explains why conflicts happen, how to read the markers, how to resolve a merge conflict step by step on the command line and in VS Code, the useful commands Git gives you, and how to avoid most conflicts in the first place.

Why a Git Merge Conflict Happens

Most merges never conflict. Git's documentation explains that when you merge, changes that don't overlap are combined automatically: if you changed one part of a file and the other branch changed a different part, both changes go in. A conflict only appears when both sides changed the same area of a file, because Git won't guess which version is right.

The common types of conflict:

Conflict typeWhat happenedWhat Git shows
Content conflictBoth branches changed the same linesConflict markers inside the file
Modify/delete conflictOne branch edited a file the other deleted (often after a rename)CONFLICT (modify/delete); the file is kept with the edited version
Add/add conflictBoth branches created a file with the same path but different contentConflict markers in the whole file
Rename conflictBoth branches renamed the same file differentlyCONFLICT (rename/rename)
Binary file conflictBoth branches changed an image or other binary fileGit can't mark it; you pick one version

Conflicts also show up during git rebase, git cherry-pick, git stash pop and git pull (which runs a merge or rebase). The way you resolve them is the same.

How to Read Conflict Markers

Here is a git merge conflict example. When a merge stops with a conflict, git status lists the files under "Unmerged paths". Inside each file, the conflicting part looks like this:

<<<<<<< HEAD
const timeout = 30;
=======
const timeout = 60;
>>>>>>> feature/retry
  • Everything between <<<<<<< HEAD and ======= is your current branch's version (the branch you were on when you ran git merge).
  • Everything between ======= and >>>>>>> feature/retry is the incoming branch's version.
  • Lines outside the markers were merged cleanly.

That's the whole git merge conflict current vs incoming idea: "current" is the branch you're on, "incoming" is the branch being merged in. Editors use the same words in their buttons.

You can make conflicts easier to understand by also showing the original version from before either branch changed it. Git calls this the diff3 style; the zdiff3 variant shows the same information more compactly. Turn it on once:

git config --global merge.conflictStyle zdiff3

With it enabled, a third section marked ||||||| shows the common ancestor's version, which tells you what each side actually changed.

A light document with ink lines divided by three dotted ink lines, with one orange line in the upper block.
Conflict markers split the file into your version and the incoming version.

How to Resolve a Merge Conflict Step by Step

Here's how to resolve merge conflicts on the command line, step by step.

1. See what's conflicted.

git status

2. Open each conflicted file and decide. For each block, keep your version, keep theirs, or write a combination. Delete all three marker lines (<<<<<<<, =======, >>>>>>>). In the example above, you might decide the correct value is 60 and keep only that line.

3. Check that no markers are left.

git diff --check

4. Test the result. Run the code or tests. A file without markers can still be wrong if the two changes don't fit together.

5. Stage each resolved file.

git add src/config.js

6. Finish the merge.

git merge --continue

git merge --continue checks that a merge is in progress and then commits; a plain git commit does the same. For a rebase, use git rebase --continue instead, and repeat for each commit that conflicts.

Keeping one side for a whole file

If you know one branch's version of a file is right, you don't have to edit it by hand. While the file is still unmerged, take your side or theirs:

git checkout --ours path/to/file
git checkout --theirs path/to/file

Then git add the file. Be careful during a rebase: there, "ours" means the branch you're rebasing onto and "theirs" means your own commits, the opposite of what most people expect.

Useful Commands for a Git Merge Conflict

Git's documentation lists several tools for working through a conflict:

CommandWhat it shows or does
git statusLists unmerged files
git diffA three-way diff of the conflicted areas, against both sides
git diff AUTO_MERGEWhat you've changed so far while resolving
git log --merge -p <path>The commits from each side that touched the file
git show :1:<file>The common ancestor's version of the file
git show :2:<file>Your current branch's (HEAD) version
git show :3:<file>The incoming branch's version
git mergetoolOpens a configured graphical merge tool for each file
git merge --abortCancels the merge and restores the pre-merge state
git rerereRecords how you resolved a conflict and reuses it if the same conflict appears again

The :1:, :2: and :3: versions are especially useful when a conflict block is confusing: you can open the three versions side by side and see exactly what each branch did.

Git Merge Abort: Starting Over Safely

If a merge goes wrong or you're not ready to deal with it, git merge --abort cancels the merge and puts your branch and files back the way they were before you started. It's the safest exit. Git's documentation describes it as resetting the index and cleaning up the working tree changes the merge made.

Commit or stash your own uncommitted changes before merging. If you start a merge with uncommitted work, aborting may not be able to restore it cleanly. For a rebase, the equivalent is git rebase --abort.

How to Resolve a Merge Conflict in VS Code

Many people resolve conflicts in an editor instead of by hand, and Visual Studio Code merge conflicts are a good example of how editors help. In a merge conflict, VS Code highlights each block and adds inline actions above it:

  • Accept Current Change keeps your branch's version.
  • Accept Incoming Change keeps the other branch's version.
  • Accept Both Changes keeps both, one after the other.
  • Compare Changes shows them side by side.

VS Code also has a three-way merge editor that shows the incoming version, your version and the result in separate panes, which makes larger conflicts easier to handle. When each file looks right, save it and stage it in the Source Control panel, then commit. Other editors and Git clients (JetBrains IDEs, GitHub Desktop, GitKraken and others) offer similar tools, and GitHub can resolve simple conflicts directly in a pull request.

The editor buttons do exactly what manual editing does; they just save typing. You still need to read the result and run your tests.

Resolving Conflicts in Pull Requests

On hosting platforms like GitHub and GitLab, a pull request that conflicts with the target branch can't be merged until the conflict is fixed. You have two options:

  1. Merge the target branch into yours (git merge main on your branch), resolve the conflicts, and push.
  2. Rebase your branch onto the target (git rebase main), resolve conflicts commit by commit, and force-push with git push --force-with-lease.

Rebasing gives a cleaner history; merging is simpler and doesn't rewrite commits other people may have pulled. Follow your team's convention. If you're comparing hosting platforms, our guide to GitHub alternatives covers how the main ones handle pull requests.

How to Avoid Merge Conflicts

You can't avoid every conflict, but these habits remove most of them:

  • Merge or rebase often. Pull the main branch into your work daily, so differences stay small.
  • Keep branches short-lived. A branch that lives for weeks collects conflicts; one that lives a day or two rarely does.
  • Make small, focused commits and pull requests. Smaller changes overlap less.
  • Agree on formatting. Run the same formatter (Prettier, gofmt, Black and others) for everyone, so whitespace and style changes don't collide.
  • Avoid mass reformatting in feature branches. Reformat the whole codebase in one separate commit.
  • Talk before big refactors. If you're moving or renaming files many people touch, warn the team.
  • Split huge files. Files that everyone edits (routes, configs, translation files) conflict constantly; splitting them helps.

These habits matter even more when several AI coding agents work in parallel. Giving each agent its own branch and folder with a git worktree keeps their changes apart until you merge them one by one.

A thick ink line with grey dots and four short ink arcs leaving and rejoining it, each topped by a small light circle except one orange circle.
Short branches merged often keep conflicts small.

Common Mistakes When Resolving Conflicts

  • Leaving markers in the code. Run git diff --check before committing.
  • Keeping both sides blindly. "Accept Both" can produce duplicated or broken logic.
  • Not running tests. A clean merge in text can still break behavior.
  • Resolving under time pressure without context. Use git log --merge to see why each side changed the code, or ask the author.
  • Using `git push --force` instead of `--force-with-lease` after a rebase, which can overwrite someone else's work.

Code review helps catch bad resolutions too; our guide to AI code review covers how review tools flag risky changes.

FAQ

What causes a git merge conflict?

A merge conflict happens when two branches change the same lines of a file, or one branch modifies a file that the other deletes or renames. Git merges non-overlapping changes automatically but stops when it can't tell which version is correct.

How do I resolve a merge conflict in Git?

Open each conflicted file listed by git status, keep the correct code, delete the conflict markers, then run git add on the file. When all files are resolved, run git merge --continue or git commit to finish the merge.

How do I cancel a merge with conflicts?

Run git merge --abort. It stops the merge and restores your branch and files to the state before the merge started. During a rebase, use git rebase --abort instead.

What do HEAD and the other branch name mean in conflict markers?

The section after <<<<<<< HEAD is the version on your current branch. The section before >>>>>>> branch-name is the version from the branch you're merging in. During a rebase these sides are reversed, because Git replays your commits on top of the other branch.

How do I resolve merge conflicts in VS Code?

Open the conflicted file; VS Code shows options above each conflict to accept the current change, the incoming change or both, and a three-way merge editor for larger conflicts. Save the file, stage it in Source Control, and commit.

All articles

Read next

Language

EnglishEnglish · en TürkçeTurkish · tr DeutschGerman · de العربيةArabic · ar

Account

Enter your email. We’ll send you a sign-in link and a 6-digit code. No password needed.

Sign in

Theme

Language

Show our email address

To keep spam out of our inbox, please confirm your own email first. We will send you a 6-digit code.

We send codes only to these providers: gmail.com, googlemail.com, outlook.com, hotmail.com, live.com, msn.com, proton.me, protonmail.com, pm.me, icloud.com, me.com, mac.com, yahoo.com, aol.com, gmx.de, gmx.net, web.de, t-online.de, yandex.com, omniai.blog