Skip to main content

What is a merge conflict?

A conflict during git merge is a situation where Git cannot automatically combine changes because the same parts of the code were changed in two branches.

In simple terms: Git does not know which version to pick, and asks a person to resolve it manually.


When a conflict occurs

A conflict appears if:

  • in two branches
  • the same lines of a file were changed
  • after a common ancestor commit

Example:

  • in the main branch, a line was changed to A
  • in the feature branch, the same line was changed to B

Git cannot guess which one is correct.


What happens during a conflict

When you run:

bash
git merge feature

Git:

  1. stops the merge
  2. marks the files as conflicted
  3. shows a conflict message

What a conflict looks like in a file

In a conflicting file you will see something like:

text
<<<<<<< HEAD console.log("Hello from main"); ======= console.log("Hello from feature"); >>>>>>> feature
  • HEAD is the current branch
  • below ======= are the changes from the other branch

What you need to do

  1. Open the file
  2. Choose the correct version or combine both
  3. Remove the conflict markers (<<<<<<<, =======, >>>>>>>)
  4. Save the file
  5. Add the file to the index:
bash
git add file.js
  1. Finish the merge:
bash
git commit

Important to understand

  • a conflict is not an error
  • Git deliberately stops so you do not lose data
  • without a manual resolution the merge will not complete

A common interview question

Can conflicts be avoided?

Completely, no. But you can:

  • pull main more often
  • make small commits
  • avoid working for a long time detached from the main branch

Short answer for an interview

A merge conflict occurs when Git cannot automatically combine changes from different branches because they touch the same parts of the code, and it requires manual intervention.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.