Work on Two Things at Once
When Git Can't Figure It Out
Sometimes two branches change the same part of the same file. When you try to merge them, Git can't decide which version to keep, so it asks you to resolve the conflict manually. This sounds scary, but it's straightforward once you've done it.
A conflict arises when the branches have diverged - both have new commits since they split:
E feature/modulo
/
A───B──D
\
F main
Both branches changed the same section of calculator.py,
so Git can't automatically combine them.
Let's set up a conflict on purpose. First, create a new branch and modify the "Try it out" section:
git switch -c feature/modulo
cat << 'EOF' > calculator.py
# calculator.py - A simple calculator
def add(a, b):
"""Add two numbers and return the result."""
return a + b
def subtract(a, b):
"""Subtract b from a and return the result."""
return a - b
def multiply(a, b):
"""Multiply two numbers and return the result."""
return a * b
def divide(a, b):
"""Divide a by b and return the result."""
if b == 0:
return "Error: Cannot divide by zero"
return a / b
def power(a, b):
"""Raise a to the power of b and return the result."""
return a ** b
def modulo(a, b):
"""Return the remainder of a divided by b."""
if b == 0:
return "Error: Cannot divide by zero"
return a % b
# Try it out
print("=== Calculator Demo ===")
print(f"5 + 3 = {add(5, 3)}")
print(f"10 - 4 = {subtract(10, 4)}")
print(f"6 * 7 = {multiply(6, 7)}")
print(f"10 / 2 = {divide(10, 2)}")
print(f"2 ^ 8 = {power(2, 8)}")
print(f"10 % 3 = {modulo(10, 3)}")
EOF
git add calculator.py
git commit -m "Add modulo function"
Now switch back to main and make a different change to the same section:
git switch main
Next, modify the "Try it out" section to have a different header and formatting:
cat << 'EOF' > calculator.py
# calculator.py - A simple calculator
def add(a, b):
"""Add two numbers and return the result."""
return a + b
def subtract(a, b):
"""Subtract b from a and return the result."""
return a - b
def multiply(a, b):
"""Multiply two numbers and return the result."""
return a * b
def divide(a, b):
"""Divide a by b and return the result."""
if b == 0:
return "Error: Cannot divide by zero"
return a / b
def power(a, b):
"""Raise a to the power of b and return the result."""
return a ** b
# Try it out
print("--- Calculator Results ---")
print(f"Addition: 5 + 3 = {add(5, 3)}")
print(f"Subtraction: 10 - 4 = {subtract(10, 4)}")
print(f"Multiplication: 6 * 7 = {multiply(6, 7)}")
print(f"Division: 10 / 2 = {divide(10, 2)}")
print(f"Power: 2 ^ 8 = {power(2, 8)}")
EOF
Add and commit:
git add calculator.py
git commit -m "Improve output formatting"
Now both branches changed the "Try it out" section differently. Let's merge:
git merge feature/modulo
Git will report a conflict:
Auto-merging calculator.pyLearn Git in a Day
Everything you need, nothing you don'tEnroll now to unlock all content and receive all future updates for free.
Hurry! This limited time offer ends in:
To redeem this offer, copy the coupon code below and apply it at checkout:
