feat: split out reset from the revert. First the safe path then the advanced path

This commit is contained in:
Bjarke Sporring
2026-01-11 23:02:48 +01:00
parent 039debe744
commit 8d63b2d22e
20 changed files with 2618 additions and 607 deletions

View File

@@ -1,198 +0,0 @@
# Module 10: Reset vs Revert
## Learning Objectives
By the end of this module, you will:
- Understand the difference between `git reset` and `git revert`
- Know when to use reset vs revert
- Understand the three modes of reset (--soft, --mixed, --hard)
- Safely undo commits in both local and shared branches
- Understand the risks of rewriting history
## Challenge Description
You have two branches with problematic commits:
1. **local-feature**: A private branch with bad commits that you haven't shared with anyone
2. **shared-feature**: A branch that has been pushed and others might be using
Your task is to:
1. Use `git reset` to remove the bad commit from the local-feature branch (safe because it's not shared)
2. Use `git revert` to undo the bad commit from the shared-feature branch (safe because it preserves history)
## Key Concepts
### Git Reset: Rewriting History
`git reset` moves the branch pointer backward, effectively erasing commits from history. It has three modes:
**--soft**: Moves HEAD, keeps changes staged
```bash
git reset --soft HEAD~1
# Commit is gone, but changes are staged and ready to commit again
```
**--mixed** (default): Moves HEAD, keeps changes unstaged
```bash
git reset HEAD~1
# Commit is gone, changes are in working directory but not staged
```
**--hard**: Moves HEAD, discards all changes
```bash
git reset --hard HEAD~1
# Commit is gone, changes are PERMANENTLY DELETED
```
### Git Revert: Safe Undo
`git revert` creates a NEW commit that undoes the changes from a previous commit. History is preserved.
```bash
git revert <commit-hash>
# Creates a new commit that reverses the specified commit
```
### Visual Comparison
**Before (both branches):**
```
A---B---C---D (D is the bad commit)
```
**After Reset (rewrites history):**
```
A---B---C
```
Commit D is gone. If anyone else had D, they'll have problems.
**After Revert (preserves history):**
```
A---B---C---D---E
```
E is a new commit that undoes D. Everyone can pull E safely.
### When to Use Each
**Use Reset when:**
- The commits haven't been pushed to a shared repository
- You're cleaning up local commits before pushing
- You made a mistake locally and want to start over
- You're working alone on a branch
**Use Revert when:**
- The commits have been pushed to a shared repository
- Others might have based work on these commits
- You want to preserve the complete history
- You need a safe, reversible undo operation
### The Golden Rule
**Never use `git reset` on commits that have been pushed to a shared branch!**
This will cause problems for anyone who has pulled those commits. Use `git revert` instead.
## Useful Commands
```bash
# Reset (for local-only commits)
git reset --soft HEAD~1 # Undo commit, keep changes staged
git reset HEAD~1 # Undo commit, keep changes unstaged
git reset --hard HEAD~1 # Undo commit, discard changes (DANGEROUS!)
# Reset to a specific commit
git reset --hard <commit-hash>
# Revert (for shared commits)
git revert <commit-hash>
git revert HEAD # Revert the last commit
# See what would be affected before resetting
git log --oneline
git diff HEAD~1
# If you reset by mistake, you can sometimes recover with reflog
git reflog
git reset --hard <commit-hash-from-reflog>
```
## Verification
Run the verification script to check your solution:
```bash
.\verify.ps1
```
The verification will check that:
- local-feature branch has the bad commit removed via reset
- shared-feature branch has the bad commit undone via revert
- shared-feature has a revert commit in the history
- All good commits are preserved
## Challenge Steps
1. Navigate to the challenge directory
2. You're on the local-feature branch with a bad commit
3. View commits: `git log --oneline`
4. Use `git reset --hard HEAD~1` to remove the bad commit
5. Switch to shared-feature: `git switch shared-feature`
6. View commits: `git log --oneline`
7. Find the hash of the "Add broken feature" commit
8. Use `git revert <commit-hash>` to undo it safely
9. Run the verification script
## Tips
- `HEAD~1` means "one commit before HEAD"
- `HEAD~2` means "two commits before HEAD"
- Always check `git log` before and after reset/revert
- `git reset --hard` is DANGEROUS - it permanently deletes uncommitted changes
- If you're unsure, use `git reset --soft` instead of `--hard`
- Revert will open an editor for the commit message - you can accept the default
- You can always use `.\reset.ps1` to start over if you make a mistake
## Common Mistakes to Avoid
### Mistake 1: Using Reset on Pushed Commits
```bash
# DON'T DO THIS if the commit was pushed!
git reset --hard HEAD~1
git push --force # This will cause problems for others
```
### Mistake 2: Using --hard Without Checking
```bash
# This DELETES your work permanently!
git reset --hard HEAD~1 # Uncommitted changes are GONE
```
### Mistake 3: Reverting the Wrong Commit
```bash
# Always double-check the commit hash
git log --oneline
git show <commit-hash> # Verify it's the right commit
git revert <commit-hash> # Now revert it
```
## Recovery from Mistakes
If you reset by accident, Git keeps a reflog:
```bash
# See recent HEAD movements
git reflog
# Find the commit you want to restore
# Output looks like:
# abc1234 HEAD@{0}: reset: moving to HEAD~1
# def5678 HEAD@{1}: commit: The commit you just lost
# Restore it
git reset --hard def5678
```
The reflog is your safety net, but it only keeps history for about 30 days.
## What You'll Learn
Understanding when to use reset versus revert is crucial for safe Git usage. Reset is powerful but dangerous when used on shared commits, while revert is always safe but creates additional history. Mastering both commands and knowing which to use in different situations is a hallmark of Git expertise. The rule is simple: if in doubt, use revert - it's always safe.

View File

@@ -1,22 +0,0 @@
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Resets the reset vs revert challenge environment.
.DESCRIPTION
Removes the existing challenge directory and runs setup.ps1
to create a fresh challenge environment.
#>
Write-Host "Resetting challenge environment..." -ForegroundColor Yellow
# Remove existing challenge directory if present
if (Test-Path "challenge") {
Remove-Item -Path "challenge" -Recurse -Force
Write-Host "Removed existing challenge directory." -ForegroundColor Cyan
}
# Run setup script
Write-Host "Running setup script...`n" -ForegroundColor Cyan
& ".\setup.ps1"

View File

@@ -1,190 +0,0 @@
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Sets up the reset vs revert challenge environment.
.DESCRIPTION
Creates a Git repository with two branches:
- local-feature: A private branch where reset should be used
- shared-feature: A pushed branch where revert should be used
#>
# Remove existing challenge directory if present
if (Test-Path "challenge") {
Write-Host "Removing existing challenge directory..." -ForegroundColor Yellow
Remove-Item -Path "challenge" -Recurse -Force
}
# Create challenge directory
Write-Host "Creating challenge environment..." -ForegroundColor Cyan
New-Item -ItemType Directory -Path "challenge" | Out-Null
Set-Location "challenge"
# Initialize git repository
git init | Out-Null
git config user.name "Workshop User" | Out-Null
git config user.email "user@workshop.local" | Out-Null
# Create initial commits on main
$app = @"
class Calculator:
def add(self, a, b):
return a + b
def subtract(self, a, b):
return a - b
"@
Set-Content -Path "calculator.py" -Value $app
git add calculator.py
git commit -m "Initial calculator implementation" | Out-Null
$readme = @"
# Calculator App
A simple calculator application.
"@
Set-Content -Path "README.md" -Value $readme
git add README.md
git commit -m "Add README" | Out-Null
# Create local-feature branch (private, not shared)
git checkout -b local-feature | Out-Null
$appWithMultiply = @"
class Calculator:
def add(self, a, b):
return a + b
def subtract(self, a, b):
return a - b
def multiply(self, a, b):
return a * b
"@
Set-Content -Path "calculator.py" -Value $appWithMultiply
git add calculator.py
git commit -m "Add multiply function" | Out-Null
# Add a bad commit that should be removed with reset
$appWithBadCode = @"
class Calculator:
def add(self, a, b):
return a + b
def subtract(self, a, b):
return a - b
def multiply(self, a, b):
return a * b
# BUG: This is broken and should never have been committed!
def divide(self, a, b):
# Forgot to check for division by zero
return a / b # This will raise ZeroDivisionError for zero!
"@
Set-Content -Path "calculator.py" -Value $appWithBadCode
git add calculator.py
git commit -m "Add broken divide function - DO NOT KEEP" | Out-Null
# Switch back to main for shared-feature branch
git checkout main | Out-Null
# Create shared-feature branch (simulating a pushed/shared branch)
git checkout -b shared-feature | Out-Null
$appWithPower = @"
class Calculator:
def add(self, a, b):
return a + b
def subtract(self, a, b):
return a - b
def power(self, a, b):
return a ** b
"@
Set-Content -Path "calculator.py" -Value $appWithPower
git add calculator.py
git commit -m "Add power function" | Out-Null
# Add a bad commit that should be reverted (not reset)
$appWithBrokenFeature = @"
import math
class Calculator:
def add(self, a, b):
return a + b
def subtract(self, a, b):
return a - b
def power(self, a, b):
return a ** b
# BUG: This breaks the calculator!
def square_root(self, a):
# This implementation is wrong for negative numbers
return math.sqrt(a) # Raises ValueError for negative numbers without warning!
"@
Set-Content -Path "calculator.py" -Value $appWithBrokenFeature
git add calculator.py
git commit -m "Add broken feature" | Out-Null
# Add another good commit after the bad one (to show that revert preserves subsequent commits)
$appWithMoreFeatures = @"
import math
class Calculator:
def add(self, a, b):
return a + b
def subtract(self, a, b):
return a - b
def power(self, a, b):
return a ** b
# BUG: This breaks the calculator!
def square_root(self, a):
# This implementation is wrong for negative numbers
return math.sqrt(a) # Raises ValueError for negative numbers without warning!
def modulo(self, a, b):
return a % b
"@
Set-Content -Path "calculator.py" -Value $appWithMoreFeatures
git add calculator.py
git commit -m "Add modulo function" | Out-Null
# Switch to local-feature for the challenge start
git checkout local-feature | Out-Null
# Return to module directory
Set-Location ..
Write-Host "`n========================================" -ForegroundColor Green
Write-Host "Challenge environment created!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host "`nYou have two branches with bad commits:" -ForegroundColor Cyan
Write-Host "`n1. local-feature (PRIVATE - not shared):" -ForegroundColor Yellow
Write-Host " - Has a broken divide function commit" -ForegroundColor White
Write-Host " - Safe to use 'git reset' to remove it" -ForegroundColor Green
Write-Host "`n2. shared-feature (PUBLIC - shared with team):" -ForegroundColor Yellow
Write-Host " - Has a broken feature commit" -ForegroundColor White
Write-Host " - Must use 'git revert' to undo it safely" -ForegroundColor Green
Write-Host "`nYour task:" -ForegroundColor Yellow
Write-Host "1. Navigate to the challenge directory: cd challenge" -ForegroundColor White
Write-Host "2. You're on local-feature - view commits: git log --oneline" -ForegroundColor White
Write-Host "3. Remove the bad commit with: git reset --hard HEAD~1" -ForegroundColor White
Write-Host "4. Switch to shared-feature: git checkout shared-feature" -ForegroundColor White
Write-Host "5. Find the 'Add broken feature' commit hash: git log --oneline" -ForegroundColor White
Write-Host "6. Revert it with: git revert <commit-hash>" -ForegroundColor White
Write-Host "`nRun '../verify.ps1' from the challenge directory to check your solution.`n" -ForegroundColor Cyan

View File

@@ -1,172 +0,0 @@
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Verifies the reset vs revert challenge solution.
.DESCRIPTION
Checks that the user correctly used reset on the local branch
and revert on the shared branch.
#>
Set-Location "challenge" -ErrorAction SilentlyContinue
# Check if challenge directory exists
if (-not (Test-Path "../verify.ps1")) {
Write-Host "Error: Please run this script from the module directory" -ForegroundColor Red
exit 1
}
if (-not (Test-Path ".")) {
Write-Host "Error: Challenge directory not found. Run setup.ps1 first." -ForegroundColor Red
Set-Location ..
exit 1
}
Write-Host "Verifying your solution..." -ForegroundColor Cyan
# Check if git repository exists
if (-not (Test-Path ".git")) {
Write-Host "[FAIL] No git repository found." -ForegroundColor Red
Set-Location ..
exit 1
}
# Verify local-feature branch
Write-Host "`nChecking local-feature branch..." -ForegroundColor Cyan
git checkout local-feature 2>$null | Out-Null
# Check commit count on local-feature (should be 3: initial + README + multiply)
$localCommitCount = (git rev-list --count local-feature 2>$null)
if ($localCommitCount -ne 3) {
Write-Host "[FAIL] local-feature should have 3 commits, found $localCommitCount" -ForegroundColor Red
if ($localCommitCount -gt 3) {
Write-Host "Hint: The bad commit should be removed. Use 'git reset --hard HEAD~1'" -ForegroundColor Yellow
} else {
Write-Host "Hint: You may have reset too far. Run ../reset.ps1 to start over." -ForegroundColor Yellow
}
Set-Location ..
exit 1
}
# Check that calculator.py exists
if (-not (Test-Path "calculator.py")) {
Write-Host "[FAIL] calculator.py not found." -ForegroundColor Red
Set-Location ..
exit 1
}
# Check calculator.py on local-feature
$localCalcContent = Get-Content "calculator.py" -Raw
# Should have multiply function
if ($localCalcContent -notmatch "multiply") {
Write-Host "[FAIL] calculator.py should have the multiply function." -ForegroundColor Red
Set-Location ..
exit 1
}
# Should NOT have divide function (it was in the bad commit that should be reset)
if ($localCalcContent -match "divide") {
Write-Host "[FAIL] calculator.py should NOT have the divide function." -ForegroundColor Red
Write-Host "Hint: Use 'git reset --hard HEAD~1' to remove the bad commit" -ForegroundColor Yellow
Set-Location ..
exit 1
}
# Check commit messages on local-feature
$localCommits = git log --pretty=format:"%s" local-feature 2>$null
if ($localCommits -match "broken divide") {
Write-Host "[FAIL] The 'broken divide' commit should be removed from local-feature." -ForegroundColor Red
Write-Host "Hint: Use 'git reset --hard HEAD~1' to remove it" -ForegroundColor Yellow
Set-Location ..
exit 1
}
Write-Host "[PASS] local-feature branch correctly reset!" -ForegroundColor Green
# Verify shared-feature branch
Write-Host "`nChecking shared-feature branch..." -ForegroundColor Cyan
git checkout shared-feature 2>$null | Out-Null
# Check commit count on shared-feature
# Should be 6: initial + README + power + broken feature + modulo + revert
$sharedCommitCount = (git rev-list --count shared-feature 2>$null)
if ($sharedCommitCount -ne 6) {
Write-Host "[FAIL] shared-feature should have 6 commits, found $sharedCommitCount" -ForegroundColor Red
if ($sharedCommitCount -lt 6) {
Write-Host "Hint: You should REVERT the bad commit, not reset it." -ForegroundColor Yellow
Write-Host " Revert creates a new commit that undoes the bad one." -ForegroundColor Yellow
Write-Host " Use: git revert <commit-hash>" -ForegroundColor Yellow
} else {
Write-Host "Hint: You should have exactly 6 commits after reverting." -ForegroundColor Yellow
}
Set-Location ..
exit 1
}
# Check that there's a revert commit
$sharedCommits = git log --pretty=format:"%s" shared-feature 2>$null
if ($sharedCommits -notmatch "Revert") {
Write-Host "[FAIL] No revert commit found on shared-feature." -ForegroundColor Red
Write-Host "Hint: Use 'git revert <commit-hash>' to undo the bad commit" -ForegroundColor Yellow
Write-Host " Find the hash with: git log --oneline" -ForegroundColor Yellow
Set-Location ..
exit 1
}
# Check calculator.py on shared-feature
$sharedCalcContent = Get-Content "calculator.py" -Raw
# Should have power function
if ($sharedCalcContent -notmatch "power") {
Write-Host "[FAIL] calculator.py should have the power function." -ForegroundColor Red
Set-Location ..
exit 1
}
# Should have modulo function (commits after the reverted one should be preserved)
if ($sharedCalcContent -notmatch "modulo") {
Write-Host "[FAIL] calculator.py should have the modulo function." -ForegroundColor Red
Write-Host "Hint: Reverting should preserve commits made after the bad one" -ForegroundColor Yellow
Set-Location ..
exit 1
}
# Should NOT have square_root function (it was in the bad commit that should be reverted)
if ($sharedCalcContent -match "square_root") {
Write-Host "[FAIL] calculator.py should NOT have the square_root function." -ForegroundColor Red
Write-Host "Hint: The 'Add broken feature' commit should be reverted" -ForegroundColor Yellow
Write-Host " Use: git revert <commit-hash-of-broken-feature>" -ForegroundColor Yellow
Set-Location ..
exit 1
}
# Verify the revert commit specifically reverted the "Add broken feature" commit
$revertCommitMessage = git log --grep="Revert" --pretty=format:"%s" -n 1 2>$null
if ($revertCommitMessage -notmatch "broken feature") {
Write-Host "[FAIL] The revert commit should mention 'broken feature'." -ForegroundColor Red
Write-Host "Hint: Make sure you reverted the correct commit (the one that added square_root)" -ForegroundColor Yellow
Set-Location ..
exit 1
}
Write-Host "[PASS] shared-feature branch correctly reverted!" -ForegroundColor Green
# Success!
Write-Host "`n========================================" -ForegroundColor Green
Write-Host "SUCCESS! Challenge completed!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host "`nYou have successfully:" -ForegroundColor Cyan
Write-Host "- Used 'git reset' on local-feature (private branch)" -ForegroundColor White
Write-Host " Removed the bad commit completely from history" -ForegroundColor White
Write-Host "- Used 'git revert' on shared-feature (public branch)" -ForegroundColor White
Write-Host " Created a new commit that undoes the bad one" -ForegroundColor White
Write-Host " Preserved all history and subsequent commits" -ForegroundColor White
Write-Host "`nYou now understand when to use reset vs revert!" -ForegroundColor Green
Write-Host "`nKey takeaway:" -ForegroundColor Yellow
Write-Host "- Reset rewrites history (use only on private commits)" -ForegroundColor White
Write-Host "- Revert preserves history (safe for shared commits)`n" -ForegroundColor White
Set-Location ..
exit 0

View File

@@ -0,0 +1,638 @@
# Module 05: Git Revert - Safe Undoing
## About This Module
Welcome to Module 05, where you'll learn the **safe, team-friendly way to undo changes** in Git. Unlike destructive commands that erase history, `git revert` creates new commits that undo previous changes while preserving the complete project history.
**Why revert is important:**
- ✅ Safe for shared/pushed commits
- ✅ Preserves complete history and audit trail
- ✅ Transparent to your team
- ✅ Can be undone itself if needed
- ✅ Works with any commit in history
**Key principle:** Revert doesn't erase mistakes—it documents how you fixed them.
## Learning Objectives
By completing this module, you will:
1. Revert regular commits safely while preserving surrounding changes
2. Revert merge commits using the `-m` flag
3. Understand merge commit parent numbering
4. Handle the re-merge problem that occurs after reverting merges
5. Revert multiple commits at once
6. Know when to use revert vs. other undo strategies
## Prerequisites
Before starting this module, you should be comfortable with:
- Creating commits (`git commit`)
- Viewing commit history (`git log`)
- Understanding branches and merging (Module 03)
## Setup
Run the setup script to create the challenge environment:
```powershell
./setup.ps1
```
This creates a `challenge/` directory with three branches demonstrating different revert scenarios:
- `regular-revert` - Basic commit reversion
- `merge-revert` - Merge commit reversion
- `multi-revert` - Multiple commit reversion
## Challenge 1: Reverting a Regular Commit
### Scenario
You're working on a calculator application. A developer added a `divide` function that crashes when dividing by zero. The bug was discovered after subsequent commits were made, so you can't just delete it—you need to revert it while keeping the commits that came after.
### Your Task
1. Navigate to the challenge directory:
```bash
cd challenge
```
2. You should be on the `regular-revert` branch. View the commit history:
```bash
git log --oneline
```
3. Find the commit with the broken divide function (message: "Add broken divide function - needs to be reverted!")
4. Revert that specific commit:
```bash
git revert <commit-hash>
```
5. Git will open your editor for the revert commit message. The default message is fine—save and close.
### What to Observe
After reverting, check:
```bash
# View the new revert commit
git log --oneline
# Check that divide function is gone
cat calculator.py | grep "def divide" # Should return nothing
# Check that modulo function still exists (it came after the bad commit)
cat calculator.py | grep "def modulo" # Should find it
# Check that multiply function still exists (it came before the bad commit)
cat calculator.py | grep "def multiply" # Should find it
```
**Key insight:** Revert creates a new commit that undoes the changes from the target commit, but leaves all other commits intact.
### Understanding the Timeline
```
Before revert:
main.py (initial) → multiply (good) → divide (BAD) → modulo (good)
We want to undo THIS
After revert:
main.py (initial) → multiply (good) → divide (BAD) → modulo (good) → revert divide (new commit)
Removes divide, keeps modulo
```
The revert commit adds a new point in history that undoes the divide changes.
## Challenge 2: Reverting a Merge Commit
### Scenario
Your team merged a `feature-auth` branch that added authentication functionality. After deployment, you discovered the authentication system has critical security issues. You need to revert the entire merge while the security team redesigns the feature.
**This is different from reverting a regular commit!** Merge commits have **two parents**, so you must tell Git which parent to keep.
### Understanding Merge Commit Parents
When you merge a feature branch into main:
```
feature-auth (parent 2)
C---D
/ \
A---B-----M ← Merge commit (has TWO parents)
parent 1 (main)
```
The merge commit `M` has:
- **Parent 1**: The branch you merged INTO (main)
- **Parent 2**: The branch you merged FROM (feature-auth)
When reverting a merge, you must specify which parent to keep using the `-m` flag:
- `-m 1` means "keep parent 1" (main) - **Most common**
- `-m 2` means "keep parent 2" (feature-auth) - Rare
**In practice:** You almost always use `-m 1` to keep the main branch and undo the feature branch changes.
### Your Task
1. Switch to the merge-revert branch:
```bash
git switch merge-revert
```
2. View the commit history and find the merge commit:
```bash
git log --oneline --graph
```
Look for: "Merge feature-auth branch"
3. Revert the merge commit using `-m 1`:
```bash
git revert -m 1 <merge-commit-hash>
```
**Explanation:**
- `-m 1` tells Git to keep parent 1 (main branch)
- 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
# Verify calculator.py no longer imports auth
cat calculator.py | grep "from auth" # Should return nothing
```
### What Happens Without -m?
If you try to revert a merge commit without the `-m` flag:
```bash
git revert <merge-commit-hash>
# Error: commit <hash> is a merge but no -m option was given
```
Git doesn't know which parent you want to keep, so it refuses to proceed.
### The Re-Merge Problem
**Important gotcha:** After reverting a merge, you **cannot simply re-merge** the same branch!
Here's why:
```
Initial merge:
A---B---M (merged feature-auth)
All changes from feature-auth are now in main
After revert:
A---B---M---R (reverted merge)
Changes removed, but Git remembers they were merged
Attempting to re-merge:
A---B---M---R---M2 (try to merge feature-auth again)
Git thinks: "I already merged these commits,
nothing new to add!" (Empty merge)
```
**Solutions if you need to re-merge:**
1. **Revert the revert** (recommended):
```bash
git revert <revert-commit-hash>
```
This brings back all the feature-auth changes.
2. **Cherry-pick new commits** from the feature branch:
```bash
git cherry-pick <new-commits>
```
3. **Merge with --no-ff** and resolve conflicts manually (advanced).
### When to Revert Merges
Revert merge commits when:
- ✅ Feature causes production issues
- ✅ Need to temporarily remove a feature
- ✅ Discovered critical bugs after merging
- ✅ Security issues require immediate rollback
Don't revert merges when:
- ❌ You just need to fix a small bug (fix it with a new commit instead)
- ❌ You plan to re-merge the same branch soon (use reset if local, or revert-the-revert later)
## Challenge 3: Reverting Multiple Commits
### Scenario
Two separate commits added broken mathematical functions (`square_root` and `logarithm`). Both have critical bugs and need to be removed. You can revert multiple commits at once.
### Your Task
1. Switch to the multi-revert branch:
```bash
git switch multi-revert
```
2. View the commit history:
```bash
git log --oneline
```
Find the two commits:
- "Add broken square_root - REVERT THIS!"
- "Add broken logarithm - REVERT THIS TOO!"
3. Revert both commits in one command:
```bash
git revert <commit-hash-1> <commit-hash-2>
```
**Important:** List commits from **oldest to newest** for cleanest history.
Alternatively, revert them one at a time:
```bash
git revert <commit-hash-1>
git revert <commit-hash-2>
```
4. Git will prompt for a commit message for each revert. Accept the defaults.
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
# Check that good functions remain
cat calculator.py | grep "def power" # Should find it
cat calculator.py | grep "def absolute" # Should find it
```
### Multi-Revert Strategies
**Reverting a range of commits:**
```bash
# Revert commits from A to B (inclusive)
git revert A^..B
# Example: Revert last 3 commits
git revert HEAD~3..HEAD
```
**Reverting without auto-commit:**
```bash
# Stage revert changes without committing
git revert --no-commit <commit-hash>
# Review changes
git diff --staged
# Commit when ready
git commit
```
This is useful when reverting multiple commits and you want one combined revert commit.
## Verification
Verify your solutions by running the verification script:
```bash
cd .. # Return to module directory
./verify.ps1
```
The script checks that:
- ✅ Revert commits were created (not destructive deletion)
- ✅ Bad code is removed
- ✅ Good code before and after is preserved
- ✅ Merge commits still exist in history
- ✅ Proper use of `-m` flag for merge reverts
## Command Reference
### Basic Revert
```bash
# Revert a specific commit
git revert <commit-hash>
# Revert the most recent commit
git revert HEAD
# Revert the second-to-last commit
git revert HEAD~1
```
### Merge Commit Revert
```bash
# Revert a merge commit (keep parent 1)
git revert -m 1 <merge-commit-hash>
# Revert a merge commit (keep parent 2) - rare
git revert -m 2 <merge-commit-hash>
```
### Multiple Commits
```bash
# Revert multiple specific commits
git revert <hash1> <hash2> <hash3>
# Revert a range of commits (oldest^..newest)
git revert <oldest-hash>^..<newest-hash>
# Revert last 3 commits
git revert HEAD~3..HEAD
```
### Revert Options
```bash
# Revert but don't commit automatically
git revert --no-commit <commit-hash>
# Revert and edit the commit message
git revert --edit <commit-hash>
# Revert without opening editor (use default message)
git revert --no-edit <commit-hash>
# Abort a revert in progress (if conflicts)
git revert --abort
# Continue revert after resolving conflicts
git revert --continue
```
## When to Use Git Revert
Use `git revert` when:
- ✅ **Commits are already pushed** - Safe for shared history
- ✅ **Working in a team** - Transparent to everyone
- ✅ **Need audit trail** - Shows what was undone and why
- ✅ **Public repositories** - Can't rewrite public history
- ✅ **Undoing old commits** - Can revert commits from weeks ago
- ✅ **Production hotfixes** - Safe emergency rollback
**Golden Rule:** If others might have your commits, use revert.
## When NOT to Use Git Revert
Consider alternatives when:
- ❌ **Commits are still local** - Use `git reset` instead (Module 06)
- ❌ **Just want to edit a commit** - Use `git commit --amend`
- ❌ **Haven't pushed yet** - Reset is cleaner for local cleanup
- ❌ **Need to combine commits** - Use interactive rebase
- ❌ **Reverting creates complex conflicts** - Might need manual fix forward
## Revert vs. Reset vs. Rebase
| Command | History | Safety | Use Case |
|---------|---------|--------|----------|
| **revert** | Preserves | ✅ Safe | Undo pushed commits |
| **reset** | Erases | ⚠️ Dangerous | Clean up local commits |
| **rebase** | Rewrites | ⚠️ Dangerous | Polish commit history |
**This module teaches revert.** You'll learn reset in Module 06.
## Handling Revert Conflicts
Sometimes reverting causes conflicts if subsequent changes touched the same code:
```bash
# Start revert
git revert <commit-hash>
# If conflicts occur:
# Conflict in calculator.py
# CONFLICT (content): Merge conflict in calculator.py
```
**To resolve:**
1. Open conflicted files and fix conflicts (look for `<<<<<<<` markers)
2. Stage resolved files:
```bash
git add <resolved-files>
```
3. Continue the revert:
```bash
git revert --continue
```
Or abort if you change your mind:
```bash
git revert --abort
```
## Common Mistakes
### 1. Forgetting -m for Merge Commits
```bash
# ❌ Wrong - will fail
git revert <merge-commit>
# ✅ Correct
git revert -m 1 <merge-commit>
```
### 2. Trying to Re-Merge After Revert
```bash
# After reverting a merge:
git revert -m 1 <merge-commit>
# ❌ This won't work as expected
git merge feature-branch # Empty merge!
# ✅ Do this instead
git revert <the-revert-commit> # Revert the revert
```
### 3. Using Reset on Pushed Commits
```bash
# ❌ NEVER do this with pushed commits
git reset --hard HEAD~3
# ✅ Do this instead
git revert HEAD~3..HEAD
```
### 4. Reverting Commits in Wrong Order
When reverting multiple related commits, revert from newest to oldest:
```bash
# If you have: A → B → C (and C depends on B)
# ✅ Correct order
git revert C
git revert B
# ❌ Wrong order (may cause conflicts)
git revert B # Conflict! C still references B
git revert C
```
## Best Practices
1. **Write clear revert messages:**
```bash
git revert <hash> -m "Revert authentication - security issue #1234"
```
2. **Link to issue tracking:**
```
Revert "Add new payment system"
This reverts commit abc123.
Critical bug in payment processing.
See bug tracker: ISSUE-1234
```
3. **Test after reverting:**
- Run your test suite
- Verify the application still works
- Check no unintended changes occurred
4. **Communicate with team:**
- Announce reverts in team chat
- Explain why the revert was necessary
- Provide timeline for re-introducing the feature
5. **Keep reverts focused:**
- Revert the minimum necessary
- Don't bundle multiple unrelated reverts
- One problem = one revert commit
## Troubleshooting
### "Commit is a merge but no -m option was given"
**Problem:** Trying to revert a merge commit without `-m`.
**Solution:**
```bash
git revert -m 1 <merge-commit-hash>
```
### "Empty Revert / No Changes"
**Problem:** Revert doesn't seem to do anything.
**Possible causes:**
- Commit was already reverted
- Subsequent commits already undid the changes
- Wrong commit hash
**Solution:**
```bash
# Check what the commit actually changed
git show <commit-hash>
# Check if already reverted
git log --grep="Revert"
```
### "Conflicts During Revert"
**Problem:** Revert causes merge conflicts.
**Why:** Subsequent commits modified the same code.
**Solution:**
1. Manually resolve conflicts in affected files
2. `git add <resolved-files>`
3. `git revert --continue`
Or consider fixing forward with a new commit instead of reverting.
### "Can't Re-Merge After Reverting Merge"
**Problem:** After reverting a merge, re-merging the branch brings no changes.
**Solution:** Revert the revert commit:
```bash
# Find the revert commit
git log --oneline
# Revert the revert (brings changes back)
git revert <revert-commit-hash>
```
## Advanced: Revert Internals
Understanding what revert does under the hood:
```bash
# Revert creates a new commit with inverse changes
git revert <commit-hash>
# This is equivalent to:
git diff <commit-hash>^..<commit-hash> > changes.patch
patch -R < changes.patch # Apply in reverse
git add .
git commit -m "Revert '<original message>'"
```
**Key insight:** Revert computes the diff of the target commit, inverts it, and applies it as a new commit.
## Going Further
Now that you understand revert, you're ready for:
- **Module 06: Git Reset** - Learn the dangerous but powerful local history rewriting
- **Module 07: Git Stash** - Temporarily set aside uncommitted changes
- **Module 08: Multiplayer Git** - Collaborate with advanced workflows
## Summary
You've learned:
- ✅ `git revert` creates new commits that undo previous changes
- ✅ Revert is safe for shared/pushed commits
- ✅ Merge commits require `-m 1` or `-m 2` flag
- ✅ Parent 1 = branch merged into, Parent 2 = branch merged from
- ✅ Can't simply re-merge after reverting a merge
- ✅ Multiple commits can be reverted in one command
- ✅ Revert preserves complete history for audit trails
**The Golden Rule of Revert:** Use revert for any commit that might be shared with others.
## Next Steps
1. Complete all three challenge scenarios
2. Run `./verify.ps1` to check your solutions
3. Experiment with reverting different commits
4. Move on to Module 06: Git Reset (dangerous but powerful!)
---
**Need Help?**
- Review the command reference above
- Check the troubleshooting section
- Re-run `./setup.ps1` to start fresh
- Practice reverting in different orders to understand the behavior

View File

@@ -0,0 +1,24 @@
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Resets the Module 05 challenge environment to start fresh.
.DESCRIPTION
This script removes the challenge directory and re-runs setup.ps1
to create a fresh challenge environment.
#>
Write-Host "`n=== Resetting Module 05: Git Revert Challenge ===" -ForegroundColor Cyan
# Check if challenge directory exists
if (Test-Path "challenge") {
Write-Host "Removing existing challenge directory..." -ForegroundColor Yellow
Remove-Item -Recurse -Force "challenge"
Write-Host "[OK] Challenge directory removed" -ForegroundColor Green
} else {
Write-Host "[INFO] No existing challenge directory found" -ForegroundColor Yellow
}
# Run setup to create fresh environment
Write-Host "`nRunning setup to create fresh challenge environment..." -ForegroundColor Cyan
& "$PSScriptRoot/setup.ps1"

View File

@@ -0,0 +1,373 @@
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Sets up the Module 05 challenge environment for learning git revert.
.DESCRIPTION
This script creates a challenge directory with three branches demonstrating
different revert scenarios:
- regular-revert: Basic revert of a single bad commit
- merge-revert: Reverting a merge commit with -m flag
- multi-revert: Reverting multiple commits at once
#>
Write-Host "`n=== Setting up Module 05: Git Revert Challenge ===" -ForegroundColor Cyan
# Remove existing challenge directory if it exists
if (Test-Path "challenge") {
Write-Host "Removing existing challenge directory..." -ForegroundColor Yellow
Remove-Item -Recurse -Force "challenge"
}
# Create fresh challenge directory
Write-Host "Creating challenge directory..." -ForegroundColor Green
New-Item -ItemType Directory -Path "challenge" | Out-Null
Set-Location "challenge"
# Initialize Git repository
Write-Host "Initializing Git repository..." -ForegroundColor Green
git init | Out-Null
# Configure git for this repository
git config user.name "Workshop Student"
git config user.email "student@example.com"
# ============================================================================
# SCENARIO 1: Regular Revert (Basic)
# ============================================================================
Write-Host "`nScenario 1: Creating regular-revert branch..." -ForegroundColor Cyan
# Initial 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
"@
Set-Content -Path "calculator.py" -Value $calcContent
git add .
git commit -m "Initial calculator implementation" | Out-Null
# 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
def multiply(a, b):
"""Multiply two numbers."""
return a * b
"@
Set-Content -Path "calculator.py" -Value $calcContent
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
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
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!
def modulo(a, b):
"""Return remainder of a divided by b."""
return a % b
"@
Set-Content -Path "calculator.py" -Value $calcContent
git add .
git commit -m "Add modulo function" | Out-Null
Write-Host "[CREATED] regular-revert branch with bad divide commit" -ForegroundColor Green
# ============================================================================
# SCENARIO 2: Merge Revert (Merge Commit with -m flag)
# ============================================================================
Write-Host "`nScenario 2: Creating merge-revert scenario..." -ForegroundColor Cyan
# Switch back to main
git switch main | Out-Null
# Create merge-revert branch
git switch -c merge-revert | Out-Null
# Create a feature branch to merge
git switch -c feature-auth | Out-Null
# Add auth functionality
$authContent = @"
# auth.py - Authentication module
def login(username, password):
\"\"\"Login user.\"\"\"
print(f"Logging in {username}...")
return True
def logout(username):
\"\"\"Logout user.\"\"\"
print(f"Logging out {username}...")
return True
"@
Set-Content -Path "auth.py" -Value $authContent
git add .
git commit -m "Add authentication module" | Out-Null
# Add password validation
$authContent = @"
# auth.py - Authentication module
def validate_password(password):
\"\"\"Validate password strength.\"\"\"
return len(password) >= 8
def login(username, password):
\"\"\"Login user.\"\"\"
if not validate_password(password):
print("Password too weak!")
return False
print(f"Logging in {username}...")
return True
def logout(username):
\"\"\"Logout user.\"\"\"
print(f"Logging out {username}...")
return True
"@
Set-Content -Path "auth.py" -Value $authContent
git add .
git commit -m "Add password validation" | Out-Null
# Integrate auth into calculator (part of the feature branch)
$calcContent = @"
# calculator.py - Simple calculator
from auth import login
def add(a, b):
"""Add two numbers."""
return a + b
def subtract(a, b):
"""Subtract b from a."""
return a - b
def secure_divide(a, b, username):
"""Secure divide - requires authentication."""
if login(username, "password123"):
return a / b
return None
"@
Set-Content -Path "calculator.py" -Value $calcContent
git add .
git commit -m "Integrate auth into calculator" | Out-Null
# Switch back to merge-revert and merge feature-auth
git switch merge-revert | Out-Null
git merge feature-auth --no-ff -m "Merge feature-auth branch" | Out-Null
Write-Host "[CREATED] merge-revert branch with merge commit to revert" -ForegroundColor Green
# ============================================================================
# SCENARIO 3: Multi Revert (Multiple Bad Commits)
# ============================================================================
Write-Host "`nScenario 3: Creating multi-revert branch..." -ForegroundColor Cyan
# Switch back to main
git switch main | Out-Null
# Create multi-revert branch
git switch -c multi-revert | Out-Null
# Reset calculator to simple version
$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
"@
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
def power(a, b):
"""Raise a to the power of b."""
return a ** b
"@
Set-Content -Path "calculator.py" -Value $calcContent
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
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
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!
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
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!
def absolute(a):
"""Return absolute value of a."""
return abs(a)
"@
Set-Content -Path "calculator.py" -Value $calcContent
git add .
git commit -m "Add absolute value function" | Out-Null
Write-Host "[CREATED] multi-revert branch with two bad commits to revert" -ForegroundColor Green
# ============================================================================
# Return to regular-revert to start
# ============================================================================
git switch regular-revert | Out-Null
# Return to module directory
Set-Location ..
Write-Host "`n=== Setup Complete! ===" -ForegroundColor Green
Write-Host "`nThree revert scenarios have been created:" -ForegroundColor Cyan
Write-Host " 1. regular-revert - Revert a single bad commit (basic)" -ForegroundColor White
Write-Host " 2. merge-revert - Revert a merge commit with -m flag" -ForegroundColor White
Write-Host " 3. multi-revert - Revert multiple bad commits" -ForegroundColor White
Write-Host "`nYou are currently on the 'regular-revert' branch." -ForegroundColor Cyan
Write-Host "`nNext steps:" -ForegroundColor Cyan
Write-Host " 1. cd challenge" -ForegroundColor White
Write-Host " 2. Read the README.md for detailed instructions" -ForegroundColor White
Write-Host " 3. Complete each revert challenge" -ForegroundColor White
Write-Host " 4. Run '..\verify.ps1' to check your solutions" -ForegroundColor White
Write-Host ""

View File

@@ -0,0 +1,226 @@
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Verifies the Module 05 challenge solutions.
.DESCRIPTION
Checks that all three revert scenarios have been completed correctly:
- regular-revert: Single commit reverted
- merge-revert: Merge commit reverted with -m flag
- multi-revert: Multiple commits reverted
#>
Write-Host "`n=== Verifying Module 05: Git Revert Solutions ===" -ForegroundColor Cyan
$allChecksPassed = $true
$originalDir = Get-Location
# Check if challenge directory exists
if (-not (Test-Path "challenge")) {
Write-Host "[FAIL] Challenge directory not found. Run setup.ps1 first." -ForegroundColor Red
exit 1
}
Set-Location "challenge"
# Check if git repository exists
if (-not (Test-Path ".git")) {
Write-Host "[FAIL] Not a git repository. Run setup.ps1 first." -ForegroundColor Red
Set-Location $originalDir
exit 1
}
# ============================================================================
# SCENARIO 1: Regular Revert Verification
# ============================================================================
Write-Host "`n=== Scenario 1: Regular Revert ===" -ForegroundColor Cyan
git switch regular-revert 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Host "[FAIL] regular-revert branch not found" -ForegroundColor Red
$allChecksPassed = $false
} else {
# Check that a revert commit exists
$revertCommit = git log --oneline --grep="Revert" 2>$null
if ($revertCommit) {
Write-Host "[PASS] Revert commit found" -ForegroundColor Green
} else {
Write-Host "[FAIL] No revert commit found" -ForegroundColor Red
Write-Host "[HINT] Use: git revert <commit-hash>" -ForegroundColor Yellow
$allChecksPassed = $false
}
# Check that calculator.py exists
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
} else {
Write-Host "[FAIL] modulo function missing (should still exist)" -ForegroundColor Red
Write-Host "[HINT] Only revert the bad commit, not the good ones after it" -ForegroundColor Yellow
$allChecksPassed = $false
}
# Check that multiply function exists (should be preserved)
if ($calcContent -match "def multiply") {
Write-Host "[PASS] multiply function preserved (good commit before bad one)" -ForegroundColor Green
} else {
Write-Host "[FAIL] multiply function missing" -ForegroundColor Red
$allChecksPassed = $false
}
} else {
Write-Host "[FAIL] calculator.py not found" -ForegroundColor Red
$allChecksPassed = $false
}
}
# ============================================================================
# SCENARIO 2: Merge Revert Verification
# ============================================================================
Write-Host "`n=== Scenario 2: Merge Revert ===" -ForegroundColor Cyan
git switch merge-revert 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Host "[FAIL] merge-revert branch not found" -ForegroundColor Red
$allChecksPassed = $false
} else {
# Check that a revert commit for the merge exists
$revertMerge = git log --oneline --grep="Revert.*Merge" 2>$null
if ($revertMerge) {
Write-Host "[PASS] Merge revert commit found" -ForegroundColor Green
} else {
Write-Host "[FAIL] No merge revert commit found" -ForegroundColor Red
Write-Host "[HINT] Use: git revert -m 1 <merge-commit-hash>" -ForegroundColor Yellow
$allChecksPassed = $false
}
# Check that the original merge commit still exists (revert doesn't erase it)
$mergeCommit = git log --merges --oneline --grep="Merge feature-auth" 2>$null
if ($mergeCommit) {
Write-Host "[PASS] Original merge commit still in history (not erased)" -ForegroundColor Green
} else {
Write-Host "[INFO] Original merge commit not found (this is OK if you used a different approach)" -ForegroundColor Yellow
}
# Check that auth.py no longer exists or its effects are reverted
if (-not (Test-Path "auth.py")) {
Write-Host "[PASS] auth.py removed (merge reverted successfully)" -ForegroundColor Green
} else {
Write-Host "[INFO] auth.py still exists (check if merge was fully reverted)" -ForegroundColor Yellow
}
# Check that calculator.py exists
if (Test-Path "calculator.py") {
$calcContent = Get-Content "calculator.py" -Raw
# After reverting the merge, calculator shouldn't import auth
if ($calcContent -notmatch "from auth import") {
Write-Host "[PASS] Auth integration reverted from calculator.py" -ForegroundColor Green
} else {
Write-Host "[FAIL] calculator.py still imports auth (merge not fully reverted)" -ForegroundColor Red
Write-Host "[HINT] Reverting the merge should remove the auth integration" -ForegroundColor Yellow
$allChecksPassed = $false
}
}
}
# ============================================================================
# SCENARIO 3: Multi Revert Verification
# ============================================================================
Write-Host "`n=== Scenario 3: Multi Revert ===" -ForegroundColor Cyan
git switch multi-revert 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Host "[FAIL] multi-revert branch not found" -ForegroundColor Red
$allChecksPassed = $false
} else {
# Count revert commits
$revertCommits = git log --oneline --grep="Revert" 2>$null
$revertCount = ($revertCommits | Measure-Object).Count
if ($revertCount -ge 2) {
Write-Host "[PASS] Found $revertCount revert commits (expected at least 2)" -ForegroundColor Green
} else {
Write-Host "[FAIL] Found only $revertCount revert commit(s), need at least 2" -ForegroundColor Red
Write-Host "[HINT] Revert both bad commits: git revert <commit1> <commit2>" -ForegroundColor Yellow
$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
} else {
Write-Host "[FAIL] power function missing (should still exist)" -ForegroundColor Red
$allChecksPassed = $false
}
# Check that absolute function still exists (good commit after bad ones)
if ($calcContent -match "def absolute") {
Write-Host "[PASS] absolute function preserved" -ForegroundColor Green
} else {
Write-Host "[FAIL] absolute function missing (should still exist)" -ForegroundColor Red
$allChecksPassed = $false
}
} else {
Write-Host "[FAIL] calculator.py not found" -ForegroundColor Red
$allChecksPassed = $false
}
}
Set-Location $originalDir
# Final summary
Write-Host ""
if ($allChecksPassed) {
Write-Host "=========================================" -ForegroundColor Green
Write-Host " CONGRATULATIONS! ALL SCENARIOS PASSED!" -ForegroundColor Green
Write-Host "=========================================" -ForegroundColor Green
Write-Host "`nYou've mastered git revert!" -ForegroundColor Cyan
Write-Host "You now understand:" -ForegroundColor Cyan
Write-Host " ✓ Reverting regular commits safely" -ForegroundColor White
Write-Host " ✓ Reverting merge commits with -m flag" -ForegroundColor White
Write-Host " ✓ Reverting multiple commits at once" -ForegroundColor White
Write-Host " ✓ Preserving history while undoing changes" -ForegroundColor White
Write-Host "`nReady for Module 06: Git Reset!" -ForegroundColor Green
Write-Host ""
exit 0
} else {
Write-Host "[SUMMARY] Some checks failed. Review the hints above and try again." -ForegroundColor Red
Write-Host "[INFO] You can run this verification script as many times as needed." -ForegroundColor Yellow
Write-Host ""
exit 1
}

View File

@@ -0,0 +1,717 @@
# Module 06: Git Reset - Dangerous History Rewriting
## ⚠️ CRITICAL SAFETY WARNING ⚠️
**Git reset is DESTRUCTIVE and DANGEROUS when misused!**
Before using `git reset`, always ask yourself:
```
Have I pushed these commits to a remote repository?
├─ YES → ❌ DO NOT USE RESET!
│ Use git revert instead (Module 05)
│ Rewriting pushed history breaks collaboration!
└─ NO → ✅ Proceed with reset (local cleanup only)
Choose your mode carefully:
--soft (safest), --mixed (moderate), --hard (DANGEROUS)
```
**The Golden Rule:** NEVER reset commits that have been pushed/shared.
## About This Module
Welcome to Module 06, where you'll learn the powerful but dangerous `git reset` command. Unlike `git revert` (Module 05) which safely creates new commits, **reset erases commits from history**.
**Why reset exists:**
- ✅ Clean up messy local commit history before pushing
- ✅ Undo commits you haven't shared yet
- ✅ Unstage files from the staging area
- ✅ Recover from mistakes (with reflog)
**Why reset is dangerous:**
- ⚠️ Erases commits permanently (without reflog)
- ⚠️ Breaks repositories if used on pushed commits
- ⚠️ Can lose work if used incorrectly
- ⚠️ Confuses teammates if they have your commits
**Key principle:** Reset is for polishing LOCAL history before sharing.
## Learning Objectives
By completing this module, you will:
1. Understand the three reset modes: --soft, --mixed, --hard
2. Reset commits while keeping changes staged (--soft)
3. Reset commits and unstage changes (--mixed)
4. Reset commits and discard everything (--hard)
5. Know when reset is appropriate (local only!)
6. Understand when to use revert instead
7. Use reflog to recover from mistakes
## Prerequisites
Before starting this module, you should:
- Be comfortable with commits and staging (`git add`, `git commit`)
- Understand `git revert` from Module 05
- **Know the difference between local and pushed commits!**
## Setup
Run the setup script to create the challenge environment:
```powershell
./setup.ps1
```
This creates a `challenge/` directory with three branches demonstrating different reset modes:
- `soft-reset` - Reset with --soft (keep changes staged)
- `mixed-reset` - Reset with --mixed (unstage changes)
- `hard-reset` - Reset with --hard (discard everything)
**Remember:** These are all LOCAL commits that have NEVER been pushed!
## Understanding Reset Modes
Git reset has three modes that control what happens to your changes:
| Mode | Commits | Staging Area | Working Directory |
|------|---------|--------------|-------------------|
| **--soft** | ✂️ Removed | ✅ Kept (staged) | ✅ Kept |
| **--mixed** (default) | ✂️ Removed | ✂️ Cleared | ✅ Kept (unstaged) |
| **--hard** | ✂️ Removed | ✂️ Cleared | ✂️ **LOST!** |
**Visual explanation:**
```
Before reset (3 commits):
A → B → C → HEAD
After git reset --soft HEAD~1:
A → B → HEAD
C's changes are staged
After git reset --mixed HEAD~1 (or just git reset HEAD~1):
A → B → HEAD
C's changes are unstaged (in working directory)
After git reset --hard HEAD~1:
A → B → HEAD
C's changes are GONE (discarded completely!)
```
## Challenge 1: Soft Reset (Safest)
### Scenario
You committed "feature C" but immediately realized the implementation is wrong. You want to undo the commit but keep the changes staged so you can edit and re-commit them properly.
**Use case:** Fixing the last commit's message or contents.
### Your Task
1. Navigate to the challenge directory:
```bash
cd challenge
```
2. You should be on the `soft-reset` branch. View the commits:
```bash
git log --oneline
```
You should see:
- "Add feature C - needs better implementation!"
- "Add feature B"
- "Add feature A"
- "Initial project setup"
3. View the current state:
```bash
git status
# Should be clean
```
4. Reset the last commit with --soft:
```bash
git reset --soft HEAD~1
```
5. Check what happened:
```bash
# Commit is gone
git log --oneline
# Should only show 3 commits now (feature C commit removed)
# Changes are still staged
git status
# Should show "Changes to be committed"
# View the staged changes
git diff --cached
# Should show feature C code ready to be re-committed
```
### What to Observe
After `--soft` reset:
- ✅ Commit removed from history
- ✅ Changes remain in staging area
- ✅ Working directory unchanged
- ✅ Ready to edit and re-commit
**When to use --soft:**
- Fix the last commit message (though `commit --amend` is simpler)
- Combine multiple commits into one
- Re-do a commit with better changes
## Challenge 2: Mixed Reset (Default, Moderate)
### Scenario
You committed two experimental features that aren't ready. You want to remove both commits and have the changes back in your working directory (unstaged) so you can review and selectively re-commit them.
**Use case:** Undoing commits and starting over with more careful staging.
### Your Task
1. Switch to the mixed-reset branch:
```bash
git switch mixed-reset
```
2. View the commits:
```bash
git log --oneline
```
You should see:
- "Add debug mode - REMOVE THIS TOO!"
- "Add experimental feature X - REMOVE THIS!"
- "Add logging system"
- "Add application lifecycle"
3. Reset the last TWO commits (default is --mixed):
```bash
git reset HEAD~2
# This is equivalent to: git reset --mixed HEAD~2
```
4. Check what happened:
```bash
# Commits are gone
git log --oneline
# Should only show 2 commits (lifecycle + logging)
# NO staged changes
git diff --cached
# Should be empty
# Changes are in working directory (unstaged)
git status
# Should show "Changes not staged for commit"
# View the unstaged changes
git diff
# Should show experimental and debug code
```
### What to Observe
After `--mixed` reset (the default):
- ✅ Commits removed from history
- ✅ Staging area cleared
- ✅ Changes moved to working directory (unstaged)
- ✅ Can selectively stage and re-commit parts
**When to use --mixed (default):**
- Undo commits and start over with clean staging
- Split one large commit into multiple smaller ones
- Review changes before re-committing
- Most common reset mode for cleanup
## Challenge 3: Hard Reset (MOST DANGEROUS!)
### ⚠️ EXTREME CAUTION REQUIRED ⚠️
**This will PERMANENTLY DELETE your work!**
Only use `--hard` when you're absolutely sure you want to throw away changes.
### Scenario
You committed completely broken code that you want to discard entirely. There's no salvaging it—you just want it gone.
**Use case:** Throwing away failed experiments or completely wrong code.
### Your Task
1. Switch to the hard-reset branch:
```bash
git switch hard-reset
```
2. View the commits and the broken code:
```bash
git log --oneline
# Shows "Add broken helper D - DISCARD COMPLETELY!"
cat utils.py
# Shows the broken helper_d function
```
3. Reset the last commit with --hard:
```bash
git reset --hard HEAD~1
```
**WARNING:** This will permanently discard all changes from that commit!
4. Check what happened:
```bash
# Commit is gone
git log --oneline
# Should only show 2 commits
# NO staged changes
git diff --cached
# Empty
# NO unstaged changes
git diff
# Empty
# Working directory clean
git status
# "nothing to commit, working tree clean"
# File doesn't have broken code
cat utils.py
# helper_d is completely gone
```
### What to Observe
After `--hard` reset:
- ✅ Commit removed from history
- ✅ Staging area cleared
- ✅ Working directory reset to match
- ⚠️ All changes from that commit PERMANENTLY DELETED
**When to use --hard:**
- Discarding failed experiments completely
- Throwing away work you don't want (CAREFUL!)
- Cleaning up after mistakes (use reflog to recover if needed)
- Resetting to a known good state
**⚠️ WARNING:** Files in the discarded commit are NOT gone forever—they're still in reflog for about 90 days. See "Recovery with Reflog" section below.
## Understanding HEAD~N Syntax
When resetting, you specify where to reset to:
```bash
# Reset to the commit before HEAD
git reset HEAD~1
# Reset to 2 commits before HEAD
git reset HEAD~2
# Reset to 3 commits before HEAD
git reset HEAD~3
# Reset to a specific commit hash
git reset abc123
# Reset to a branch
git reset main
```
**Visualization:**
```
HEAD~3 HEAD~2 HEAD~1 HEAD
↓ ↓ ↓ ↓
A → B → C → D → E
Current commit
```
- `git reset HEAD~1` moves HEAD from E to D
- `git reset HEAD~2` moves HEAD from E to C
- `git reset abc123` moves HEAD to that specific commit
## Verification
Verify your solutions by running the verification script:
```bash
cd .. # Return to module directory
./verify.ps1
```
The script checks that:
- ✅ Commits were reset (count decreased)
- ✅ --soft: Changes remain staged
- ✅ --mixed: Changes are unstaged
- ✅ --hard: Everything is clean
## Recovery with Reflog
**Good news:** Even `--hard` reset doesn't immediately destroy commits!
Git keeps a "reflog" (reference log) of where HEAD has been for about 90 days. You can use this to recover "lost" commits.
### How to Recover from a Reset
1. View the reflog:
```bash
git reflog
```
Output example:
```
abc123 HEAD@{0}: reset: moving to HEAD~1
def456 HEAD@{1}: commit: Add broken helper D
...
```
2. Find the commit you want to recover (def456 in this example)
3. Reset back to it:
```bash
git reset def456
# Or use the reflog reference:
git reset HEAD@{1}
```
4. Your "lost" commit is back!
### Reflog Safety Net
**Important:**
- Reflog entries expire after ~90 days (configurable)
- Reflog is LOCAL to your repository (not shared)
- `git gc` can clean up old reflog entries
- If you really lose a commit, check reflog first!
**Pro tip:** Before doing dangerous operations, note your current commit hash:
```bash
git log --oneline | head -1
# abc123 Current work
```
## When to Use Git Reset
Use `git reset` when:
- ✅ **Commits are LOCAL only** (never pushed)
- ✅ **Cleaning up messy history** before sharing
- ✅ **Undoing recent commits** you don't want
- ✅ **Combining commits** into one clean commit
- ✅ **Unstaging files** (mixed mode)
- ✅ **Polishing commit history** before pull request
**Golden Rule:** Only reset commits that are local to your machine!
## When NOT to Use Git Reset
DO NOT use `git reset` when:
- ❌ **Commits are pushed/shared** with others
- ❌ **Teammates have your commits** (breaks their repos)
- ❌ **In public repositories** (use revert instead)
- ❌ **Unsure if pushed** (check `git log origin/main`)
- ❌ **On main/master branch** after push
- ❌ **Need audit trail** of changes
**Use git revert instead** (Module 05) for pushed commits!
## Decision Tree: Reset vs Revert
```
Need to undo a commit?
├─ Have you pushed this commit?
│ │
│ ├─ YES → Use git revert (Module 05)
│ │ Safe for shared history
│ │ Preserves complete audit trail
│ │
│ └─ NO → Can use git reset (local only)
│ │
│ ├─ Want to keep changes?
│ │ │
│ │ ├─ Keep staged → git reset --soft
│ │ └─ Keep unstaged → git reset --mixed
│ │
│ └─ Discard everything? → git reset --hard
│ (CAREFUL!)
```
## Reset vs Revert vs Rebase
| Command | History | Safety | Use Case |
|---------|---------|--------|----------|
| **reset** | Erases | ⚠️ Dangerous | Local cleanup before push |
| **revert** | Preserves | ✅ Safe | Undo pushed commits |
| **rebase** | Rewrites | ⚠️ Dangerous | Polish history before push |
**This module teaches reset.** You learned revert in Module 05.
## Command Reference
### Basic Reset
```bash
# Reset last commit, keep changes staged
git reset --soft HEAD~1
# Reset last commit, unstage changes (default)
git reset HEAD~1
git reset --mixed HEAD~1 # Same as above
# Reset last commit, discard everything (DANGEROUS!)
git reset --hard HEAD~1
# Reset multiple commits
git reset --soft HEAD~3 # Last 3 commits
# Reset to specific commit
git reset --soft abc123
```
### Unstaging Files
```bash
# Unstage a specific file (common use of reset)
git reset HEAD filename.txt
# Unstage all files
git reset HEAD .
# This is the same as:
git restore --staged filename.txt # Modern syntax
```
### Reflog and Recovery
```bash
# View reflog
git reflog
# Recover from reset
git reset --hard HEAD@{1}
git reset --hard abc123
```
### Check Before Reset
```bash
# Check if commits are pushed
git log origin/main..HEAD
# If output is empty, commits are pushed (DO NOT RESET)
# If output shows commits, they're local (safe to reset)
# Another way to check
git log --oneline --graph --all
# Look for origin/main marker
```
## Common Mistakes
### 1. Resetting Pushed Commits
```bash
# ❌ NEVER do this if you've pushed!
git push
# ... time passes ...
git reset --hard HEAD~3 # BREAKS teammate repos!
# ✅ Do this instead
git revert HEAD~3..HEAD # Safe for shared history
```
### 2. Using --hard Without Thinking
```bash
# ❌ Dangerous - loses work!
git reset --hard HEAD~1
# ✅ Better - keep changes to review
git reset --mixed HEAD~1
# Now you can review changes and decide
```
### 3. Resetting Without Checking If Pushed
```bash
# ❌ Risky - are these commits pushed?
git reset HEAD~5
# ✅ Check first
git log origin/main..HEAD # Local commits only
git reset HEAD~5 # Now safe if output showed commits
```
### 4. Forgetting Reflog Exists
```bash
# ❌ Panic after accidental --hard reset
# "I lost my work!"
# ✅ Check reflog first!
git reflog # Find the "lost" commit
git reset --hard HEAD@{1} # Recover it
```
## Best Practices
1. **Always check if commits are pushed before reset:**
```bash
git log origin/main..HEAD
```
2. **Prefer --mixed over --hard:**
- You can always discard changes later
- Hard to recover if you use --hard by mistake
3. **Commit often locally, reset before push:**
- Make many small local commits
- Reset/squash into clean commits before pushing
4. **Use descriptive commit messages even for local commits:**
- Helps when reviewing before reset
- Useful when checking reflog
5. **Know your escape hatch:**
```bash
git reflog # Your safety net!
```
6. **Communicate with team:**
- NEVER reset shared branches (main, develop, etc.)
- Only reset your personal feature branches
- Only before pushing!
## Troubleshooting
### "I accidentally reset with --hard and lost work!"
**Solution:** Check reflog:
```bash
git reflog
# Find the commit before your reset
git reset --hard HEAD@{1} # Or the commit hash
```
**Prevention:** Always use --mixed first, then discard if really needed.
### "I reset but teammates still have my commits"
**Problem:** You reset and pushed with --force after they pulled.
**Impact:** Their repository is now broken/inconsistent.
**Solution:** Communicate! They need to:
```bash
git fetch
git reset --hard origin/main # Or whatever branch
```
**Prevention:** NEVER reset pushed commits!
### "Reset didn't do what I expected"
**Issue:** Wrong mode or wrong HEAD~N count.
**Solution:** Check current state:
```bash
git status
git diff
git diff --cached
git log --oneline
```
Undo the reset:
```bash
git reflog
git reset HEAD@{1} # Go back to before your reset
```
### "Can't reset - 'fatal: ambiguous argument HEAD~1'"
**Issue:** No commits to reset (probably first commit).
**Solution:** You can't reset before the first commit. If you want to remove the first commit entirely:
```bash
rm -rf .git # Nuclear option - deletes entire repo
git init # Start over
```
## Advanced: Reset Internals
Understanding what reset does under the hood:
```bash
# Reset moves the branch pointer
# Before:
main → A → B → C (HEAD)
# After git reset --soft HEAD~1:
main → A → B (HEAD)
C still exists in reflog, just not in branch history
# The commit object C is still in .git/objects
# It's just unreachable from any branch
```
**Key insight:** Reset moves the HEAD and branch pointers backward. The commits still exist temporarily in reflog until garbage collection.
## Going Further
Now that you understand reset, you're ready for:
- **Module 07: Git Stash** - Temporarily save uncommitted work
- **Module 08: Multiplayer Git** - Collaborate with complex workflows
- **Interactive Rebase** - Advanced history polishing (beyond this workshop)
## Summary
You've learned:
- ✅ `git reset` rewrites history by moving HEAD backward
- ✅ `--soft` keeps changes staged (safest)
- ✅ `--mixed` (default) unstages changes
- ✅ `--hard` discards everything (most dangerous)
- ✅ NEVER reset pushed/shared commits
- ✅ Use reflog to recover from mistakes
- ✅ Check if commits are pushed before resetting
- ✅ Use revert (Module 05) for shared commits
**The Critical Rule:** Reset is for LOCAL commits ONLY. Once you push, use revert!
## Next Steps
1. Complete all three challenge scenarios
2. Run `./verify.ps1` to check your solutions
3. Practice checking if commits are pushed before reset
4. Move on to Module 07: Git Stash
---
**⚠️ FINAL REMINDER ⚠️**
**Before any `git reset` command, ask yourself:**
> "Have I pushed these commits?"
If YES → Use `git revert` instead!
If NO → Proceed carefully, choose the right mode.
**When in doubt, use --mixed instead of --hard!**

View File

@@ -0,0 +1,24 @@
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Resets the Module 06 challenge environment to start fresh.
.DESCRIPTION
This script removes the challenge directory and re-runs setup.ps1
to create a fresh challenge environment.
#>
Write-Host "`n=== Resetting Module 06: Git Reset Challenge ===" -ForegroundColor Cyan
# Check if challenge directory exists
if (Test-Path "challenge") {
Write-Host "Removing existing challenge directory..." -ForegroundColor Yellow
Remove-Item -Recurse -Force "challenge"
Write-Host "[OK] Challenge directory removed" -ForegroundColor Green
} else {
Write-Host "[INFO] No existing challenge directory found" -ForegroundColor Yellow
}
# Run setup to create fresh environment
Write-Host "`nRunning setup to create fresh challenge environment..." -ForegroundColor Cyan
& "$PSScriptRoot/setup.ps1"

View File

@@ -0,0 +1,348 @@
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Sets up the Module 06 challenge environment for learning git reset.
.DESCRIPTION
This script creates a challenge directory with three branches demonstrating
different reset scenarios:
- soft-reset: Reset with --soft (keeps changes staged)
- mixed-reset: Reset with --mixed (unstages changes)
- hard-reset: Reset with --hard (discards everything) + reflog recovery
#>
Write-Host "`n=== Setting up Module 06: Git Reset Challenge ===" -ForegroundColor Cyan
Write-Host "⚠️ WARNING: Git reset is DANGEROUS - use with extreme caution! ⚠️" -ForegroundColor Red
# Remove existing challenge directory if it exists
if (Test-Path "challenge") {
Write-Host "Removing existing challenge directory..." -ForegroundColor Yellow
Remove-Item -Recurse -Force "challenge"
}
# Create fresh challenge directory
Write-Host "Creating challenge directory..." -ForegroundColor Green
New-Item -ItemType Directory -Path "challenge" | Out-Null
Set-Location "challenge"
# Initialize Git repository
Write-Host "Initializing Git repository..." -ForegroundColor Green
git init | Out-Null
# Configure git for this repository
git config user.name "Workshop Student"
git config user.email "student@example.com"
# ============================================================================
# Create initial commit (shared by all scenarios)
# ============================================================================
$readmeContent = @"
# Git Reset Practice
This repository contains practice scenarios for learning git reset.
"@
Set-Content -Path "README.md" -Value $readmeContent
git add .
git commit -m "Initial commit" | Out-Null
# ============================================================================
# SCENARIO 1: Soft Reset (--soft)
# ============================================================================
Write-Host "`nScenario 1: Creating soft-reset branch..." -ForegroundColor Cyan
# Create soft-reset branch from initial commit
git switch -c soft-reset | Out-Null
# Build up scenario 1 commits
$projectContent = @"
# project.py - Main project file
def initialize():
"""Initialize the project."""
print("Project initialized")
def main():
initialize()
print("Running application...")
"@
Set-Content -Path "project.py" -Value $projectContent
git add .
git commit -m "Initial project setup" | Out-Null
# Good commit: Add feature A
$projectContent = @"
# project.py - Main project file
def initialize():
"""Initialize the project."""
print("Project initialized")
def feature_a():
"""Feature A implementation."""
print("Feature A is working")
def main():
initialize()
feature_a()
print("Running application...")
"@
Set-Content -Path "project.py" -Value $projectContent
git add .
git commit -m "Add feature A" | Out-Null
# Good commit: Add feature B
$projectContent = @"
# project.py - Main project file
def initialize():
"""Initialize the project."""
print("Project initialized")
def feature_a():
"""Feature A implementation."""
print("Feature A is working")
def feature_b():
"""Feature B implementation."""
print("Feature B is working")
def main():
initialize()
feature_a()
feature_b()
print("Running application...")
"@
Set-Content -Path "project.py" -Value $projectContent
git add .
git commit -m "Add feature B" | Out-Null
# BAD commit: Add feature C (wrong implementation)
$projectContent = @"
# project.py - Main project file
def initialize():
"""Initialize the project."""
print("Project initialized")
def feature_a():
"""Feature A implementation."""
print("Feature A is working")
def feature_b():
"""Feature B implementation."""
print("Feature B is working")
def feature_c():
"""Feature C implementation - WRONG!"""
print("Feature C has bugs!") # This needs to be re-implemented
def main():
initialize()
feature_a()
feature_b()
feature_c()
print("Running application...")
"@
Set-Content -Path "project.py" -Value $projectContent
git add .
git commit -m "Add feature C - needs better implementation!" | Out-Null
Write-Host "[CREATED] soft-reset branch with commit to reset --soft" -ForegroundColor Green
# ============================================================================
# SCENARIO 2: Mixed Reset (--mixed, default)
# ============================================================================
Write-Host "`nScenario 2: Creating mixed-reset branch..." -ForegroundColor Cyan
# Switch back to initial commit and create mixed-reset branch
git switch main | Out-Null
git switch -c mixed-reset | Out-Null
# Build up scenario 2 commits
$appContent = @"
# app.py - Application entry point
def start():
"""Start the application."""
print("Application started")
def stop():
"""Stop the application."""
print("Application stopped")
"@
Set-Content -Path "app.py" -Value $appContent
git add .
git commit -m "Add application lifecycle" | Out-Null
# Good commit: Add logging
$appContent = @"
# app.py - Application entry point
def log(message):
"""Log a message."""
print(f"[LOG] {message}")
def start():
"""Start the application."""
log("Application started")
def stop():
"""Stop the application."""
log("Application stopped")
"@
Set-Content -Path "app.py" -Value $appContent
git add .
git commit -m "Add logging system" | Out-Null
# BAD commit 1: Add experimental feature X
$appContent = @"
# app.py - Application entry point
def log(message):
"""Log a message."""
print(f"[LOG] {message}")
def experimental_feature_x():
"""Experimental feature - NOT READY!"""
log("Feature X is experimental and buggy")
def start():
"""Start the application."""
log("Application started")
experimental_feature_x()
def stop():
"""Stop the application."""
log("Application stopped")
"@
Set-Content -Path "app.py" -Value $appContent
git add .
git commit -m "Add experimental feature X - REMOVE THIS!" | Out-Null
# BAD commit 2: Add debug mode (also not ready)
$appContent = @"
# app.py - Application entry point
DEBUG_MODE = True # Should not be committed!
def log(message):
"""Log a message."""
print(f"[LOG] {message}")
def experimental_feature_x():
"""Experimental feature - NOT READY!"""
log("Feature X is experimental and buggy")
def start():
"""Start the application."""
if DEBUG_MODE:
log("DEBUG MODE ACTIVE!")
log("Application started")
experimental_feature_x()
def stop():
"""Stop the application."""
log("Application stopped")
"@
Set-Content -Path "app.py" -Value $appContent
git add .
git commit -m "Add debug mode - REMOVE THIS TOO!" | Out-Null
Write-Host "[CREATED] mixed-reset branch with commits to reset --mixed" -ForegroundColor Green
# ============================================================================
# SCENARIO 3: Hard Reset (--hard) + Reflog Recovery
# ============================================================================
Write-Host "`nScenario 3: Creating hard-reset branch..." -ForegroundColor Cyan
# Switch back to main and create hard-reset branch
git switch main | Out-Null
git switch -c hard-reset | Out-Null
# Reset to basic state
$utilsContent = @"
# utils.py - Utility functions
def helper_a():
"""Helper function A."""
return "Helper A"
def helper_b():
"""Helper function B."""
return "Helper B"
"@
Set-Content -Path "utils.py" -Value $utilsContent
git add .
git commit -m "Add utility helpers" | Out-Null
# Good commit: Add helper C
$utilsContent = @"
# utils.py - Utility functions
def helper_a():
"""Helper function A."""
return "Helper A"
def helper_b():
"""Helper function B."""
return "Helper B"
def helper_c():
"""Helper function C."""
return "Helper C"
"@
Set-Content -Path "utils.py" -Value $utilsContent
git add .
git commit -m "Add helper C" | Out-Null
# BAD commit: Add broken helper D (completely wrong)
$utilsContent = @"
# utils.py - Utility functions
def helper_a():
"""Helper function A."""
return "Helper A"
def helper_b():
"""Helper function B."""
return "Helper B"
def helper_c():
"""Helper function C."""
return "Helper C"
def helper_d():
"""COMPLETELY BROKEN - throw away!"""
# This is all wrong and needs to be discarded
broken_code = "This doesn't even make sense"
return broken_code.nonexistent_method() # Will crash!
"@
Set-Content -Path "utils.py" -Value $utilsContent
git add .
git commit -m "Add broken helper D - DISCARD COMPLETELY!" | Out-Null
Write-Host "[CREATED] hard-reset branch with commit to reset --hard" -ForegroundColor Green
# ============================================================================
# Return to soft-reset to start
# ============================================================================
git switch soft-reset | Out-Null
# Return to module directory
Set-Location ..
Write-Host "`n=== Setup Complete! ===`n" -ForegroundColor Green
Write-Host "Three reset scenarios have been created:" -ForegroundColor Cyan
Write-Host " 1. soft-reset - Reset --soft (keep changes staged)" -ForegroundColor White
Write-Host " 2. mixed-reset - Reset --mixed (unstage changes)" -ForegroundColor White
Write-Host " 3. hard-reset - Reset --hard (discard everything) + reflog recovery" -ForegroundColor White
Write-Host "`n⚠️ CRITICAL SAFETY REMINDER ⚠️" -ForegroundColor Red
Write-Host "NEVER use git reset on commits that have been PUSHED!" -ForegroundColor Red
Write-Host "These scenarios are LOCAL ONLY for practice." -ForegroundColor Yellow
Write-Host "`nYou are currently on the 'soft-reset' branch." -ForegroundColor Cyan
Write-Host "`nNext steps:" -ForegroundColor Cyan
Write-Host " 1. cd challenge" -ForegroundColor White
Write-Host " 2. Read the README.md for detailed instructions" -ForegroundColor White
Write-Host " 3. Complete each reset challenge" -ForegroundColor White
Write-Host " 4. Run '..\\verify.ps1' to check your solutions" -ForegroundColor White
Write-Host ""

View File

@@ -0,0 +1,231 @@
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Verifies the Module 06 challenge solutions.
.DESCRIPTION
Checks that all three reset scenarios have been completed correctly:
- soft-reset: Commit reset but changes remain staged
- mixed-reset: Commits reset and changes unstaged
- hard-reset: Everything reset and discarded
#>
Write-Host "`n=== Verifying Module 06: Git Reset Solutions ===" -ForegroundColor Cyan
Write-Host "⚠️ Remember: NEVER reset pushed commits! ⚠️" -ForegroundColor Red
$allChecksPassed = $true
$originalDir = Get-Location
# Check if challenge directory exists
if (-not (Test-Path "challenge")) {
Write-Host "[FAIL] Challenge directory not found. Run setup.ps1 first." -ForegroundColor Red
exit 1
}
Set-Location "challenge"
# Check if git repository exists
if (-not (Test-Path ".git")) {
Write-Host "[FAIL] Not a git repository. Run setup.ps1 first." -ForegroundColor Red
Set-Location $originalDir
exit 1
}
# ============================================================================
# SCENARIO 1: Soft Reset Verification
# ============================================================================
Write-Host "`n=== Scenario 1: Soft Reset ===`n" -ForegroundColor Cyan
git switch soft-reset 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Host "[FAIL] soft-reset branch not found" -ForegroundColor Red
$allChecksPassed = $false
} else {
# Count commits (should be 4: Initial + project setup + feature A + feature B)
$commitCount = [int](git rev-list --count HEAD 2>$null)
if ($commitCount -eq 4) {
Write-Host "[PASS] Commit count is 4 (feature C commit was reset)" -ForegroundColor Green
} else {
Write-Host "[FAIL] Expected 4 commits, found $commitCount" -ForegroundColor Red
Write-Host "[HINT] Use: git reset --soft HEAD~1" -ForegroundColor Yellow
$allChecksPassed = $false
}
# Check if changes are staged
$stagedChanges = git diff --cached --name-only 2>$null
if ($stagedChanges) {
Write-Host "[PASS] Changes are staged (feature C code in staging area)" -ForegroundColor Green
# Verify the staged changes contain feature C code
$stagedContent = git diff --cached 2>$null
if ($stagedContent -match "feature_c") {
Write-Host "[PASS] Staged changes contain feature C code" -ForegroundColor Green
} else {
Write-Host "[INFO] Staged changes don't seem to contain feature C" -ForegroundColor Yellow
}
} else {
Write-Host "[FAIL] No staged changes found" -ForegroundColor Red
Write-Host "[HINT] After --soft reset, changes should remain staged" -ForegroundColor Yellow
$allChecksPassed = $false
}
# Check working directory has no unstaged changes to tracked files
$unstagedChanges = git diff --name-only 2>$null
if (-not $unstagedChanges) {
Write-Host "[PASS] No unstaged changes (all changes are staged)" -ForegroundColor Green
} else {
Write-Host "[INFO] Found unstaged changes (expected only staged changes)" -ForegroundColor Yellow
}
}
# ============================================================================
# SCENARIO 2: Mixed Reset Verification
# ============================================================================
Write-Host "`n=== Scenario 2: Mixed Reset ===`n" -ForegroundColor Cyan
git switch mixed-reset 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Host "[FAIL] mixed-reset branch not found" -ForegroundColor Red
$allChecksPassed = $false
} else {
# Count commits (should be 3: Initial + lifecycle + logging, bad commits removed)
$commitCount = [int](git rev-list --count HEAD 2>$null)
if ($commitCount -eq 3) {
Write-Host "[PASS] Commit count is 3 (both bad commits were reset)" -ForegroundColor Green
} elseif ($commitCount -eq 4) {
Write-Host "[INFO] Commit count is 4 (one commit reset, need to reset one more)" -ForegroundColor Yellow
Write-Host "[HINT] Use: git reset HEAD~1 (or git reset --mixed HEAD~1)" -ForegroundColor Yellow
$allChecksPassed = $false
} else {
Write-Host "[FAIL] Expected 3 commits, found $commitCount" -ForegroundColor Red
Write-Host "[HINT] Use: git reset --mixed HEAD~2 to remove both bad commits" -ForegroundColor Yellow
$allChecksPassed = $false
}
# Check that there are NO staged changes
$stagedChanges = git diff --cached --name-only 2>$null
if (-not $stagedChanges) {
Write-Host "[PASS] No staged changes (--mixed unstages everything)" -ForegroundColor Green
} else {
Write-Host "[FAIL] Found staged changes (--mixed should unstage)" -ForegroundColor Red
Write-Host "[HINT] After --mixed reset, changes should be unstaged" -ForegroundColor Yellow
$allChecksPassed = $false
}
# Check that there ARE unstaged changes in working directory
$unstagedChanges = git diff --name-only 2>$null
if ($unstagedChanges) {
Write-Host "[PASS] Unstaged changes present in working directory" -ForegroundColor Green
# Verify unstaged changes contain the experimental/debug code
$workingContent = git diff 2>$null
if ($workingContent -match "experimental|DEBUG") {
Write-Host "[PASS] Unstaged changes contain the reset code" -ForegroundColor Green
} else {
Write-Host "[INFO] Unstaged changes don't contain expected code" -ForegroundColor Yellow
}
} else {
Write-Host "[FAIL] No unstaged changes found" -ForegroundColor Red
Write-Host "[HINT] After --mixed reset, changes should be in working directory (unstaged)" -ForegroundColor Yellow
$allChecksPassed = $false
}
}
# ============================================================================
# SCENARIO 3: Hard Reset Verification
# ============================================================================
Write-Host "`n=== Scenario 3: Hard Reset ===`n" -ForegroundColor Cyan
git switch hard-reset 2>&1 | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Host "[FAIL] hard-reset branch not found" -ForegroundColor Red
$allChecksPassed = $false
} else {
# Count commits (should be 3: Initial + utilities + helper C, bad commit removed)
$commitCount = [int](git rev-list --count HEAD 2>$null)
if ($commitCount -eq 3) {
Write-Host "[PASS] Commit count is 3 (broken commit was reset)" -ForegroundColor Green
} else {
Write-Host "[FAIL] Expected 3 commits, found $commitCount" -ForegroundColor Red
Write-Host "[HINT] Use: git reset --hard HEAD~1" -ForegroundColor Yellow
$allChecksPassed = $false
}
# Check that there are NO staged changes
$stagedChanges = git diff --cached --name-only 2>$null
if (-not $stagedChanges) {
Write-Host "[PASS] No staged changes (--hard discards everything)" -ForegroundColor Green
} else {
Write-Host "[FAIL] Found staged changes (--hard should discard all)" -ForegroundColor Red
$allChecksPassed = $false
}
# Check that there are NO unstaged changes
$unstagedChanges = git diff --name-only 2>$null
if (-not $unstagedChanges) {
Write-Host "[PASS] No unstaged changes (--hard discards everything)" -ForegroundColor Green
} else {
Write-Host "[FAIL] Found unstaged changes (--hard should discard all)" -ForegroundColor Red
$allChecksPassed = $false
}
# Check working directory is clean
$statusOutput = git status --porcelain 2>$null
if (-not $statusOutput) {
Write-Host "[PASS] Working directory is completely clean" -ForegroundColor Green
} else {
Write-Host "[INFO] Working directory has some changes" -ForegroundColor Yellow
}
# Verify the file doesn't have the broken code
if (Test-Path "utils.py") {
$utilsContent = Get-Content "utils.py" -Raw
if ($utilsContent -notmatch "helper_d") {
Write-Host "[PASS] Broken helper_d function is gone" -ForegroundColor Green
} else {
Write-Host "[FAIL] Broken helper_d still exists (wasn't reset)" -ForegroundColor Red
$allChecksPassed = $false
}
if ($utilsContent -match "helper_c") {
Write-Host "[PASS] Good helper_c function is preserved" -ForegroundColor Green
} else {
Write-Host "[FAIL] Good helper_c function missing" -ForegroundColor Red
$allChecksPassed = $false
}
}
}
Set-Location $originalDir
# Final summary
Write-Host ""
if ($allChecksPassed) {
Write-Host "==========================================" -ForegroundColor Green
Write-Host " CONGRATULATIONS! ALL SCENARIOS PASSED!" -ForegroundColor Green
Write-Host "==========================================" -ForegroundColor Green
Write-Host "`nYou've mastered git reset!" -ForegroundColor Cyan
Write-Host "You now understand:" -ForegroundColor Cyan
Write-Host " ✓ Resetting commits with --soft (keep staged)" -ForegroundColor White
Write-Host " ✓ Resetting commits with --mixed (unstage)" -ForegroundColor White
Write-Host " ✓ Resetting commits with --hard (discard all)" -ForegroundColor White
Write-Host " ✓ The DANGER of reset on shared history" -ForegroundColor White
Write-Host "`n⚠️ CRITICAL REMINDER ⚠️" -ForegroundColor Red
Write-Host "NEVER use 'git reset' on commits you've already PUSHED!" -ForegroundColor Red
Write-Host "Always use 'git revert' (Module 05) for shared commits!" -ForegroundColor Yellow
Write-Host "`nReady for Module 07: Git Stash!" -ForegroundColor Green
Write-Host ""
exit 0
} else {
Write-Host "[SUMMARY] Some checks failed. Review the hints above and try again." -ForegroundColor Red
Write-Host "[INFO] You can run this verification script as many times as needed." -ForegroundColor Yellow
Write-Host "[REMINDER] Reset is ONLY for local, un-pushed commits!" -ForegroundColor Yellow
Write-Host ""
exit 1
}

View File

@@ -247,6 +247,16 @@ git revert --abort
```
Abort a revert in progress.
```bash
git revert -m 1 <merge-commit>
```
Revert a merge commit (requires -m flag to specify which parent to keep). Use `-m 1` to keep the branch you merged into (most common).
```bash
git revert -m 2 <merge-commit>
```
Revert a merge commit but keep the branch that was merged in (rare).
### Cherry-Pick
```bash

View File

@@ -8,7 +8,7 @@ Perfect for developers who want to move beyond basic Git usage and master profes
The workshop is organized into two tracks:
### 01 Essentials - Core Git Skills (7 modules)
### 01 Essentials - Core Git Skills (8 modules)
Master fundamental Git concepts and collaborative workflows:
@@ -16,9 +16,10 @@ Master fundamental Git concepts and collaborative workflows:
- **Module 02: Viewing History** - Use git log and git diff to explore project history
- **Module 03: Branching and Merging** - Create branches, merge them, and resolve conflicts (checkpoint-based)
- **Module 04: Cherry-Pick** - Apply specific commits from one branch to another
- **Module 05: Reset vs Revert** - Understand when to rewrite history vs create new commits
- **Module 06: Stash** - Temporarily save work without committing
- **Module 07: Multiplayer Git** - **The Great Print Project** - Real cloud-based collaboration with teammates
- **Module 05: Git Revert** - Safe undoing - preserve history while reversing changes (includes merge commit reversion)
- **Module 06: Git Reset** - Dangerous history rewriting - local cleanup only (NEVER on pushed commits!)
- **Module 07: Stash** - Temporarily save work without committing
- **Module 08: Multiplayer Git** - **The Great Print Project** - Real cloud-based collaboration with teammates
### 02 Advanced - Professional Techniques (6 modules)
@@ -44,18 +45,18 @@ Advanced Git workflows for power users:
**Quick Reference**: See [GIT-CHEATSHEET.md](GIT-CHEATSHEET.md) for a comprehensive list of all Git commands covered in this workshop. Don't worry about memorizing everything - use this as a reference when you need to look up command syntax!
### For Module 07: Multiplayer Git
### For Module 08: Multiplayer Git
**This module is different!** It uses a real Git server for authentic collaboration:
1. Navigate to `01_essentials/09-multiplayer`
1. Navigate to `01_essentials/08-multiplayer`
2. Read the `README.md` for complete instructions
3. **No setup script** - you'll clone from https://git.frod.dk/multiplayer
4. Work with a partner on shared branches
5. Experience real merge conflicts and pull requests
6. **No verify script** - success is visual (your code appears in the final output)
**Facilitators**: See `01_essentials/09-multiplayer/FACILITATOR-SETUP.md` for server setup and workshop guidance.
**Facilitators**: See `01_essentials/08-multiplayer/FACILITATOR-SETUP.md` for server setup and workshop guidance.
## Running PowerShell Scripts
@@ -136,9 +137,9 @@ You should see your name and email printed. This is required to make commits in
$PSVersionTable.PSVersion
```
### Python (for Module 07 only)
### Python (for Module 08 only)
Module 07 (Multiplayer Git) uses Python:
Module 08 (Multiplayer Git) uses Python:
- **Python 3.6+** required to run the Great Print Project
- Check: `python --version` or `python3 --version`
@@ -209,7 +210,7 @@ Follow the instructions in each module's README.md file.
- **Progressive Difficulty**: Builds from basics to advanced Git techniques
- **Reset Anytime**: Each local module includes a reset script for a fresh start
- **Self-Paced**: Learn at your own speed with detailed README guides
- **Real Collaboration**: Module 07 uses an actual Git server for authentic teamwork
- **Real Collaboration**: Module 08 uses an actual Git server for authentic teamwork
- **Comprehensive Coverage**: From `git init` to advanced rebasing and bisecting
## Learning Path
@@ -305,7 +306,7 @@ Before distributing this workshop to attendees for self-study:
2. Each module's `challenge/` directory will become its own independent git repository when attendees run `setup.ps1`
3. This isolation ensures each module provides a clean learning environment
**Note**: Module 07 (Multiplayer) requires you to set up a Git server - see facilitator guide below.
**Note**: Module 08 (Multiplayer) requires you to set up a Git server - see facilitator guide below.
### Facilitated Workshop
@@ -313,13 +314,13 @@ For running this as a full-day instructor-led workshop:
1. **See [WORKSHOP-AGENDA.md](WORKSHOP-AGENDA.md)** - Complete agenda with timing, activities, and facilitation tips
2. **See [PRESENTATION-OUTLINE.md](PRESENTATION-OUTLINE.md)** - Slide deck outline for presentations
3. **Workshop covers:** Essentials 01-05 + Module 07 (Multiplayer collaboration exercise)
3. **Workshop covers:** Essentials 01-05 + Module 08 (Multiplayer collaboration exercise)
4. **Duration:** 6-7 hours including breaks
5. **Format:** Mix of presentation, live demos, and hands-on challenges
**Facilitator preparation:**
- Review the workshop agenda thoroughly
- Set up Git server for Module 07 (see below)
- Set up Git server for Module 08 (see below)
- Ensure all participants have prerequisites installed (Git, PowerShell, Python)
- Prepare slides using the presentation outline
- Test all modules on a clean machine
@@ -327,9 +328,9 @@ For running this as a full-day instructor-led workshop:
The workshop format combines instructor-led sessions with self-paced hands-on modules for an engaging learning experience.
### Setting Up Module 07: Multiplayer Git
### Setting Up Module 08: Multiplayer Git
Module 07 requires a Git server for authentic collaboration. You have two options:
Module 08 requires a Git server for authentic collaboration. You have two options:
**Option 1: Self-Hosted Gitea Server (Recommended)**
@@ -344,7 +345,7 @@ Run your own Git server with Gitea using Docker and Cloudflare Tunnel:
**Setup:**
1. See [GITEA-SETUP.md](GITEA-SETUP.md) for complete Gitea + Docker + Cloudflare Tunnel instructions
2. See `01_essentials/09-multiplayer/FACILITATOR-SETUP.md` for detailed workshop preparation:
2. See `01_essentials/08-multiplayer/FACILITATOR-SETUP.md` for detailed workshop preparation:
- Creating student accounts
- Setting up The Great Print Project repository
- Pairing students
@@ -374,14 +375,15 @@ git-workshop/
├── GITEA-SETUP.md # Self-hosted Git server setup
├── install-glow.ps1 # Install glow markdown renderer
├── 01_essentials/ # Core Git skills (7 modules)
├── 01_essentials/ # Core Git skills (8 modules)
│ ├── 01-basics/ # Initialize, commit, status
│ ├── 02-history/ # Log, diff, show
│ ├── 03-branching-and-merging/ # Branches, merging, conflicts (checkpoint-based)
│ ├── 04-cherry-pick/ # Apply specific commits
│ ├── 05-reset-vs-revert/ # Undo changes safely
│ ├── 06-stash/ # Save work-in-progress
── 07-multiplayer/ # Real collaboration (cloud-based)
│ ├── 05-revert/ # Safe undoing (includes merge commits)
│ ├── 06-reset/ # Dangerous local cleanup
── 07-stash/ # Save work-in-progress
│ └── 08-multiplayer/ # Real collaboration (cloud-based)
│ ├── README.md # Student guide
│ └── FACILITATOR-SETUP.md # Server setup guide
@@ -398,7 +400,7 @@ git-workshop/
### The Great Print Project (Module 07)
Unlike any other Git tutorial, Module 07 provides **real collaborative experience**:
Unlike any other Git tutorial, Module 08 provides **real collaborative experience**:
- **Real Git server**: Not simulated - actual cloud repository at https://git.frod.dk/multiplayer
- **Real teammates**: Work in pairs on shared branches
@@ -425,7 +427,7 @@ This is how professional developers actually work - no simulation, no shortcuts.
**Q: Do I need to complete all modules?**
A: No! Essentials 01-05 covers what most developers use daily. Complete 06-09 and Advanced modules to deepen your skills.
**Q: Can I do Module 07 (Multiplayer) without a partner?**
**Q: Can I do Module 08 (Multiplayer) without a partner?**
A: Not recommended - collaboration is the point. If solo, skip to Advanced modules or wait until you can pair with someone.
**Q: How long does the workshop take?**
@@ -441,16 +443,16 @@ A:
2. Check [GIT-CHEATSHEET.md](GIT-CHEATSHEET.md)
3. Run `./reset.ps1` to start fresh
4. Use `git status` and `git log --graph` to understand current state
5. For Module 07, ask your partner or facilitator
5. For Module 08, ask your partner or facilitator
**Q: Can I use this for a team workshop at my company?**
A: Absolutely! See the "For Workshop Facilitators" section above. The materials are designed for both self-study and instructor-led workshops.
**Q: Do I need internet access?**
A: Modules 01-08 work completely offline. Module 07 requires internet to access the Git server.
A: Modules 01-07 work completely offline. Module 08 requires internet to access the Git server.
**Q: What if I prefer GitHub/GitLab instead of Gitea?**
A: The skills are identical across all Git platforms. Module 07 uses Gitea but everything you learn applies to GitHub, GitLab, Bitbucket, etc.
A: The skills are identical across all Git platforms. Module 08 uses Gitea but everything you learn applies to GitHub, GitLab, Bitbucket, etc.
---