---
title: "How do I resolve a merge conflict in Git?"
description: "Git stops a merge when both branches changed the same lines. Learn how to read the conflict markers, choose the right code, and finish or abort the merge safely."
url: https://www.danny.engineering/answer/git/how-do-i-resolve-a-merge-conflict-in-git
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["Git"]
---

# How do I resolve a merge conflict in Git?

## The short answer


Git could not combine two changes to the same lines, so it has paused the merge and written both versions into the file. Open each conflicted file, edit it so it contains the code you actually want, remove the conflict markers, then stage the file and finish the merge.


```bash
git status                 # lists files under "Unmerged paths"
# edit each file and remove the markers
git add src/app.js
git commit                 # or: git merge --continue
```


If it all goes wrong, `git merge --abort` puts everything back to how it was before you started.


## Why it happens


Git merges line by line. When one branch changed some lines and the other branch left them alone, Git takes the change automatically. When both branches changed the same lines, or one branch edited a file the other deleted, Git has no way to know which version is right. It stops and asks you.


Inside the file, the conflict looks like this:


```diff
<<<<<<< HEAD
const timeout = 5000;
=======
const timeout = 10000;
>>>>>>> feature/retry
```


The part between `<<<<<<<` and `=======` is your current branch (HEAD). The part between `=======` and `>>>>>>>` is the branch you are merging in. The same thing happens during `git rebase`, `git cherry-pick` and `git stash pop`, though during a rebase the meaning of "ours" and "theirs" is swapped, because Git is replaying your commits on top of the other branch.


## How to fix it


### Edit the file by hand


This is the most reliable approach. For each conflict block, decide what the final code should be. That might be one side, the other side, or a mix of both. Delete all three marker lines, save, and check that the code still builds and the tests pass.


```bash
git diff --name-only --diff-filter=U   # list files that still have conflicts
git add src/config.js
git merge --continue
```


Search the file for `<<<<<<<` before staging. Git will happily commit a file that still contains markers.


### Take one side for a whole file


When you know one version of a file is correct, for example a lockfile you will regenerate anyway, you can take it wholesale:


```bash
git checkout --ours package-lock.json     # keep your branch's version
git checkout --theirs package-lock.json   # keep the incoming version
git add package-lock.json
```


For lockfiles it is usually better still to take either side and then run your package manager's install command, so the lockfile matches the merged `package.json`.


### Use a merge tool


Your editor probably has a built-in conflict view. VS Code, JetBrains IDEs and most Git GUIs show the two sides with buttons to accept either or both. You can also launch a configured tool from the terminal:


```bash
git mergetool
```


### Show the original too


The default markers only show the two new versions, which makes it hard to see what each side actually changed. Setting the conflict style to `zdiff3` adds a third section with the common ancestor:


```bash
git config --global merge.conflictStyle zdiff3
```


Now you can see the original line and work out the intent of each change, which makes the right answer much easier to spot.


### Start again


If you get muddled, abandon the merge and try again from a clean state:


```bash
git merge --abort      # or: git rebase --abort
```


## Common pitfalls

- Committing a file that still contains conflict markers. Search for them, or let your linter or test suite catch the syntax error before you push.
- Keeping both sides by default. Two versions of the same function or two imports of the same thing often compile but behave wrongly.
- Forgetting that "ours" and "theirs" are reversed during a rebase.
- Resolving a lockfile by hand. Regenerate it instead.
- Starting a merge with uncommitted changes. Commit or stash first so you are only dealing with one set of changes.
- Not running the code afterwards. A merge can be free of conflicts and still be broken, so build and test before you push.

## Related reading

- [Git documentation: git merge, How conflicts are presented](https://git-scm.com/docs/git-merge#_how_conflicts_are_presented)
- [Pro Git: Basic merge conflicts](https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging)
