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

View File

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

View File

@@ -51,19 +51,19 @@ if ($LASTEXITCODE -ne 0) {
$allChecksPassed = $false $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") { if (Test-Path "calculator.py") {
$calcContent = Get-Content "calculator.py" -Raw $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) # Check that modulo function still exists (should be preserved)
if ($calcContent -match "def modulo") { if ($calcContent -match "def modulo") {
Write-Host "[PASS] modulo function preserved (good commit after bad one)" -ForegroundColor Green Write-Host "[PASS] modulo function preserved (good commit after bad one)" -ForegroundColor Green
@@ -160,26 +160,26 @@ if ($LASTEXITCODE -ne 0) {
$allChecksPassed = $false $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 # Check calculator.py content
if (Test-Path "calculator.py") { if (Test-Path "calculator.py") {
$calcContent = Get-Content "calculator.py" -Raw $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) # Check that power function still exists (good commit before bad ones)
if ($calcContent -match "def power") { if ($calcContent -match "def power") {
Write-Host "[PASS] power function preserved" -ForegroundColor Green Write-Host "[PASS] power function preserved" -ForegroundColor Green