How do you resolve a merge conflict in Git?
Learn how to resolve a Git merge conflict: read the conflict markers, edit the file, stage with git add, commit, or safely back out with git merge --abort.
Expected Interview Answer
A merge conflict happens when two branches change the same lines of a file differently, so Git cannot decide automatically; you resolve it by editing the marked file to the correct final content, staging it with git add, and completing the merge with git commit.
During a conflicting merge, Git pauses and inserts conflict markers (<<<<<<<, =======, >>>>>>>) into the affected files, showing your version (HEAD) and the incoming version. You open each file, choose or combine the changes, and delete the markers so the file reads correctly. Running git add on the resolved file tells Git the conflict is settled, and git commit (or git merge --continue) records the merge. You can abort the whole attempt with git merge --abort to return to the pre-merge state, and tools like git mergetool or git status help track what remains.
- Gives you full control over the final merged content
- Conflict markers make the two versions explicit
- git merge --abort provides a safe escape hatch
- git add records resolution incrementally per file
- Merge tools and git status make progress easy to track
AI Mentor Explanation
A merge conflict is like two scorers writing different totals for the same over — the match cannot proceed until the captains sit down, agree on the true figure by checking each delivery, cross out both disputed entries, and initial the corrected line. In Git you likewise open the disputed file, decide the correct content, remove both conflicting versions' markers, and sign off by staging and committing.
Step-by-Step Explanation
Step 1
Identify conflicts
After a merge stops, run git status to list files marked as 'both modified' — these contain conflicts.
Step 2
Open the marked files
Find the <<<<<<< HEAD, =======, and >>>>>>> markers showing your version and the incoming version.
Step 3
Edit to the final content
Choose one side, combine both, or rewrite the section, then delete all conflict markers so the file is clean.
Step 4
Stage the resolution
Run git add <file> for each resolved file to tell Git the conflict is settled.
Step 5
Complete or abort
Finish with git commit (or git merge --continue), or bail out entirely with git merge --abort.
What Interviewer Expects
- Understanding what causes a conflict (same lines changed differently)
- Ability to read <<<<<<<, =======, >>>>>>> markers
- Knowing git add marks a file as resolved
- Awareness of git merge --abort as an escape hatch
- Familiarity with git status and merge tools to track progress
Common Mistakes
- Leaving conflict markers in the file and committing broken code
- Blindly accepting one side without reviewing the other's changes
- Forgetting to git add resolved files before committing
- Not knowing git merge --abort exists to safely back out
- Assuming Git resolves all conflicts automatically
Best Answer (HR Friendly)
“A merge conflict happens when two people change the same part of a file and Git can't automatically pick a winner. You resolve it by opening the file, deciding what the correct final version should be, removing the markers Git added, and then telling Git it's fixed by staging and committing the change.”
Code Example
git merge feature
# CONFLICT (content): Merge conflict in app.js
# Automatic merge failed; fix conflicts and then commit
git status # lists app.js as 'both modified'
# Edit app.js, remove the markers, keep the correct content:
# <<<<<<< HEAD
# your version
# =======
# incoming version
# >>>>>>> feature
git add app.js # mark this file resolved
git commit # record the merge (or: git merge --continue)
# Change your mind and undo the whole merge:
git merge --abortFollow-up Questions
- What do the <<<<<<<, =======, and >>>>>>> markers each represent?
- How does git merge --abort differ from git reset --hard?
- What is git mergetool and when would you use it?
- How can you reduce the frequency of merge conflicts?
- How do rebase conflicts differ from merge conflicts?
MCQ Practice
1. What causes a Git merge conflict?
Conflicts arise when the same lines of a file are changed differently on both branches, so Git cannot merge them automatically.
2. After editing a conflicted file, what tells Git it is resolved?
Running git add on the edited file marks the conflict as resolved so the merge can be completed with a commit.
3. Which command safely cancels an in-progress conflicted merge?
git merge --abort restores the working tree and index to the state before the merge began, discarding the conflicted attempt.
Flash Cards
What causes a merge conflict? — Two branches changing the same lines of a file differently, so Git can't auto-merge.
What do the conflict markers mean? — <<<<<<< HEAD is your version, ======= separates, >>>>>>> is the incoming version.
How do you mark a conflict resolved? — Edit the file, remove the markers, then run git add on it.
How do you cancel a conflicted merge? — Run git merge --abort to restore the pre-merge state.