Merge Conflicts

A merge conflict happens when Git cannot automatically combine changes.

This usually means two branches changed the same part of the same file in different ways.

Simple example

On main, the code says:

const timeout = 3000

On your branch, you changed it to:

const timeout = 5000

Another developer changed it to:

const timeout = 10000

Git cannot know which value is correct.

So it asks a human to decide.

Conflict markers

Git may write this into the file:

<<<<<<< HEAD
const timeout = 10000
=======
const timeout = 5000
>>>>>>> feature-branch

Meaning:

Example final result:

const timeout = 5000

Then remove the conflict markers.

Conflict resolution flow

  1. Run the merge or rebase.
  2. Git reports conflicted files.
  3. Open each file and choose the correct final code.
  4. Remove conflict markers.
  5. Run tests if possible.
  6. Stage resolved files.
  7. Continue.

For merge:

git add conflicted-file.js
git commit

For rebase:

git add conflicted-file.js
git rebase --continue

How to reduce conflicts

You cannot avoid conflicts forever, but you can reduce them:

Common mistake

Do not blindly accept "current" or "incoming" changes.

The correct answer may be a combination of both.

Read the code and understand what each side was trying to do.

Interview answer

A merge conflict occurs when Git cannot automatically combine changes, usually because two branches edited the same lines differently. To resolve it, I inspect the conflict markers, choose or combine the correct code, remove the markers, test the result, stage the file, and complete the merge or rebase.