# Resolving Git Merge Conflicts — Step by Step, with diff3, ours and theirs

Source: https://skills101.in/learn/git/git-resolving-merge-conflicts — Skills101 free tutorial (Git & GitHub). Updated 2026-11-18.

A **merge conflict** is not an error and nothing is broken. It is Git saying: "Two people changed the same lines in different ways, and I will not guess which one is right." This lesson walks through a real conflict from start to finish. Every output shown was produced by actually running the commands (your commit ids will differ).

## 1. How Git merges: the three-way merge

When you merge a branch, Git looks at **three** versions of each file:

- **base** — the common ancestor, the last commit both branches share
- **ours** — the version on the branch you are on (`HEAD`)
- **theirs** — the version on the branch you are merging in

For each region of the file, Git applies one simple rule:

![Table of four cases with base 499: only theirs changed to 599 gives 599; only ours changed to 449 gives 449; both changed to 549 gives 549; ours 449 and theirs 599 gives a CONFLICT](https://skills101.in/learn-img/git/three-way.svg)

| ours vs base | theirs vs base | Result |
|---|---|---|
| unchanged | changed | take theirs |
| changed | unchanged | take ours |
| both made the **same** change | | take it |
| changed | changed **differently** | **conflict** — you decide |

This is why most merges succeed automatically: usually the two branches touched different parts of the code.

## 2. Creating a conflict on purpose

A shop sells a course at ₹499. On `main`, the team gives a festive discount (₹449). Meanwhile, on `feature/pricing`, someone raises the price to ₹599.

```bash
git switch -c feature/pricing
# change 499 to 599 in price.js
git commit -am "Raise price to 599"
git switch main
# change 499 to 449 in price.js
git commit -am "Festive discount: 449"
git merge feature/pricing
```

```text
Auto-merging price.js
CONFLICT (content): Merge conflict in price.js
Automatic merge failed; fix conflicts and then commit the result.
```

## 3. Reading the situation

First, always run `git status`:

```text
On branch main
You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   price.js

no changes added to commit (use "git add" and/or "git commit -a")
```

"both modified" tells you which files need attention. Git has already written into `price.js` everything it could merge, plus **conflict markers** around the part it could not:

```text
<<<<<<< HEAD
const price = 449;
=======
const price = 599;
>>>>>>> feature/pricing
const gst = 0.18;
```

| Marker | Meaning |
|---|---|
| `<<<<<<< HEAD` | start of **ours** (the branch you are on) |
| `=======` | divider |
| `>>>>>>> feature/pricing` | end of **theirs** (the branch being merged) |

Notice `const gst = 0.18;` has no markers: neither side changed it, so Git kept it as it was.

## 4. See the base too: the diff3 style

The default markers show the two results but not **what each side started from**. Turn on the `diff3` style (newer Git versions also offer `zdiff3`, which shows the same thing more compactly):

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

The same conflict now looks like this:

![The diff3 conflict shows ours (const price = 449), then the base after the ||||||| line (const price = 499), then theirs (const price = 599); the base shows main gave a discount and the branch raised the price](https://skills101.in/learn-img/git/diff3-markers.svg)

```text
<<<<<<< HEAD
const price = 449;
||||||| ddca92c
const price = 499;
=======
const price = 599;
>>>>>>> feature/pricing
const gst = 0.18;
```

Now the story is clear: one side **lowered** the price for a festival and the other **raised** the base price. You might decide the correct answer is the new base price with the festive discount applied — perhaps ₹549. That is a **business decision**, which is exactly why Git asks a human.

## 5. Resolving it

1. Edit the file to the final, correct code and **delete every marker line**.
2. Run the tests or the app to check it still works.
3. Stage the file — this tells Git "this conflict is resolved".
4. Commit to finish the merge.

```bash
# price.js now contains:  const price = 549;  and  const gst = 0.18;
git add price.js
git status
```

```text
On branch main
All conflicts fixed but you are still merging.
  (use "git commit" to conclude merge)

Changes to be committed:
	modified:   price.js
```

```bash
git commit --no-edit        # keeps Git's default message "Merge branch 'feature/pricing'"
git log --oneline --graph
```

```text
*   8c5321b Merge branch 'feature/pricing'
|\  
| * 6a74bae Raise price to 599
* | 4407a59 Festive discount: 449
|/  
* ddca92c Add price
```

The graph shows both lines of work and the merge commit that joins them. Notice `ddca92c` is the same base commit named in the diff3 markers.

## 6. Escape hatches and shortcuts

| Command | Use it when |
|---|---|
| `git merge --abort` | you want to cancel the whole merge and go back to before it started |
| `git checkout --ours file` | you want your branch's version of this file (still `git add` it afterwards) |
| `git checkout --theirs file` | you want the incoming branch's version of this file |
| `git diff` | during a conflict, shows the conflicted parts |
| `git log --merge` | lists the commits on both sides that touched the conflicted files |

VS Code and other editors show buttons — **Accept Current**, **Accept Incoming**, **Accept Both**, **Compare** — over each conflict. They only edit the text for you; you still need to check the result, then `git add` and commit.

## 7. Conflicts during a rebase

Rebase replays your commits one by one, so a conflict can appear **in the middle**:

```text
error: could not apply 4407a59... Festive discount: 449
hint: Resolve all conflicts manually, mark them as resolved with
hint: "git add/rm <conflicted_files>", then run "git rebase --continue".
hint: You can instead skip this commit: run "git rebase --skip".
hint: To abort and get back to the state before "git rebase", run "git rebase --abort".
```

The fix is the same (edit, remove markers, `git add`), but you finish with `git rebase --continue` instead of `git commit`.

**Watch out:** during a rebase, "ours" and "theirs" are **swapped**. Git is replaying your commit on top of the other branch, so `HEAD` ("ours") is the branch you are rebasing **onto**, and "theirs" is **your** commit being replayed. Read the content, not just the labels.

## 8. Fewer and smaller conflicts

- Pull or rebase onto `main` **often** — small differences mean small conflicts.
- Keep branches short and pull requests small.
- Do not mix formatting changes (re-indenting a whole file) with real changes.
- Agree on a code formatter (for example Prettier) so whitespace never conflicts.
- For generated files like `package-lock.json`, resolve `package.json` first, then regenerate the lock file with `npm install` rather than editing it by hand.

## Common mistakes

- Committing a file that still contains `<<<<<<<` or `>>>>>>>` lines. Search for them before you commit.
- Clicking "Accept Current" on everything without reading — you silently throw away a teammate's work.
- Panicking and deleting the repository. `git merge --abort` (or `git rebase --abort`) safely takes you back.
- Forgetting that ours and theirs swap meaning during a rebase.

## Quick recap

- Git compares **base**, **ours** and **theirs**; a conflict happens only when both sides changed the same lines differently.
- `git status` lists conflicted files; markers `<<<<<<<`, `=======`, `>>>>>>>` surround the conflict.
- `merge.conflictStyle diff3` also shows the base, which explains each side's change.
- Resolve: edit, delete markers, test, `git add`, then `git commit` (or `git rebase --continue`).
- `git merge --abort` cancels; `--ours` / `--theirs` pick a whole side.


Practice quiz and free certificate: https://skills101.in/learn/git/git-resolving-merge-conflicts