feat: add more challenges to the history module

This commit is contained in:
Bjarke Sporring
2026-01-07 20:12:54 +01:00
parent 5f8091d5b2
commit aefcfbe100
3 changed files with 259 additions and 57 deletions

View File

@@ -7,7 +7,9 @@ In this module, you will:
- Use `git log` to view commit history with various formats
- 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
- Understand commit hashes and references
- Discover how `git diff` reveals changes not visible in current files
## Challenge
@@ -23,7 +25,11 @@ This will create a `challenge/` directory with a Git repository that already has
### Your Task
You'll explore an existing Git repository that contains multiple commits. Your goal is to use Git commands to discover information about the repository's history.
You'll explore an existing Git repository that contains multiple commits. Your goal is to use Git commands to discover information about the repository's history, including:
- Viewing commit history
- Examining changes between specific commits
- Understanding staged changes
- **Finding a secret code hidden in the commit history!** (Only discoverable by using `git diff`)
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.
@@ -31,17 +37,19 @@ The setup script will create an `answers.md` file in the challenge directory wit
1. Navigate to the challenge directory: `cd challenge`
2. Open `answers.md` to see the questions
3. View the commit history: `git log`
4. Try different log formats: `git log --oneline`, `git log --stat`
5. View specific commits: `git show <commit-hash>`
6. Compare commits: `git diff <commit1> <commit2>`
7. Fill in your answers in `answers.md`
3. View the commit history: `git log` or `git log --oneline`
4. Check repository status: `git status`
5. View staged changes: `git diff --staged`
6. Try different log formats: `git log --stat`, `git log --graph`
7. View specific commits: `git show <commit-hash>`
8. Compare specific commits: `git diff <commit1> <commit2> <file>`
9. Fill in your answers in `answers.md`
> **Important Notes:**
> - You can use any Git commands you like to explore the repository
> - Fill in your answers directly in the `answers.md` file (there are placeholder sections for each answer)
> - Try different `git log` options to see which format you prefer
> - Commit hashes can be referenced by their full hash or just the first 7 characters
> - Notice that one file is already staged - use `git diff --staged` to see what it contains
## Key Concepts
@@ -50,6 +58,11 @@ The setup script will create an `answers.md` file in the challenge directory wit
- **Commit History**: The chronological record of all changes made to a repository.
- **HEAD**: A pointer to the current commit you're working from.
- **Diff**: A view showing the differences between two versions of files.
- **Staging Area**: Where changes wait before being committed. Use `git diff --staged` to see what's ready to commit.
- **Working Directory vs Staged vs Committed**:
- Working Directory: Files you're currently editing
- Staged (Index): Changes marked for the next commit (via `git add`)
- Committed: Changes permanently saved in history
## Understanding Diff Output
@@ -144,16 +157,23 @@ diff --git a/auth.py b/auth.py
In this module's challenge, you'll use:
```bash
git diff <commit1> <commit2> app.py
# See what's staged for the next commit
git diff --staged
# Compare changes between specific commits
git diff <commit2> <commit4> app.py
```
Pay attention to:
- Which lines were added (green, with `+`)
- Which lines were removed (red, with `-`)
- The surrounding context (white, with space)
- How `git diff --staged` shows only changes ready to commit
## Useful Commands
### Viewing History
```bash
git log # View commit history
git log --oneline # Compact one-line format
@@ -161,10 +181,29 @@ git log --stat # Show files changed in each commit
git log --graph # Show branch graph (more useful with branches)
git show <commit> # View specific commit details
git show <commit>:<file> # View a file from a specific commit
git diff <commit1> <commit2> # Compare two commits
git diff <commit> # Compare commit with current working directory
```
### Comparing Changes with git diff
```bash
# Compare commits
git diff <commit1> <commit2> # Compare two commits
git diff <commit1> <commit2> <file> # Compare specific file between commits
git diff <commit> # Compare commit with current working directory
# Compare staged changes
git diff --staged # Show changes in staging area (ready to commit)
git diff --cached # Same as --staged (alternative syntax)
git diff # Show unstaged changes in working directory
git diff HEAD # Show all changes (staged + unstaged) vs last commit
```
**When to use each `git diff` variant:**
- `git diff` - See what you've changed but haven't staged yet
- `git diff --staged` - Review what you're about to commit
- `git diff HEAD` - See all your changes since the last commit
- `git diff <commit1> <commit2>` - Compare any two points in history
## Verification
Once you've filled in your answers in `answers.md`, verify your solution:

View File

@@ -87,6 +87,7 @@ git commit -m "Add user authentication" | Out-Null
Write-Host "Adding database connection..." -ForegroundColor Green
$databaseContent = @"
# database.py - Database connection module
# SECRET_CODE: UNICORN
def connect():
# Connect to database
@@ -140,6 +141,22 @@ def logout(username):
"@
Set-Content -Path "auth.py" -Value $authContent
# Remove the secret code from database.py
$databaseContent = @"
# database.py - Database connection module
def connect():
# Connect to database
print("Connecting to database...")
return True
def disconnect():
# Disconnect from database
print("Disconnecting from database...")
return True
"@
Set-Content -Path "database.py" -Value $databaseContent
git add .
git commit -m "Fix authentication bug" | Out-Null
@@ -183,52 +200,172 @@ Set-Content -Path "app.py" -Value $appContent
git add .
git commit -m "Add user profile feature" | Out-Null
# Create a staged change scenario
Write-Host "Creating staged changes for exploration..." -ForegroundColor Green
$configContent = @"
# config.py - Configuration settings
DEBUG_MODE = False
DATABASE_URL = "sqlite:///app.db"
"@
Set-Content -Path "config.py" -Value $configContent
git add config.py
# Create answers.md template
Write-Host "Creating answers.md template..." -ForegroundColor Green
$answersTemplate = @"
# Git History Exploration - Answers
Answer the following questions by exploring the Git repository history.
## Question 1: How many commits are in the repository?
**Your Answer:**
<!-- Write your answer here -->
## Question 2: What was the commit message for the third commit?
(Counting from the first/oldest commit)
**Your Answer:**
<!-- Write your answer here -->
## Question 3: Which file was modified in the "Fix authentication bug" commit?
**Your Answer:**
<!-- Write your answer here -->
## Question 4: What changes were made to app.py between the first and last commits?
Briefly describe the main changes you observe.
**Your Answer:**
<!-- Write your answer here -->
Welcome! Answer the following questions by exploring the Git repository history using Git commands.
**Instructions:**
- Replace the "Write your answer here" comments with your actual answers
- You can use any Git commands to explore the repository
- The hints section at the bottom provides helpful commands
---
**Hints:**
- Use `git log` or `git log --oneline` to view commit history
- Use `git log --stat` to see which files were changed in each commit
- Use `git show <commit-hash>` to view details of a specific commit
- Use `git diff <commit1> <commit2> <file>` to compare changes between commits
## Question 1: How many commits are in the repository?
**Suggested commands:**
``````bash
git log --oneline
# Or count them with:
git rev-list --count HEAD
``````
**Your Answer:**
<!-- Write your answer here -->
---
## Question 2: What was the commit message for the third commit?
**Note:** Count from the first/oldest commit (the one at the bottom of `git log`)
**Suggested commands:**
``````bash
git log --oneline --reverse # Shows oldest first
git log # Shows newest first
``````
**Your Answer:**
<!-- Write your answer here -->
---
## Question 3: Which files were modified in the "Fix authentication bug" commit?
**Note:** There are multiple files - list all of them!
**Suggested commands:**
``````bash
git log --stat # Shows files changed in each commit
git show <commit-hash> --name-only # Shows only filenames for a commit
``````
**Your Answer:**
<!-- Write your answer here (list all files) -->
---
## Question 4: What new file is currently staged (ready to be committed)?
**Suggested commands:**
``````bash
git status # Shows staged, unstaged, and untracked files
git diff --staged # Shows content of staged changes
``````
**Your Answer:**
<!-- Write your answer here -->
---
## Question 5: Find the secret code hidden in the commit history! 🔍
**The Challenge:**
A secret code was added in one commit and then removed in a later commit. It doesn't exist in the current files - you can ONLY find it by comparing commits with `git diff`.
**Hint:** The secret code is in `database.py` and exists in the third commit but was removed in the fourth commit.
**Suggested commands:**
``````bash
# First, get the commit hashes
git log --oneline
# Then compare the third and fourth commits
git diff <commit3-hash> <commit4-hash> database.py
# Look for lines that were removed (marked with - in red)
``````
**Your Answer (what is the secret code?):**
<!-- Write your answer here -->
---
## Question 6: What changes were made to app.py between the second and fourth commits?
**Note:** Describe the main changes you observe (new imports, new function calls, etc.)
**Suggested commands:**
``````bash
# First, get commit hashes
git log --oneline
# Then compare commits 2 and 4
git diff <commit2-hash> <commit4-hash> app.py
``````
**Your Answer:**
<!-- Write your answer here -->
---
## Quick Reference - Useful Commands
**Viewing History:**
``````bash
git log # View commit history (newest first)
git log --oneline # Compact one-line format
git log --reverse # Show oldest commits first
git log --stat # Show files changed in each commit
git log --graph --all # Visual branch graph
``````
**Viewing Specific Commits:**
``````bash
git show <commit-hash> # View commit details
git show <commit-hash> --stat # Show files changed
git show <commit-hash>:file.txt # View file from specific commit
``````
**Comparing Changes:**
``````bash
git diff <commit1> <commit2> # Compare two commits
git diff <commit1> <commit2> file # Compare specific file
git diff --staged # Show staged changes
git diff # Show unstaged changes
git diff HEAD # Show all changes since last commit
``````
**Repository Status:**
``````bash
git status # Show working tree status
git ls-files # List all tracked files
``````
**Pro Tip:** You can use just the first 7 characters of a commit hash (e.g., `a1b2c3d` instead of the full hash)
---
When you're done, run ``..\verify.ps1`` to check your answers!
"@
Set-Content -Path "answers.md" -Value $answersTemplate

View File

@@ -59,21 +59,46 @@ if (-not (Test-Path "answers.md")) {
$allChecksPassed = $false
}
# Check 3: Contains "auth" keyword for file modified in bug fix
if ($answersLower -match "auth") {
Write-Host "[PASS] Correct file identified for bug fix commit" -ForegroundColor Green
# Check 3: Contains both "auth" and "database" keywords for files modified in bug fix
$hasAuth = $answersLower -match "auth"
$hasDatabase = $answersLower -match "database"
if ($hasAuth -and $hasDatabase) {
Write-Host "[PASS] Both files identified for bug fix commit" -ForegroundColor Green
} elseif ($hasAuth -or $hasDatabase) {
Write-Host "[PARTIAL] Only one file found - there are TWO files modified in this commit" -ForegroundColor Yellow
Write-Host "[HINT] Use 'git log --stat' or 'git show <commit-hash> --name-only' to see ALL files changed" -ForegroundColor Yellow
$allChecksPassed = $false
} else {
Write-Host "[FAIL] File modified in bug fix commit not found" -ForegroundColor Red
Write-Host "[FAIL] Files modified in bug fix commit not found" -ForegroundColor Red
Write-Host "[HINT] Use 'git log --stat' to see which files were changed in each commit" -ForegroundColor Yellow
$allChecksPassed = $false
}
# Check 4: Some mention of changes (flexible check)
if ($answersLower -match "import|profile|function|added|login|connect") {
Write-Host "[PASS] Changes to app.py described" -ForegroundColor Green
# Check 4: Contains "config" keyword for staged file
if ($answersLower -match "config") {
Write-Host "[PASS] Staged file identified" -ForegroundColor Green
} else {
Write-Host "[FAIL] Changes to app.py not described" -ForegroundColor Red
Write-Host "[HINT] Use 'git diff <first-commit> <last-commit> app.py' to see changes" -ForegroundColor Yellow
Write-Host "[FAIL] Staged file not identified" -ForegroundColor Red
Write-Host "[HINT] Use 'git status' or 'git diff --staged' to see staged changes" -ForegroundColor Yellow
$allChecksPassed = $false
}
# Check 5: Contains "unicorn" for the secret code
if ($answersLower -match "unicorn") {
Write-Host "[PASS] Secret code found!" -ForegroundColor Green
} else {
Write-Host "[FAIL] Secret code not found" -ForegroundColor Red
Write-Host "[HINT] Use 'git diff <commit3> <commit4> database.py' to find the secret code" -ForegroundColor Yellow
$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
} else {
Write-Host "[FAIL] Changes to app.py between commits not described" -ForegroundColor Red
Write-Host "[HINT] Use 'git diff <commit2> <commit4> app.py' to see changes between specific commits" -ForegroundColor Yellow
$allChecksPassed = $false
}
}
@@ -91,7 +116,8 @@ if ($allChecksPassed) {
Write-Host " - View commit history with git log" -ForegroundColor White
Write-Host " - Find specific commits and their messages" -ForegroundColor White
Write-Host " - See which files changed in commits" -ForegroundColor White
Write-Host " - Compare changes between commits with git diff" -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 "`nReady for the next module!" -ForegroundColor Green
Write-Host ""
} else {