01 / 20

Resolving Merge
Conflicts

Learn how Git detects conflicting changes, how to read conflict markers, and how to resolve them cleanly in your Python projects.

Python Git & Version Control

What Is a Merge Conflict?

A merge conflict occurs when two branches modify the same lines of the same file, and Git cannot automatically determine which version to keep. Instead of guessing, Git pauses and asks you to decide.

Why Do Conflicts Happen?

CONFLICT ? main Branch A Branch B split point Both edit the same lines

When Conflicts Don't Happen

Git is actually very smart. It can auto-merge when changes are in different files or even different lines of the same file. Conflicts only arise when the exact same lines are modified.

✓ Auto-Merges Fine
Different files changed, or different lines in the same file. Git combines both sets of changes automatically.
✕ Causes Conflict
Same file, same lines changed differently. Git cannot decide which version wins and flags a conflict.

The Anatomy of Conflict Markers

When a conflict occurs, Git inserts special conflict markers directly into the file. There are three markers that divide the conflicting sections:

<<<<<<< HEAD
your changes (current branch)
=======
their changes (incoming branch)
>>>>>>> feature-branch

Reading the Three Sections

<<<<<<< HEAD greeting = "Hello, World!" print(greeting.upper()) YOUR CODE ======= greeting = "Hi there!" print(greeting.lower()) THEIR CODE >>>>>>> feature-branch →

Merge vs Rebase Conflicts

Both merge and rebase can produce conflicts. The markers look the same, but the context differs:

git merge git rebase
HEAD means Your current branch tip The branch you're rebasing onto
Incoming means The branch being merged in Your own commit being replayed
Resolve then run git add + git commit git add + git rebase --continue
Abort with git merge --abort git rebase --abort

How to Resolve Step by Step

1
Run git status. Identify which files are listed as "both modified" (conflicted).
2
Open the conflicted file. Look for the <<<<<<<, =======, and >>>>>>> markers.
3
Decide what to keep. Choose your version, their version, a combination, or write something entirely new.
4
Delete all conflict markers. Remove every <<<<<<<, =======, and >>>>>>> line completely.
5
Stage and commit. Run git add on the resolved file, then git commit to finalize.

Example 1: Keep One Side

The simplest resolution: you decide one version is correct and delete the other entirely.

<<<<<<< HEAD
def greet(name):
    return f"Hello, {name}!"
=======
def greet(name):
    return f"Hi, {name}!"
>>>>>>> feature-greeting
def greet(name):
    return f"Hello, {name}!"

Example 2: Combine Both Sides

Sometimes both changes are valuable and should be merged together into a new version.

<<<<<<< HEAD
colors = ["red", "blue"]
=======
colors = ["red", "green"]
>>>>>>> add-colors
colors = ["red", "blue", "green"]

Example 3: Multi-line Conflict

Real-world conflicts often span multiple lines. Read each section carefully and understand the intent before resolving.

def calculate_total(items):
<<<<<<< HEAD
    total = sum(item.price for item in items)
    tax = total * 0.08
    return total + tax
=======
    subtotal = sum(item.price for item in items)
    discount = subtotal * 0.10
    return subtotal - discount
>>>>>>> add-discount
def calculate_total(items):
    subtotal = sum(item.price for item in items)
    discount = subtotal * 0.10
    tax = (subtotal - discount) * 0.08
    return subtotal - discount + tax

The Resolution Workflow

git merge feature-branch CONFLICT! git status Open File Find markers Edit File Resolve conflict Remove Markers Delete all <<< === >>> git add Stage resolved git commit Done!

Three Resolution Strategies

Accept Ours
Keep only the HEAD version. Delete the incoming section and all markers. Use when your version is correct.
Accept Theirs
Keep only the incoming version. Delete the HEAD section and all markers. Use when the other branch is correct.
Manual Merge
Combine elements from both sides, or write entirely new code. This is the most common and powerful strategy.

Useful Git Commands

1
git status: shows which files have conflicts (listed as "both modified").
2
git diff: shows the exact differences in conflicted files.
3
git merge --abort: cancels the merge entirely and returns to the pre-merge state.
4
git log --merge: shows only the commits that caused the conflict.
5
git checkout --ours/--theirs <file>: quickly accept one entire side for a specific file.

Common Mistakes

✕ Leaving Markers In
Committing the file with <<<<<<<, =======, or >>>>>>> still present. This breaks your code and confuses teammates.
✕ Deleting Both Sides
Accidentally removing both versions and leaving the section empty. Always verify the resolved file runs correctly.
✕ Forgetting git add
Editing the file but not staging it. Git still considers the file unresolved until you run git add on it.

Resolution Cheat Sheet

Scenario Action Command
See conflicted files Check status git status
Keep your version entirely Accept ours git checkout --ours file
Keep their version entirely Accept theirs git checkout --theirs file
Manually combine both Edit + remove markers edit file, git add, git commit
Cancel the merge Abort git merge --abort

Question 1 of 5

What does the ======= marker represent in a merge conflict?

Question 2 of 5

You want to keep the version from feature-login. Which line should remain after resolving?

Question 3 of 5

After manually editing a conflicted file to resolve the conflict, what must you do before committing?

Question 4 of 5

Which command lets you cancel a merge that has caused conflicts and return to the state before the merge?

Question 5 of 5

What will happen when Developer B runs git merge?