From 30b878fc67fb8013d6ae5529dc2689cddbd27194 Mon Sep 17 00:00:00 2001 From: Bjarke Sporring Date: Wed, 7 Jan 2026 20:22:53 +0100 Subject: [PATCH] feat: add `git blame` section --- 01_essentials/02-history/README.md | 18 ++++++++- 01_essentials/02-history/setup.ps1 | 61 +++++++++++++++++++++++++---- 01_essentials/02-history/verify.ps1 | 15 +++---- 3 files changed, 79 insertions(+), 15 deletions(-) diff --git a/01_essentials/02-history/README.md b/01_essentials/02-history/README.md index 42d5eca..84fdd3e 100644 --- a/01_essentials/02-history/README.md +++ b/01_essentials/02-history/README.md @@ -8,6 +8,7 @@ In this module, you will: - Use `git show` to view specific commit details - Use `git diff` to compare changes between commits - Use `git diff --staged` to view changes ready to be committed +- Use `git blame` to find who made specific changes - Understand commit hashes and references - Discover how `git diff` reveals changes not visible in current files @@ -30,6 +31,7 @@ You'll explore an existing Git repository that contains multiple commits. Your g - Examining changes between specific commits - Understanding staged changes - **Finding a secret code hidden in the commit history!** (Only discoverable by using `git diff`) +- **Tracking down who made a suspicious code change** (Using `git blame`) The setup script will create an `answers.md` file in the challenge directory with questions for you to answer. Fill in your answers directly in that file. @@ -43,7 +45,8 @@ The setup script will create an `answers.md` file in the challenge directory wit 6. Try different log formats: `git log --stat`, `git log --graph` 7. View specific commits: `git show ` 8. Compare specific commits: `git diff ` -9. Fill in your answers in `answers.md` +9. Use `git blame` to find who wrote specific lines: `git blame -e ` +10. Fill in your answers in `answers.md` > **Important Notes:** > - You can use any Git commands you like to explore the repository @@ -204,6 +207,19 @@ git diff HEAD # Show all changes (staged + unstaged) vs last c - `git diff HEAD` - See all your changes since the last commit - `git diff ` - Compare any two points in history +### Finding Who Changed What with git blame + +```bash +git blame # Show who last modified each line +git blame -e # Show with email addresses +git blame -L 10,20 # Blame specific line range (lines 10-20) +``` + +**When to use `git blame`:** +- Find who wrote a specific line of code +- Identify when a bug was introduced +- Understand the context/reason for a change by finding the author + ## Verification Once you've filled in your answers in `answers.md`, verify your solution: diff --git a/01_essentials/02-history/setup.ps1 b/01_essentials/02-history/setup.ps1 index c21b63e..2dc7b65 100644 --- a/01_essentials/02-history/setup.ps1 +++ b/01_essentials/02-history/setup.ps1 @@ -200,6 +200,39 @@ Set-Content -Path "app.py" -Value $appContent git add . git commit -m "Add user profile feature" | Out-Null +# Commit 6: Add a suspicious change (for git blame challenge) +Write-Host "Adding configuration change..." -ForegroundColor Green +# Temporarily change git user for this commit +git config user.name "Suspicious Developer" +git config user.email "guilty@email.com" + +$appContent = @" +# app.py - Main application file +from auth import login, logout +from database import connect, disconnect +from profile import get_profile + +def main(): + print("Welcome to My App!") + connect() + # Application initialization code here + if login("admin", "admin123"): # TODO: Change default credentials! + profile = get_profile("admin") + print(f"User profile: {profile}") + pass + +if __name__ == "__main__": + main() +"@ +Set-Content -Path "app.py" -Value $appContent + +git add . +git commit -m "Update default login credentials" | Out-Null + +# Reset git config back to original +git config user.name "Workshop Student" +git config user.email "student@example.com" + # Create a staged change scenario Write-Host "Creating staged changes for exploration..." -ForegroundColor Green $configContent = @" @@ -309,20 +342,26 @@ git diff database.py --- -## Question 6: What changes were made to app.py between the second and fourth commits? +## Question 6: Who changed the login credentials to hardcoded values? 🔍 -**Note:** Describe the main changes you observe (new imports, new function calls, etc.) +**The Challenge:** +Someone on the team added hardcoded login credentials to `app.py` (username: "admin", password: "admin123"). This is a security issue! Use `git blame` to find out who made this change. + +**Your task:** Find the email address of the person who wrote the line containing `login("admin", "admin123")`. **Suggested commands:** ``````bash -# First, get commit hashes -git log --oneline +# View who changed each line in app.py +git blame app.py -# Then compare commits 2 and 4 -git diff app.py +# For a more readable format +git blame -e app.py # Shows email addresses + +# Look for the line with login("admin", "admin123") +# The format is: commit_hash (Author Name date time line_number) line_content `````` -**Your Answer:** +**Your Answer (provide the email address):** @@ -355,6 +394,14 @@ git diff # Show unstaged changes git diff HEAD # Show all changes since last commit `````` +**Finding Who Changed What:** +``````bash +git blame # Show who last modified each line +git blame -e # Show with email addresses +git blame -L 10,20 # Blame specific line range +git blame | grep "pattern" # Find who changed lines matching pattern +`````` + **Repository Status:** ``````bash git status # Show working tree status diff --git a/01_essentials/02-history/verify.ps1 b/01_essentials/02-history/verify.ps1 index fb40843..0478f84 100644 --- a/01_essentials/02-history/verify.ps1 +++ b/01_essentials/02-history/verify.ps1 @@ -41,8 +41,8 @@ if (-not (Test-Path "answers.md")) { $answers = Get-Content "answers.md" -Raw $answersLower = $answers.ToLower() - # Check 1: Contains "5" or "five" for commit count - if ($answersLower -match "5|five") { + # Check 1: Contains "6" or "six" for commit count + if ($answersLower -match "6|six") { Write-Host "[PASS] Correct commit count found" -ForegroundColor Green } else { Write-Host "[FAIL] Commit count not found or incorrect" -ForegroundColor Red @@ -93,12 +93,12 @@ if (-not (Test-Path "answers.md")) { $allChecksPassed = $false } - # Check 6: Some mention of changes between commits (flexible check) - if ($answersLower -match "database|connect|disconnect|import|added|validation|error") { - Write-Host "[PASS] Changes to app.py between commits described" -ForegroundColor Green + # Check 6: Contains "guilty@email.com" for git blame challenge + if ($answersLower -match "guilty@email\.com") { + Write-Host "[PASS] Correct email address found using git blame!" -ForegroundColor Green } else { - Write-Host "[FAIL] Changes to app.py between commits not described" -ForegroundColor Red - Write-Host "[HINT] Use 'git diff app.py' to see changes between specific commits" -ForegroundColor Yellow + Write-Host "[FAIL] Guilty developer's email not found" -ForegroundColor Red + Write-Host "[HINT] Use 'git blame -e app.py' to see who changed each line with email addresses" -ForegroundColor Yellow $allChecksPassed = $false } } @@ -118,6 +118,7 @@ if ($allChecksPassed) { Write-Host " - See which files changed in commits" -ForegroundColor White Write-Host " - Check staged changes with git diff --staged" -ForegroundColor White Write-Host " - Compare changes between specific commits with git diff" -ForegroundColor White + Write-Host " - Track down who made changes with git blame" -ForegroundColor White Write-Host "`nReady for the next module!" -ForegroundColor Green Write-Host "" } else {