fix: revert conflict

This commit is contained in:
Bjarke Sporring
2026-01-15 15:50:54 +01:00
parent 939bd397f1
commit aa24c50b45
3 changed files with 141 additions and 207 deletions

View File

@@ -35,8 +35,8 @@ Before starting this module, you should be comfortable with:
Run the setup script to create the challenge environment:
```powershell
./setup.ps1
```pwsh
.\setup.ps1
```
This creates a `challenge/` directory with three branches demonstrating different revert scenarios:
@@ -52,44 +52,57 @@ You're working on a calculator application. A developer added a `divide` functio
### Your Task
1. Navigate to the challenge directory:
```bash
1. **Navigate to the challenge directory:**
```pwsh
cd challenge
```
2. You should be on the `regular-revert` branch. View the commit history:
```bash
2. **Check which branch you're on** (you should be on `regular-revert`):
```pwsh
git branch
```
The `*` should be next to `regular-revert`.
3. **View the commit history:**
```pwsh
git log --oneline
```
3. Find the commit with the broken divide function (message: "Add broken divide function - needs to be reverted!")
4. **Find the commit with message:** "Add broken divide function - needs to be reverted!"
- Note the commit hash (the 7-character code at the start, like `a1b2c3d`)
- Write it down or copy it
4. Revert that specific commit:
```bash
5. **Revert that specific commit** (replace `<commit-hash>` with the actual hash):
```pwsh
git revert <commit-hash>
```
5. Git will open your editor for the revert commit message. The default message is fine—save and close.
6. **Visual Studio Code will open** with the revert commit message:
- The default message is fine (it says "Revert 'Add broken divide function...'")
- Close the editor window to accept the commit message
- Git will create the revert commit
### What to Observe
After reverting, check:
After reverting, check your work:
```bash
# View the new revert commit
```pwsh
# View the new revert commit in history
git log --oneline
# Check that divide function is gone
cat calculator.py | grep "def divide" # Should return nothing
# Check that divide.py file is gone (reverted)
ls
# You should see calculator.py but NOT divide.py
# Check that modulo function still exists (it came after the bad commit)
cat calculator.py | grep "def modulo" # Should find it
# Check that modulo function still exists in calculator.py (it came after the bad commit)
cat calculator.py
# You should see def modulo
# Check that multiply function still exists (it came before the bad commit)
cat calculator.py | grep "def multiply" # Should find it
# (You already see it when you cat the file above)
```
**Key insight:** Revert creates a new commit that undoes the changes from the target commit, but leaves all other commits intact.
**Key insight:** Revert creates a NEW commit that undoes the changes from the target commit, but leaves all other commits intact.
### Understanding the Timeline
@@ -141,35 +154,43 @@ When reverting a merge, you must specify which parent to keep using the `-m` fla
### Your Task
1. Switch to the merge-revert branch:
```bash
1. **Switch to the merge-revert branch:**
```pwsh
git switch merge-revert
```
2. View the commit history and find the merge commit:
```bash
2. **View the commit history with the graph:**
```pwsh
git log --oneline --graph
```
Look for: "Merge feature-auth branch"
Look for the merge commit message: "Merge feature-auth branch"
- Note the commit hash
- Write it down or copy it
3. Revert the merge commit using `-m 1`:
```bash
3. **Revert the merge commit using `-m 1`** (replace `<merge-commit-hash>` with actual hash):
```pwsh
git revert -m 1 <merge-commit-hash>
```
**Explanation:**
- `-m 1` tells Git to keep parent 1 (main branch)
**What `-m 1` means:**
- `-m 1` tells Git to keep parent 1 (the main branch side)
- This undoes all changes from the feature-auth branch
- Creates a new "revert merge" commit
4. Save the default commit message and check the result:
```bash
# Verify auth.py is gone
ls auth.py # Should not exist
4. **Visual Studio Code will open** with the revert commit message:
- Close the editor window to accept it
- Git will create the revert commit
5. **Check the result:**
```pwsh
# View files - auth.py should be gone
ls
# You should see calculator.py but NOT auth.py
# Verify calculator.py no longer imports auth
cat calculator.py | grep "from auth" # Should return nothing
cat calculator.py
# Should NOT see "from auth import" anywhere
```
### What Happens Without -m?
@@ -242,44 +263,48 @@ Two separate commits added broken mathematical functions (`square_root` and `log
### Your Task
1. Switch to the multi-revert branch:
```bash
1. **Switch to the multi-revert branch:**
```pwsh
git switch multi-revert
```
2. View the commit history:
```bash
2. **View the commit history:**
```pwsh
git log --oneline
```
Find the two commits:
Find the two bad commits:
- "Add broken square_root - REVERT THIS!"
- "Add broken logarithm - REVERT THIS TOO!"
Note both commit hashes (write them down)
3. Revert both commits in one command:
```bash
3. **Revert both commits in one command** (replace with actual hashes):
```pwsh
git revert <commit-hash-1> <commit-hash-2>
```
**Important:** List commits from **oldest to newest** for cleanest history.
**Important:** List commits from **oldest to newest** for cleanest history (square_root first, then logarithm).
Alternatively, revert them one at a time:
```bash
**Alternatively**, revert them one at a time:
```pwsh
git revert <commit-hash-1>
git revert <commit-hash-2>
```
4. Git will prompt for a commit message for each revert. Accept the defaults.
4. **Visual Studio Code will open TWICE** (once for each revert):
- Close the editor each time to accept the default commit message
- Git will create two revert commits
5. Verify the result:
```bash
# Check that both bad functions are gone
cat calculator.py | grep "def square_root" # Should return nothing
cat calculator.py | grep "def logarithm" # Should return nothing
5. **Verify the result:**
```pwsh
# View files - sqrt.py and logarithm.py should be gone
ls
# You should see calculator.py but NOT sqrt.py or logarithm.py
# Check that good functions remain
cat calculator.py | grep "def power" # Should find it
cat calculator.py | grep "def absolute" # Should find it
# Check that good functions remain in calculator.py
cat calculator.py
# You should see def power and def absolute
```
### Multi-Revert Strategies
@@ -311,11 +336,17 @@ This is useful when reverting multiple commits and you want one combined revert
## Verification
Verify your solutions by running the verification script:
After completing all three challenges, verify your solutions:
```bash
cd .. # Return to module directory
./verify.ps1
```pwsh
cd .. # Return to module directory (if you're in challenge/)
.\verify.ps1
```
Or from inside the challenge directory:
```pwsh
..\verify.ps1
```
The script checks that:
@@ -329,7 +360,7 @@ The script checks that:
### Basic Revert
```bash
```pwsh
# Revert a specific commit
git revert <commit-hash>
@@ -342,7 +373,7 @@ git revert HEAD~1
### Merge Commit Revert
```bash
```pwsh
# Revert a merge commit (keep parent 1)
git revert -m 1 <merge-commit-hash>
@@ -352,7 +383,7 @@ git revert -m 2 <merge-commit-hash>
### Multiple Commits
```bash
```pwsh
# Revert multiple specific commits
git revert <hash1> <hash2> <hash3>
@@ -365,7 +396,7 @@ git revert HEAD~3..HEAD
### Revert Options
```bash
```pwsh
# Revert but don't commit automatically
git revert --no-commit <commit-hash>

View File

@@ -67,75 +67,37 @@ Write-Host "Default branch detected: $mainBranch" -ForegroundColor Yellow
# Create regular-revert branch
git switch -c regular-revert | Out-Null
# Good commit: Add multiply
$calcContent = @"
# calculator.py - Simple calculator
def add(a, b):
"""Add two numbers."""
return a + b
def subtract(a, b):
"""Subtract b from a."""
return a - b
# Good commit: Add multiply using append
$multiplyFunc = @"
def multiply(a, b):
"""Multiply two numbers."""
return a * b
"@
Set-Content -Path "calculator.py" -Value $calcContent
Add-Content -Path "calculator.py" -Value $multiplyFunc
git add .
git commit -m "Add multiply function" | Out-Null
# BAD commit: Add broken divide function
$calcContent = @"
# calculator.py - Simple calculator
def add(a, b):
"""Add two numbers."""
return a + b
def subtract(a, b):
"""Subtract b from a."""
return a - b
def multiply(a, b):
"""Multiply two numbers."""
return a * b
# BAD commit: Add broken divide function using separate file
$divideContent = @"
# divide.py - Division functionality
def divide(a, b):
"""Divide a by b - BROKEN: doesn't handle division by zero!"""
return a / b # This will crash if b is 0!
"@
Set-Content -Path "calculator.py" -Value $calcContent
Set-Content -Path "divide.py" -Value $divideContent
git add .
git commit -m "Add broken divide function - needs to be reverted!" | Out-Null
# Good commit: Add modulo (after bad commit)
$calcContent = @"
# calculator.py - Simple calculator
def add(a, b):
"""Add two numbers."""
return a + b
def subtract(a, b):
"""Subtract b from a."""
return a - b
def multiply(a, b):
"""Multiply two numbers."""
return a * b
def divide(a, b):
"""Divide a by b - BROKEN: doesn't handle division by zero!"""
return a / b # This will crash if b is 0!
# Good commit: Add modulo (after bad commit) using append
$moduloFunc = @"
def modulo(a, b):
"""Return remainder of a divided by b."""
return a % b
"@
Set-Content -Path "calculator.py" -Value $calcContent
Add-Content -Path "calculator.py" -Value $moduloFunc
git add .
git commit -m "Add modulo function" | Out-Null
@@ -254,109 +216,50 @@ Set-Content -Path "calculator.py" -Value $calcContent
git add .
git commit -m "Reset to basic calculator" | Out-Null
# Good commit: Add power function
$calcContent = @"
# calculator.py - Simple calculator
def add(a, b):
"""Add two numbers."""
return a + b
def subtract(a, b):
"""Subtract b from a."""
return a - b
# Good commit: Add power function using append
$powerFunc = @"
def power(a, b):
"""Raise a to the power of b."""
return a ** b
"@
Set-Content -Path "calculator.py" -Value $calcContent
Add-Content -Path "calculator.py" -Value $powerFunc
git add .
git commit -m "Add power function" | Out-Null
# BAD commit 1: Add broken square_root
$calcContent = @"
# calculator.py - Simple calculator
def add(a, b):
"""Add two numbers."""
return a + b
def subtract(a, b):
"""Subtract b from a."""
return a - b
def power(a, b):
"""Raise a to the power of b."""
return a ** b
# BAD commit 1: Add broken square_root in separate file
$sqrtContent = @"
# sqrt.py - Square root functionality
def square_root(a):
"""BROKEN: Returns wrong result for negative numbers!"""
return a ** 0.5 # This returns NaN for negative numbers!
"@
Set-Content -Path "calculator.py" -Value $calcContent
Set-Content -Path "sqrt.py" -Value $sqrtContent
git add .
git commit -m "Add broken square_root - REVERT THIS!" | Out-Null
# BAD commit 2: Add broken logarithm
$calcContent = @"
# calculator.py - Simple calculator
def add(a, b):
"""Add two numbers."""
return a + b
def subtract(a, b):
"""Subtract b from a."""
return a - b
def power(a, b):
"""Raise a to the power of b."""
return a ** b
def square_root(a):
"""BROKEN: Returns wrong result for negative numbers!"""
return a ** 0.5 # This returns NaN for negative numbers!
# BAD commit 2: Add broken logarithm in separate file
$logContent = @"
# logarithm.py - Logarithm functionality
def logarithm(a):
"""BROKEN: Doesn't handle zero or negative numbers!"""
import math
return math.log(a) # This crashes for a <= 0!
"@
Set-Content -Path "calculator.py" -Value $calcContent
Set-Content -Path "logarithm.py" -Value $logContent
git add .
git commit -m "Add broken logarithm - REVERT THIS TOO!" | Out-Null
# Good commit: Add absolute value (after bad commits)
$calcContent = @"
# calculator.py - Simple calculator
def add(a, b):
"""Add two numbers."""
return a + b
def subtract(a, b):
"""Subtract b from a."""
return a - b
def power(a, b):
"""Raise a to the power of b."""
return a ** b
def square_root(a):
"""BROKEN: Returns wrong result for negative numbers!"""
return a ** 0.5 # This returns NaN for negative numbers!
def logarithm(a):
"""BROKEN: Doesn't handle zero or negative numbers!"""
import math
return math.log(a) # This crashes for a <= 0!
# Good commit: Add absolute value (after bad commits) using append
$absoluteFunc = @"
def absolute(a):
"""Return absolute value of a."""
return abs(a)
"@
Set-Content -Path "calculator.py" -Value $calcContent
Add-Content -Path "calculator.py" -Value $absoluteFunc
git add .
git commit -m "Add absolute value function" | Out-Null

View File

@@ -51,19 +51,19 @@ if ($LASTEXITCODE -ne 0) {
$allChecksPassed = $false
}
# Check that calculator.py exists
# Check that divide.py is removed (was reverted)
if (-not (Test-Path "divide.py")) {
Write-Host "[PASS] Broken divide.py successfully reverted (file removed)" -ForegroundColor Green
} else {
Write-Host "[FAIL] divide.py still exists (should be reverted)" -ForegroundColor Red
Write-Host "[HINT] The bad commit should be reverted, removing divide.py" -ForegroundColor Yellow
$allChecksPassed = $false
}
# Check that calculator.py exists and has correct content
if (Test-Path "calculator.py") {
$calcContent = Get-Content "calculator.py" -Raw
# Check that divide function is NOT in the code (was reverted)
if ($calcContent -notmatch "def divide") {
Write-Host "[PASS] Broken divide function successfully reverted" -ForegroundColor Green
} else {
Write-Host "[FAIL] divide function still exists (should be reverted)" -ForegroundColor Red
Write-Host "[HINT] The bad commit should be reverted, removing the divide function" -ForegroundColor Yellow
$allChecksPassed = $false
}
# Check that modulo function still exists (should be preserved)
if ($calcContent -match "def modulo") {
Write-Host "[PASS] modulo function preserved (good commit after bad one)" -ForegroundColor Green
@@ -160,26 +160,26 @@ if ($LASTEXITCODE -ne 0) {
$allChecksPassed = $false
}
# Check that sqrt.py is removed (reverted)
if (-not (Test-Path "sqrt.py")) {
Write-Host "[PASS] Broken sqrt.py successfully reverted (file removed)" -ForegroundColor Green
} else {
Write-Host "[FAIL] sqrt.py still exists (should be reverted)" -ForegroundColor Red
$allChecksPassed = $false
}
# Check that logarithm.py is removed (reverted)
if (-not (Test-Path "logarithm.py")) {
Write-Host "[PASS] Broken logarithm.py successfully reverted (file removed)" -ForegroundColor Green
} else {
Write-Host "[FAIL] logarithm.py still exists (should be reverted)" -ForegroundColor Red
$allChecksPassed = $false
}
# Check calculator.py content
if (Test-Path "calculator.py") {
$calcContent = Get-Content "calculator.py" -Raw
# Check that square_root is NOT in code (reverted)
if ($calcContent -notmatch "def square_root") {
Write-Host "[PASS] Broken square_root function reverted" -ForegroundColor Green
} else {
Write-Host "[FAIL] square_root function still exists (should be reverted)" -ForegroundColor Red
$allChecksPassed = $false
}
# Check that logarithm is NOT in code (reverted)
if ($calcContent -notmatch "def logarithm") {
Write-Host "[PASS] Broken logarithm function reverted" -ForegroundColor Green
} else {
Write-Host "[FAIL] logarithm function still exists (should be reverted)" -ForegroundColor Red
$allChecksPassed = $false
}
# Check that power function still exists (good commit before bad ones)
if ($calcContent -match "def power") {
Write-Host "[PASS] power function preserved" -ForegroundColor Green