← Back to Git Course | Chapter 7: Collaboration | Lesson 4 of 6

Git Conflict Resolution

A merge conflict is like two people erasing and rewriting the same sentence in different ways. Git cannot guess which one you want, so it asks you to pick.
Syntax
bash
git merge branch_name
# edit the file to resolve the conflict markers
git add file_name
git commit

What is a Merge Conflict?

A merge conflict happens when the branch you're merging in and your current branch both changed the same lines of the same file since they diverged, so Git has no way to automatically decide which version is correct and pauses the merge for you to resolve it by hand.

Example: What is a Merge Conflict?

bash
git merge feature-x
# CONFLICT (content): Merge conflict in file.txt

Understanding Conflict Markers

Git marks each conflicting section directly in the file with <<<<<<<, =======, and >>>>>>> lines separating "your" version from "their" version. Resolving the conflict means editing the file to keep the correct content — from one side, the other, or a manual combination — and deleting all three marker lines completely.

Example: Understanding Conflict Markers

bash
<<<<<<< HEAD
your version
=======
their version
>>>>>>> feature-x

Staging and Committing the Resolution

Once every conflict marker is gone and the file reflects the outcome you want, git add <file> stages the resolution and git commit finishes the merge — Git already knows a merge was in progress, so it pre-fills a merge commit message for you.

Example: Staging and Committing the Resolution

bash
git add file.txt
git commit

Aborting a Conflicting Merge

If a conflict turns out to be more trouble than it's worth to resolve right now, git merge --abort cancels the merge entirely and returns your branch to exactly the state it was in before you started — a full escape hatch as long as you haven't already committed the resolution.

Example: Aborting a Conflicting Merge

bash
git merge --abort

Resolving Conflicts During a Rebase

Conflicts during a rebase work the same way file-wise, but the continuation command differs: after fixing and staging the file, you run git rebase --continue (not commit) to move on to replaying the next commit — and each subsequent commit in the rebase can trigger its own separate conflict.

Example: Resolving Conflicts During a Rebase

bash
git add file.txt
git rebase --continue
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Committing the file while it still contains <<<<<<<, ======= and >>>>>>> markers.
  2. Keeping only one side without reading the other, deleting work the other developer did.
  3. Forgetting git add after fixing the file, so git commit says the merge is still unresolved.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.