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
mainbranch, a line was changed toA - in the
featurebranch, the same line was changed toB
Git cannot guess which one is correct.
What happens during a conflict
When you run:
bash
git merge featureGit:
- stops the merge
- marks the files as conflicted
- 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");
>>>>>>> featureHEADis the current branch- below
=======are the changes from the other branch
What you need to do
- Open the file
- Choose the correct version or combine both
- Remove the conflict markers (
<<<<<<<,=======,>>>>>>>) - Save the file
- Add the file to the index:
bash
git add file.js- Finish the merge:
bash
git commitImportant 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
mainmore 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 readyPremium
A concise answer to help you respond confidently on this topic during an interview.