Compare commits
81 Commits
f11f5a4646
...
refactor-r
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6cf0418ebd | ||
|
|
a8e9507f89 | ||
|
|
a895abdd03 | ||
|
|
356b6268ba | ||
|
|
bd69774191 | ||
|
|
fcaf97f60b | ||
|
|
2a5eb137f6 | ||
|
|
b0d2d43c8b | ||
|
|
cdd695b250 | ||
|
|
0474a6de0e | ||
|
|
575e083f33 | ||
|
|
aa24c50b45 | ||
|
|
939bd397f1 | ||
|
|
3130874981 | ||
|
|
a34b7d155f | ||
|
|
1c20a06c21 | ||
|
|
988ce3bc92 | ||
|
|
9fbdd941fa | ||
|
|
40341d21a7 | ||
|
|
8a82253fc2 | ||
|
|
f0522e14bc | ||
|
|
0183a06134 | ||
|
|
c3d9d3337c | ||
|
|
ced65740a3 | ||
|
|
bf07cb1868 | ||
|
|
7b638d27de | ||
|
|
ea5cbccc75 | ||
|
|
76c4182186 | ||
|
|
5b4c544fee | ||
|
|
74a23dbbca | ||
|
|
25077cd589 | ||
|
|
8b3ba808d1 | ||
|
|
9e03a9624a | ||
|
|
b2b8a2cfff | ||
|
|
7e2f8d64fb | ||
|
|
eadf8cfe6a | ||
|
|
9e22f84a53 | ||
|
|
daa787842a | ||
|
|
a392e8c97d | ||
|
|
009a3a9104 | ||
|
|
32a0e89f72 | ||
|
|
91c46718c6 | ||
|
|
c99e238814 | ||
|
|
2633ee2b71 | ||
|
|
c28151cc19 | ||
|
|
07faa14b7a | ||
|
|
09f25d6eae | ||
|
|
cbefeaf4d2 | ||
|
|
7066e648d5 | ||
|
|
14cfc2feeb | ||
|
|
734b49bc7d | ||
|
|
985c4a0a8a | ||
|
|
f55cb444e7 | ||
|
|
6d2e099eb4 | ||
|
|
f696044461 | ||
|
|
e7ce41cbbc | ||
|
|
34f2607b22 | ||
|
|
bb01592b6c | ||
|
|
5492f17a5a | ||
|
|
5f78245734 | ||
|
|
8d63b2d22e | ||
|
|
039debe744 | ||
|
|
a0a9a5a0d9 | ||
|
|
0a9f7c1842 | ||
|
|
7f34cf2d08 | ||
|
|
eb63970b7a | ||
|
|
dff82c847c | ||
|
|
c057fd617b | ||
|
|
f48eefee10 | ||
|
|
df9a2bf7c1 | ||
|
|
7fb84560f5 | ||
|
|
6b0e84934a | ||
|
|
30b878fc67 | ||
|
|
aefcfbe100 | ||
|
|
5f8091d5b2 | ||
|
|
cf073d569e | ||
|
|
d7c146975d | ||
|
|
dc94520b2a | ||
|
|
e29b9bca70 | ||
|
|
a7b511c8cb | ||
|
|
a8eb66d3c9 |
116
01-essentials/01-basics/README.md
Normal file
116
01-essentials/01-basics/README.md
Normal file
@@ -0,0 +1,116 @@
|
||||
# Module 01: Git Basics
|
||||
|
||||
## Learning Objectives
|
||||
|
||||
- Understand what a git repository is
|
||||
- Learn the basic git workflow: modify → stage → commit
|
||||
- Use `git status` to check repository state
|
||||
- Use `git add` to stage changes
|
||||
- Use `git commit` to save changes
|
||||
|
||||
## Challenge
|
||||
|
||||
In this challenge, you'll learn the fundamental git workflow.
|
||||
|
||||
### Setup
|
||||
|
||||
Run the setup script to prepare the challenge:
|
||||
|
||||
```powershell
|
||||
.\setup.ps1
|
||||
```
|
||||
|
||||
This will create a directory called `challenge` with some files that need to be committed.
|
||||
|
||||
### Your Task
|
||||
|
||||
Your goal is to commit both `welcome.txt` and `instructions.txt` to a git repository. Here's a suggested approach:
|
||||
|
||||
1. Navigate into the `challenge` directory: `cd challenge`
|
||||
2. **Initialize a new git repository**: `git init` (this is your first step!)
|
||||
3. Check the status of your repository: `git status`
|
||||
4. Stage the file you want to commit: `git add welcome.txt` (or `git add .` to stage all files)
|
||||
5. Check the status again and see the difference `git status`. Notice the file is now *staged* and ready to be committed.
|
||||
6. Create a commit: `git commit -m "add welcome.txt"`
|
||||
5. Check the status again and see the difference `git status`. Notice the file no longer appears in the output.
|
||||
7. Stage the next file: `git add instructions.txt` (or `git add .` to stage all files)
|
||||
8. Check the status again and see the difference `git status`. Notice the file is now *staged* and ready to be committed.
|
||||
9. Create a commit: `git commit -m "add instructions.txt"`
|
||||
10. Check the status again and see the difference `git status`. Notice that the files are now not shown in status. If and when you change something about the file you will once again see it in the `git status` command.
|
||||
|
||||
**Important Notes**:
|
||||
- The challenge directory is NOT a git repository until you run `git init`. This is intentional - you're learning to start from scratch!
|
||||
- You can commit both files together in one commit, or separately in multiple commits (use `git add .` to add all files in the folder) - it's up to you!
|
||||
- The verification script checks that both files are committed, not the specific commit messages or order
|
||||
|
||||
### Key Concepts
|
||||
|
||||
- **Repository**: A directory tracked by git, containing your project files and their history
|
||||
- **Working Directory**: The files you see and edit
|
||||
- **Staging Area**: A preparation area for your next commit, you first add the files to the stage, and then you commit the files to repository.
|
||||
- **Commit**: A snapshot of your staged changes
|
||||
|
||||
### Useful Commands
|
||||
|
||||
```bash
|
||||
git init # Initialize a new git repository
|
||||
git status # Show the working tree status
|
||||
git add <file> # Stage a specific file for commit
|
||||
git add . # Stage all files in current directory
|
||||
git commit -m "<message>" # Create a commit with a message
|
||||
git ls-tree -r HEAD --name-only # ADVANCED: List all files in the latest commit
|
||||
git log # View commit history
|
||||
```
|
||||
|
||||
### Verification
|
||||
|
||||
Once you think you've completed the challenge, run the verification script.
|
||||
|
||||
**Important:** Run this from the **module directory**, not the challenge directory.
|
||||
|
||||
```powershell
|
||||
# If you're in the challenge directory, go back up:
|
||||
cd ..
|
||||
|
||||
# Then verify:
|
||||
.\verify.ps1
|
||||
```
|
||||
|
||||
This will check if you've successfully completed all the steps.
|
||||
|
||||
### Troubleshooting
|
||||
|
||||
**Error: "fatal: unable to auto-detect email address"**
|
||||
|
||||
This means Git doesn't know who you are yet. You need to configure your name and email:
|
||||
|
||||
```powershell
|
||||
git config user.name "Your Name"
|
||||
git config user.email "your.email@example.com"
|
||||
```
|
||||
|
||||
Then try your commit again. For more details, see the "Requirements" section in the main README.md.
|
||||
|
||||
**Error: "Not a git repository"**
|
||||
|
||||
Make sure you ran `git init` in the challenge directory. This creates a hidden `.git` folder that tracks your project.
|
||||
|
||||
**Can't find the challenge directory?**
|
||||
|
||||
Make sure you ran `.\setup.ps1` first from the module directory. This creates the `challenge/` folder.
|
||||
|
||||
**Where am I?**
|
||||
|
||||
Use `pwd` (Print Working Directory) to see your current location:
|
||||
- If you're in something like `.../module-01-basics/challenge`, you're in the challenge directory
|
||||
- If you're in something like `.../module-01-basics`, you're in the module directory
|
||||
|
||||
### Need to Start Over?
|
||||
|
||||
If you want to reset the challenge and start fresh, run:
|
||||
|
||||
```powershell
|
||||
.\reset.ps1
|
||||
```
|
||||
|
||||
This will remove your challenge directory and set up a new one.
|
||||
225
01-essentials/02-history/README.md
Normal file
225
01-essentials/02-history/README.md
Normal file
@@ -0,0 +1,225 @@
|
||||
# Module 02: Viewing History
|
||||
|
||||
## Learning Objectives
|
||||
|
||||
In this module, you will:
|
||||
- Understand commit history and how to navigate it
|
||||
- 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
|
||||
|
||||
### Setup
|
||||
|
||||
Run the setup script to create your challenge environment:
|
||||
|
||||
```powershell
|
||||
.\setup.ps1
|
||||
```
|
||||
|
||||
This will create a `challenge/` directory with a Git repository that already has some commit history.
|
||||
|
||||
### 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, 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.
|
||||
|
||||
**Suggested Approach:**
|
||||
|
||||
1. Navigate to the challenge directory: `cd challenge`
|
||||
2. Open `answers.md` to see the questions
|
||||
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)
|
||||
> - 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
|
||||
|
||||
- **Commit Hash**: A unique identifier (SHA-1 hash) for each commit. You can use the full hash or just the first few characters.
|
||||
- **Commit Message**: A description of what changed in that commit, written by the author.
|
||||
- **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
|
||||
|
||||
When you run `git diff` between commits, the output can look confusing at first. Here's how to read it:
|
||||
|
||||
### Example Diff Output
|
||||
|
||||
```diff
|
||||
diff --git a/app.py b/app.py
|
||||
index 1a2b3c4..5d6e7f8 100644
|
||||
--- a/app.py
|
||||
+++ b/app.py
|
||||
@@ -1,5 +1,7 @@
|
||||
# app.py - Main application file
|
||||
+from auth import login, logout
|
||||
|
||||
def main():
|
||||
print("Welcome to My App!")
|
||||
- # Application initialization code here
|
||||
+ login("user", "password")
|
||||
pass
|
||||
```
|
||||
|
||||
### Breaking It Down
|
||||
|
||||
**1. File Header**
|
||||
```diff
|
||||
diff --git a/app.py b/app.py
|
||||
```
|
||||
- Shows which file is being compared
|
||||
- `a/app.py` = old version (before)
|
||||
- `b/app.py` = new version (after)
|
||||
|
||||
**2. Metadata**
|
||||
```diff
|
||||
index 1a2b3c4..5d6e7f8 100644
|
||||
--- a/app.py
|
||||
+++ b/app.py
|
||||
```
|
||||
- `---` indicates the old version
|
||||
- `+++` indicates the new version
|
||||
- The hashes (1a2b3c4, 5d6e7f8) are internal Git identifiers
|
||||
|
||||
**3. Change Location (Hunk Header)**
|
||||
```diff
|
||||
@@ -1,5 +1,7 @@
|
||||
```
|
||||
- `@@ -1,5 +1,7 @@` tells you where changes occurred
|
||||
- `-1,5` = in the old file, starting at line 1, showing 5 lines
|
||||
- `+1,7` = in the new file, starting at line 1, showing 7 lines
|
||||
- The file grew by 2 lines (from 5 to 7)
|
||||
|
||||
**4. The Actual Changes**
|
||||
|
||||
Lines are prefixed with symbols:
|
||||
- ` ` (space) = unchanged line (context)
|
||||
- `-` (minus) = line removed from old version (shown in red in terminal)
|
||||
- `+` (plus) = line added in new version (shown in green in terminal)
|
||||
|
||||
In our example:
|
||||
```diff
|
||||
# app.py - Main application file ← unchanged
|
||||
+from auth import login, logout ← added (new)
|
||||
|
||||
def main(): ← unchanged
|
||||
print("Welcome to My App!") ← unchanged
|
||||
- # Application initialization code here ← removed (old)
|
||||
+ login("user", "password") ← added (new)
|
||||
pass ← unchanged
|
||||
```
|
||||
|
||||
### Reading Multiple Files
|
||||
|
||||
If multiple files changed, you'll see multiple diff sections:
|
||||
|
||||
```diff
|
||||
diff --git a/app.py b/app.py
|
||||
[changes to app.py]
|
||||
|
||||
diff --git a/auth.py b/auth.py
|
||||
[changes to auth.py]
|
||||
```
|
||||
|
||||
### Pro Tips
|
||||
|
||||
- **Context lines**: Unchanged lines around changes help you understand where the change happened
|
||||
- **Color coding**: In your terminal, deletions are usually red, additions are green
|
||||
- **No newline warning**: If you see `\ No newline at end of file`, it means the file doesn't end with a newline character (usually not important for beginners)
|
||||
- **Binary files**: For images or other binary files, Git just says "Binary files differ"
|
||||
|
||||
### Try It Yourself
|
||||
|
||||
In this module's challenge, you'll use:
|
||||
```bash
|
||||
# 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
|
||||
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
|
||||
```
|
||||
|
||||
### 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:
|
||||
|
||||
```powershell
|
||||
.\verify.ps1
|
||||
```
|
||||
|
||||
The verification script will check that your answers contain the expected information.
|
||||
|
||||
## Need to Start Over?
|
||||
|
||||
If you want to reset the challenge and start fresh:
|
||||
|
||||
```powershell
|
||||
.\reset.ps1
|
||||
```
|
||||
|
||||
This will remove the challenge directory and run the setup script again, giving you a clean slate.
|
||||
@@ -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,155 @@ 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 -->
|
||||
|
||||
---
|
||||
|
||||
---
|
||||
|
||||
## 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
|
||||
@@ -42,7 +42,7 @@ if (-not (Test-Path "answers.md")) {
|
||||
$answersLower = $answers.ToLower()
|
||||
|
||||
# Check 1: Contains "5" or "five" for commit count
|
||||
if ($answersLower -match "5|five") {
|
||||
if ($answersLower -match "5|five|fem") {
|
||||
Write-Host "[PASS] Correct commit count found" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "[FAIL] Commit count not found or incorrect" -ForegroundColor Red
|
||||
@@ -59,23 +59,40 @@ 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.py") {
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Set-Location ..
|
||||
@@ -91,7 +108,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 {
|
||||
351
01-essentials/03-branching-and-merging/README.md
Normal file
351
01-essentials/03-branching-and-merging/README.md
Normal file
@@ -0,0 +1,351 @@
|
||||
# Module 03: Branching and Merging
|
||||
|
||||
## Learning Objectives
|
||||
|
||||
By the end of this module, you will:
|
||||
- Understand what branches are and why they're useful
|
||||
- Create and switch between branches
|
||||
- Make commits on different branches
|
||||
- Merge branches together
|
||||
- Visualize branch history with `git log --graph`
|
||||
- Understand merge commits and how they work
|
||||
|
||||
## Setup
|
||||
|
||||
Create the challenge environment:
|
||||
|
||||
```pwsh
|
||||
.\setup.ps1
|
||||
```
|
||||
|
||||
This creates a repository with a realistic project history showing multiple merged feature branches.
|
||||
|
||||
## Overview
|
||||
|
||||
**Branching** lets you create independent lines of development. Think of it like parallel universes for your code - you can experiment on one branch without affecting others.
|
||||
|
||||
**Merging** combines work from different branches back together.
|
||||
|
||||
### Why Use Branches?
|
||||
|
||||
- **Experiment safely** - Try new ideas without breaking main code
|
||||
- **Work in parallel** - Multiple features can be developed simultaneously
|
||||
- **Organize work** - Each feature/fix gets its own branch
|
||||
- **Collaborate better** - Team members work on separate branches
|
||||
|
||||
## Your Task
|
||||
|
||||
### Part 1: Explore the Repository
|
||||
|
||||
First, explore the existing history to see what merging looks like:
|
||||
|
||||
```pwsh
|
||||
cd challenge
|
||||
git log --oneline --graph --all
|
||||
```
|
||||
|
||||
You'll see a visual graph showing:
|
||||
- The `main` branch timeline
|
||||
- Feature branches that split off from main
|
||||
- Merge points where branches come back together
|
||||
|
||||
**Study the graph:**
|
||||
- Look for the `*` symbols (commits)
|
||||
- Notice the `|`, `/`, and `\` characters (branch lines)
|
||||
- Find the merge commits (they have two parent lines converging)
|
||||
|
||||
**Explore the branches:**
|
||||
```pwsh
|
||||
# See all branches
|
||||
git branch --all
|
||||
|
||||
# Check which files exist on different branches
|
||||
git ls-tree --name-only feature-login
|
||||
git ls-tree --name-only feature-api
|
||||
git ls-tree --name-only main
|
||||
```
|
||||
|
||||
**View specific merges:**
|
||||
```pwsh
|
||||
# See all merge commits
|
||||
git log --merges --oneline
|
||||
|
||||
# See details of a specific merge
|
||||
git show <merge-commit-hash>
|
||||
```
|
||||
|
||||
### Part 2: Create Your Own Branch
|
||||
|
||||
Now practice creating your own branch:
|
||||
|
||||
1. Create a new branch with the name `my-feature` using `git switch -c my-feature`
|
||||
2. Check which branch you're on with `git branch`. The `*` shows which branch you're currently on.
|
||||
|
||||
### Part 3: Make Commits on Your Branch
|
||||
|
||||
1. Create a new file called `DOCS.md` and add some content. What doesn't matter, just something.
|
||||
2. Add and commit the `DOCS.md` file (use the commands from `01-basics` module).
|
||||
3. Add some more content to the `DOCS.md` file, add and commit the changes.
|
||||
4. Now check how the tree is looking with `git log --oneline --graph --all`
|
||||
|
||||
**Important:** Changes on your branch don't affect main!
|
||||
|
||||
1. Change back to the `main` branch `git switch main`
|
||||
2. Check the changes you committed before. You'll notice that they're gone!
|
||||
3. Now edit a file or create a new file (perhaps GUIDE.md, content of the file doesn't matter) and add it and commit it on your `main` branch (hint: `git add .`, `git commit -m`)
|
||||
- This way we create diverging branches. The `main` branch has changes as well as your new `my-feature` branch.
|
||||
- Run `git log --oneline --graph --all` to see how the tree is looking
|
||||
4. Switch back to your branch `git switch my-feature`
|
||||
5. The changes from the `main` branch are now gone. Check the changes you committed before. You'll notice they're back!
|
||||
|
||||
### Part 4: Merge Your Branch
|
||||
|
||||
Bring your work into main:
|
||||
|
||||
1. Go back to the main branch `git switch main`
|
||||
2. Run a `git log --oneline --graph --all` to see that the `HEAD` is on your `main` branch
|
||||
3. It might be wise to first ensure that we're using Visual Studio Code to handle merge messages. If you haven't already set `git config --global core.editor "code --wait"` this sets Visual Studio Code to be the default editor for anything Git related.
|
||||
4. Let's merge the recently created branch `git merge my-feature`
|
||||
5. Visual Studio Code should open with a commit message. In order to solidify the commit simply close the window. That tells Git that the commit message has been written and the change should be committed.
|
||||
6. Now run `git log --oneline --graph --all` and see your changes merge into the `main` branch!
|
||||
7. Now let's clean up a bit, run `git branch -d my-feature` to remove the recently merged branch.
|
||||
- If you hadn't merged the branch first this command would fail as Git will warn you that you have changes not merged into the `main` branch
|
||||
|
||||
You should see your feature branch merged into main!
|
||||
|
||||
### Part 5: Practice More
|
||||
|
||||
Create additional branches to practice:
|
||||
|
||||
```pwsh
|
||||
# Create another feature
|
||||
git switch -c another-feature
|
||||
|
||||
# Add changes and commits
|
||||
# ... your work ...
|
||||
|
||||
# Merge it
|
||||
git switch main
|
||||
git merge another-feature
|
||||
```
|
||||
|
||||
**Verify your work:**
|
||||
```pwsh
|
||||
# From the module directory (not inside challenge/)
|
||||
.\verify.ps1
|
||||
```
|
||||
|
||||
## Understanding Branches
|
||||
|
||||
### What is a Branch?
|
||||
|
||||
A **branch** is a lightweight movable pointer to a commit. When you create a branch, Git creates a new pointer - it doesn't copy all your files!
|
||||
|
||||
```
|
||||
main: A---B---C
|
||||
\
|
||||
my-feature: D---E
|
||||
```
|
||||
|
||||
- Both branches share commits A and B
|
||||
- Main has commit C
|
||||
- My-feature has commits D and E
|
||||
- They're independent!
|
||||
|
||||
### What is HEAD?
|
||||
|
||||
`HEAD` points to your current branch. It's Git's way of saying "you are here."
|
||||
|
||||
```pwsh
|
||||
# HEAD points to main
|
||||
git switch main
|
||||
|
||||
# Now HEAD points to my-feature
|
||||
git switch my-feature
|
||||
```
|
||||
|
||||
## Understanding Merging
|
||||
|
||||
### Types of Merges
|
||||
|
||||
**Three-way merge** (most common):
|
||||
```
|
||||
Before:
|
||||
main: A---B---C
|
||||
\
|
||||
feature: D---E
|
||||
|
||||
After merge:
|
||||
main: A---B---C---M
|
||||
\ /
|
||||
feature: D---E
|
||||
```
|
||||
|
||||
Git creates a merge commit `M` that has two parents (C and E).
|
||||
|
||||
**Fast-forward merge**:
|
||||
```
|
||||
Before:
|
||||
main: A---B
|
||||
\
|
||||
feature: C---D
|
||||
|
||||
After merge:
|
||||
main: A---B---C---D
|
||||
```
|
||||
|
||||
If main hasn't changed, Git just moves the pointer forward. No merge commit needed!
|
||||
|
||||
## Key Commands
|
||||
|
||||
### Branching
|
||||
|
||||
```pwsh
|
||||
# List all branches (* shows current branch)
|
||||
git branch
|
||||
|
||||
# Create a new branch
|
||||
git branch feature-name
|
||||
|
||||
# Create AND switch to new branch
|
||||
git switch -c feature-name
|
||||
|
||||
# Switch to existing branch
|
||||
git switch branch-name
|
||||
|
||||
# Switch to previous branch
|
||||
git switch -
|
||||
|
||||
# Delete a branch (only if merged)
|
||||
git branch -d feature-name
|
||||
|
||||
# Force delete a branch
|
||||
git branch -D feature-name
|
||||
```
|
||||
|
||||
### Merging
|
||||
|
||||
```pwsh
|
||||
# Merge a branch into your current branch
|
||||
git merge branch-name
|
||||
|
||||
# Abort a merge if something goes wrong
|
||||
git merge --abort
|
||||
|
||||
# Force a merge commit (even if fast-forward possible)
|
||||
git merge --no-ff branch-name
|
||||
```
|
||||
|
||||
### Viewing History
|
||||
|
||||
```pwsh
|
||||
# Visual branch graph
|
||||
git log --oneline --graph --all
|
||||
|
||||
# Compact history
|
||||
git log --oneline
|
||||
|
||||
# Only merge commits
|
||||
git log --merges
|
||||
|
||||
# Show which branches have been merged into main
|
||||
git branch --merged main
|
||||
|
||||
# Show which branches haven't been merged
|
||||
git branch --no-merged main
|
||||
```
|
||||
|
||||
## Common Workflows
|
||||
|
||||
### Creating a Feature
|
||||
|
||||
```pwsh
|
||||
# Start from main
|
||||
git switch main
|
||||
|
||||
# Create feature branch
|
||||
git switch -c feature-awesome
|
||||
|
||||
# Make changes
|
||||
echo "cool stuff" > feature.txt
|
||||
git add .
|
||||
git commit -m "Add awesome feature"
|
||||
|
||||
# More changes...
|
||||
echo "more cool stuff" >> feature.txt
|
||||
git add .
|
||||
git commit -m "Improve awesome feature"
|
||||
```
|
||||
|
||||
### Merging a Feature
|
||||
|
||||
```pwsh
|
||||
# Switch to main
|
||||
git switch main
|
||||
|
||||
# Merge your feature
|
||||
git merge feature-awesome
|
||||
|
||||
# Delete the feature branch (optional)
|
||||
git branch -d feature-awesome
|
||||
```
|
||||
|
||||
### Keeping Main Updated While Working
|
||||
|
||||
```pwsh
|
||||
# You're on feature-awesome
|
||||
git switch feature-awesome
|
||||
|
||||
# Main branch has new commits from others
|
||||
# Bring those into your feature branch
|
||||
git switch main
|
||||
git pull
|
||||
git switch feature-awesome
|
||||
git merge main
|
||||
|
||||
# Or in one command (more advanced):
|
||||
git pull origin main
|
||||
```
|
||||
|
||||
### "I want to see what changed in a merge!"
|
||||
|
||||
```pwsh
|
||||
# Show the merge commit
|
||||
git show <merge-commit-hash>
|
||||
|
||||
# Compare two branches before merging
|
||||
git diff main..feature-branch
|
||||
```
|
||||
|
||||
## Tips for Success
|
||||
|
||||
💡 **Branch often** - Branches are cheap! Create one for each feature or experiment.
|
||||
💡 **Commit before switching** - Always commit (or stash) changes before switching branches.
|
||||
💡 **Keep branches focused** - One feature per branch makes merging easier.
|
||||
💡 **Delete merged branches** - Clean up with `git branch -d branch-name` after merging.
|
||||
💡 **Use descriptive names** - `feature-login` is better than `stuff` or `branch1`.
|
||||
💡 **Visualize often** - Run `git log --oneline --graph --all` to understand your history.
|
||||
|
||||
## What You've Learned
|
||||
|
||||
After completing this module, you understand:
|
||||
|
||||
- Branches create independent lines of development
|
||||
- `git switch -c` creates a new branch
|
||||
- Changes in one branch don't affect others
|
||||
- `git merge` combines branches
|
||||
- Merge commits have two parent commits
|
||||
- `git log --graph` visualizes branch history
|
||||
- Branches are pointers, not copies of files
|
||||
|
||||
## Next Steps
|
||||
|
||||
Ready to continue? The next module covers **merge conflicts** - what happens when Git can't automatically merge changes.
|
||||
|
||||
To start over:
|
||||
```pwsh
|
||||
.\reset.ps1
|
||||
.\setup.ps1
|
||||
```
|
||||
|
||||
**Need help?** Review the commands above, or run `git status` to see what Git suggests!
|
||||
23
01-essentials/03-branching-and-merging/reset.ps1
Executable file
23
01-essentials/03-branching-and-merging/reset.ps1
Executable file
@@ -0,0 +1,23 @@
|
||||
#!/usr/bin/env pwsh
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Resets the Module 03 challenge environment.
|
||||
|
||||
.DESCRIPTION
|
||||
This script removes the challenge directory, allowing you to start fresh.
|
||||
Run setup.ps1 again after resetting to recreate the environment.
|
||||
#>
|
||||
|
||||
Write-Host "`n=== Resetting Module 03 Challenge ===" -ForegroundColor Cyan
|
||||
|
||||
if (Test-Path "challenge") {
|
||||
Write-Host "Removing challenge directory..." -ForegroundColor Yellow
|
||||
Remove-Item -Recurse -Force "challenge"
|
||||
Write-Host "`n[SUCCESS] Challenge environment reset complete!" -ForegroundColor Green
|
||||
Write-Host "`nRun .\setup.ps1 to create a fresh challenge environment." -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
} else {
|
||||
Write-Host "`n[INFO] No challenge directory found. Nothing to reset." -ForegroundColor Yellow
|
||||
Write-Host "Run .\setup.ps1 to create the challenge environment." -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
}
|
||||
315
01-essentials/03-branching-and-merging/setup.ps1
Executable file
315
01-essentials/03-branching-and-merging/setup.ps1
Executable file
@@ -0,0 +1,315 @@
|
||||
#!/usr/bin/env pwsh
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Sets up the Module 03 challenge environment for branching and merging.
|
||||
|
||||
.DESCRIPTION
|
||||
This script creates a challenge directory with a Git repository containing
|
||||
a realistic project history with multiple merged branches. Students will see
|
||||
what branching and merging looks like in practice, then create their own
|
||||
branches to experiment.
|
||||
#>
|
||||
|
||||
Write-Host "`n=== Setting up Module 03: Branching and Merging ===" -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"
|
||||
|
||||
# Detect the default branch name (could be main, master, etc.)
|
||||
# First commit creates the branch, so we detect it after that
|
||||
$mainBranch = git branch --show-current
|
||||
if (-not $mainBranch) {
|
||||
# Fallback: Get default branch name from git config
|
||||
$mainBranch = git config --get init.defaultBranch
|
||||
if (-not $mainBranch) {
|
||||
# Ultimate fallback: use "main"
|
||||
$mainBranch = "main"
|
||||
}
|
||||
}
|
||||
Write-Host "Default branch detected: $mainBranch" -ForegroundColor Yellow
|
||||
|
||||
# ============================================================================
|
||||
# Create a realistic project history with multiple merged branches
|
||||
# ============================================================================
|
||||
Write-Host "Creating project history with multiple branches..." -ForegroundColor Cyan
|
||||
|
||||
# Initial commits on main
|
||||
$readmeContent = @"
|
||||
# My Application
|
||||
|
||||
A sample application for learning Git branching and merging.
|
||||
"@
|
||||
Set-Content -Path "README.md" -Value $readmeContent
|
||||
git add .
|
||||
git commit -m "Initial commit" | Out-Null
|
||||
|
||||
$mainContent = @"
|
||||
# main.py - Main application file
|
||||
|
||||
def main():
|
||||
print("Welcome to the Application!")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
"@
|
||||
Set-Content -Path "main.py" -Value $mainContent
|
||||
git add .
|
||||
git commit -m "Add main application file" | Out-Null
|
||||
|
||||
# ============================================================================
|
||||
# Branch 1: feature-login (will be merged)
|
||||
# ============================================================================
|
||||
Write-Host "Creating feature-login branch..." -ForegroundColor Cyan
|
||||
git switch -c feature-login | Out-Null
|
||||
|
||||
$loginContent = @"
|
||||
# login.py - User authentication
|
||||
|
||||
def login(username, password):
|
||||
"""Authenticate a user."""
|
||||
print(f"Logging in user: {username}")
|
||||
return True
|
||||
"@
|
||||
Set-Content -Path "login.py" -Value $loginContent
|
||||
git add .
|
||||
git commit -m "Add login module" | Out-Null
|
||||
|
||||
$loginContent = @"
|
||||
# login.py - User authentication
|
||||
|
||||
def validate_password(password):
|
||||
"""Validate password strength."""
|
||||
return len(password) >= 8
|
||||
|
||||
def login(username, password):
|
||||
"""Authenticate a user."""
|
||||
if not validate_password(password):
|
||||
print("Password too weak!")
|
||||
return False
|
||||
print(f"Logging in user: {username}")
|
||||
return True
|
||||
"@
|
||||
Set-Content -Path "login.py" -Value $loginContent
|
||||
git add .
|
||||
git commit -m "Add password validation" | Out-Null
|
||||
|
||||
# Switch back to main branch
|
||||
git switch $mainBranch | Out-Null
|
||||
|
||||
$appContent = @"
|
||||
# app.py - Application entry point
|
||||
|
||||
from main import main
|
||||
|
||||
def run():
|
||||
"""Run the application."""
|
||||
print("Starting application...")
|
||||
main()
|
||||
|
||||
if __name__ == "__main__":
|
||||
run()
|
||||
"@
|
||||
Set-Content -Path "app.py" -Value $appContent
|
||||
git add .
|
||||
git commit -m "Add app.py entry point" | Out-Null
|
||||
|
||||
# Merge feature-login into $mainBranch
|
||||
Write-Host "Merging feature-login into $mainBranch..." -ForegroundColor Green
|
||||
git merge feature-login --no-edit | Out-Null
|
||||
|
||||
# ============================================================================
|
||||
# Branch 2: feature-api (will be merged)
|
||||
# ============================================================================
|
||||
Write-Host "Creating feature-api branch..." -ForegroundColor Cyan
|
||||
git switch -c feature-api | Out-Null
|
||||
|
||||
$apiContent = @"
|
||||
# api.py - API endpoints
|
||||
|
||||
def get_data():
|
||||
"""Retrieve data from API."""
|
||||
return {"status": "ok", "data": []}
|
||||
|
||||
def post_data(data):
|
||||
"""Send data to API."""
|
||||
print(f"Posting data: {data}")
|
||||
return {"status": "ok"}
|
||||
"@
|
||||
Set-Content -Path "api.py" -Value $apiContent
|
||||
git add .
|
||||
git commit -m "Add API module" | Out-Null
|
||||
|
||||
$apiContent = @"
|
||||
# api.py - API endpoints
|
||||
|
||||
def get_data():
|
||||
"""Retrieve data from API."""
|
||||
return {"status": "ok", "data": []}
|
||||
|
||||
def post_data(data):
|
||||
"""Send data to API."""
|
||||
print(f"Posting data: {data}")
|
||||
return {"status": "ok"}
|
||||
|
||||
def delete_data(id):
|
||||
"""Delete data by ID."""
|
||||
print(f"Deleting data: {id}")
|
||||
return {"status": "ok"}
|
||||
"@
|
||||
Set-Content -Path "api.py" -Value $apiContent
|
||||
git add .
|
||||
git commit -m "Add delete endpoint to API" | Out-Null
|
||||
|
||||
# Switch back to main branch and add documentation
|
||||
git switch $mainBranch | Out-Null
|
||||
|
||||
$readmeContent = @"
|
||||
# My Application
|
||||
|
||||
A sample application for learning Git branching and merging.
|
||||
|
||||
## Features
|
||||
|
||||
- User authentication
|
||||
- Main application logic
|
||||
|
||||
## Setup
|
||||
|
||||
Run: python app.py
|
||||
"@
|
||||
Set-Content -Path "README.md" -Value $readmeContent
|
||||
git add .
|
||||
git commit -m "Update README with setup instructions" | Out-Null
|
||||
|
||||
# Merge feature-api into $mainBranch
|
||||
Write-Host "Merging feature-api into $mainBranch..." -ForegroundColor Green
|
||||
git merge feature-api --no-edit | Out-Null
|
||||
|
||||
# ============================================================================
|
||||
# Branch 3: feature-database (will be merged)
|
||||
# ============================================================================
|
||||
Write-Host "Creating feature-database branch..." -ForegroundColor Cyan
|
||||
git switch -c feature-database | Out-Null
|
||||
|
||||
$dbContent = @"
|
||||
# database.py - Database operations
|
||||
|
||||
class Database:
|
||||
def __init__(self):
|
||||
self.connection = None
|
||||
|
||||
def connect(self):
|
||||
"""Connect to database."""
|
||||
print("Connecting to database...")
|
||||
self.connection = True
|
||||
|
||||
def query(self, sql):
|
||||
"""Execute SQL query."""
|
||||
print(f"Executing: {sql}")
|
||||
return []
|
||||
"@
|
||||
Set-Content -Path "database.py" -Value $dbContent
|
||||
git add .
|
||||
git commit -m "Add database module" | Out-Null
|
||||
|
||||
$dbContent = @"
|
||||
# database.py - Database operations
|
||||
|
||||
class Database:
|
||||
def __init__(self):
|
||||
self.connection = None
|
||||
|
||||
def connect(self):
|
||||
"""Connect to database."""
|
||||
print("Connecting to database...")
|
||||
self.connection = True
|
||||
|
||||
def disconnect(self):
|
||||
"""Disconnect from database."""
|
||||
print("Disconnecting from database...")
|
||||
self.connection = None
|
||||
|
||||
def query(self, sql):
|
||||
"""Execute SQL query."""
|
||||
print(f"Executing: {sql}")
|
||||
return []
|
||||
"@
|
||||
Set-Content -Path "database.py" -Value $dbContent
|
||||
git add .
|
||||
git commit -m "Add disconnect method" | Out-Null
|
||||
|
||||
# Switch to main branch and add another commit (to create divergent history)
|
||||
git switch $mainBranch | Out-Null
|
||||
|
||||
$configContent = @"
|
||||
{
|
||||
"app": {
|
||||
"port": 3000,
|
||||
"debug": false
|
||||
}
|
||||
}
|
||||
"@
|
||||
Set-Content -Path "config.json" -Value $configContent
|
||||
git add .
|
||||
git commit -m "Add configuration file" | Out-Null
|
||||
|
||||
# Merge feature-database (will be three-way merge since main diverged)
|
||||
Write-Host "Merging feature-database into $mainBranch..." -ForegroundColor Green
|
||||
git merge feature-database --no-edit | Out-Null
|
||||
|
||||
# Final update on main
|
||||
$readmeContent = @"
|
||||
# My Application
|
||||
|
||||
A sample application for learning Git branching and merging.
|
||||
|
||||
## Features
|
||||
|
||||
- User authentication
|
||||
- API endpoints
|
||||
- Database operations
|
||||
- Main application logic
|
||||
|
||||
## Setup
|
||||
|
||||
Run: python app.py
|
||||
|
||||
## Requirements
|
||||
|
||||
- Python 3.6+
|
||||
"@
|
||||
Set-Content -Path "README.md" -Value $readmeContent
|
||||
git add .
|
||||
git commit -m "Update README with all features" | Out-Null
|
||||
|
||||
# Return to module directory
|
||||
Set-Location ..
|
||||
|
||||
Write-Host "`n=== Setup Complete! ===" -ForegroundColor Green
|
||||
Write-Host "`nYour challenge environment is ready in the 'challenge/' directory." -ForegroundColor Cyan
|
||||
Write-Host "`nThe repository contains a realistic project history:" -ForegroundColor Yellow
|
||||
Write-Host " - Multiple feature branches (login, api, database)" -ForegroundColor White
|
||||
Write-Host " - All branches have been merged into $mainBranch" -ForegroundColor White
|
||||
Write-Host " - View the history: git log --oneline --graph --all" -ForegroundColor White
|
||||
Write-Host "`nNext steps:" -ForegroundColor Cyan
|
||||
Write-Host " 1. Read the README.md for detailed instructions" -ForegroundColor White
|
||||
Write-Host " 2. cd challenge" -ForegroundColor White
|
||||
Write-Host " 3. Explore the repository history: git log --oneline --graph --all" -ForegroundColor White
|
||||
Write-Host " 4. Create your own branches and practice merging!" -ForegroundColor White
|
||||
Write-Host ""
|
||||
177
01-essentials/03-branching-and-merging/verify.ps1
Executable file
177
01-essentials/03-branching-and-merging/verify.ps1
Executable file
@@ -0,0 +1,177 @@
|
||||
#!/usr/bin/env pwsh
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Verifies the Module 03 challenge solution.
|
||||
|
||||
.DESCRIPTION
|
||||
This script checks that you've practiced branching and merging by:
|
||||
- Creating at least one new branch (beyond the example branches)
|
||||
- Making commits on your branch
|
||||
- Merging your branch into main
|
||||
#>
|
||||
|
||||
$script:allChecksPassed = $true
|
||||
|
||||
# ============================================================================
|
||||
# Helper Functions
|
||||
# ============================================================================
|
||||
|
||||
function Write-Pass {
|
||||
param([string]$Message)
|
||||
Write-Host "[PASS] $Message" -ForegroundColor Green
|
||||
}
|
||||
|
||||
function Write-Fail {
|
||||
param([string]$Message)
|
||||
Write-Host "[FAIL] $Message" -ForegroundColor Red
|
||||
$script:allChecksPassed = $false
|
||||
}
|
||||
|
||||
function Write-Hint {
|
||||
param([string]$Message)
|
||||
Write-Host "[HINT] $Message" -ForegroundColor Yellow
|
||||
}
|
||||
|
||||
function Write-Info {
|
||||
param([string]$Message)
|
||||
Write-Host "[INFO] $Message" -ForegroundColor Cyan
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Check challenge directory exists
|
||||
# ============================================================================
|
||||
|
||||
if (-not (Test-Path "challenge")) {
|
||||
Write-Host "[ERROR] Challenge directory not found." -ForegroundColor Red
|
||||
Write-Host "Run .\setup.ps1 first to create the challenge environment." -ForegroundColor Yellow
|
||||
exit 1
|
||||
}
|
||||
|
||||
Push-Location "challenge"
|
||||
|
||||
if (-not (Test-Path ".git")) {
|
||||
Write-Host "[ERROR] Not a git repository." -ForegroundColor Red
|
||||
Write-Host "Run ..\setup.ps1 first to create the challenge environment." -ForegroundColor Yellow
|
||||
Pop-Location
|
||||
exit 1
|
||||
}
|
||||
|
||||
Write-Host "`n=== Verifying Module 03: Branching and Merging ===" -ForegroundColor Cyan
|
||||
|
||||
# ============================================================================
|
||||
# Detect the main branch name (could be main, master, etc.)
|
||||
# ============================================================================
|
||||
# Try to get the default branch from remote origin first
|
||||
$mainBranch = git symbolic-ref refs/remotes/origin/HEAD 2>$null | Split-Path -Leaf
|
||||
if (-not $mainBranch) {
|
||||
# Fallback: try to detect from local branches
|
||||
$allBranches = git branch --list 2>$null | ForEach-Object { $_.Trim('* ') }
|
||||
if ($allBranches -contains "main") {
|
||||
$mainBranch = "main"
|
||||
} elseif ($allBranches -contains "master") {
|
||||
$mainBranch = "master"
|
||||
} else {
|
||||
# Get the default branch from git config
|
||||
$mainBranch = git config --get init.defaultBranch
|
||||
if (-not $mainBranch) {
|
||||
# Ultimate fallback: use the first branch
|
||||
$mainBranch = $allBranches | Select-Object -First 1
|
||||
if (-not $mainBranch) { $mainBranch = "main" }
|
||||
}
|
||||
}
|
||||
}
|
||||
Write-Host "Detected main branch: $mainBranch" -ForegroundColor Cyan
|
||||
|
||||
# ============================================================================
|
||||
# Count initial setup commits (should be 15 commits from setup)
|
||||
# ============================================================================
|
||||
$initialCommitCount = 15
|
||||
|
||||
# ============================================================================
|
||||
# Check for new commits beyond setup
|
||||
# ============================================================================
|
||||
Write-Host "`nChecking your work..." -ForegroundColor Cyan
|
||||
|
||||
$totalCommits = [int](git rev-list --count HEAD 2>$null)
|
||||
|
||||
if ($totalCommits -gt $initialCommitCount) {
|
||||
$newCommits = $totalCommits - $initialCommitCount
|
||||
Write-Pass "Found $newCommits new commit(s) beyond the initial setup"
|
||||
} else {
|
||||
Write-Fail "No new commits found"
|
||||
Write-Hint "Create a branch, make some commits, and merge it into main"
|
||||
Write-Hint "Example: git switch -c my-feature"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Check for branches (excluding the example branches)
|
||||
# ============================================================================
|
||||
$allBranches = git branch --list 2>$null | ForEach-Object { $_.Trim('* ') }
|
||||
$exampleBranches = @($mainBranch, 'feature-login', 'feature-api', 'feature-database')
|
||||
$studentBranches = $allBranches | Where-Object { $_ -notin $exampleBranches }
|
||||
|
||||
if ($studentBranches.Count -gt 0) {
|
||||
Write-Pass "Created $($studentBranches.Count) new branch(es): $($studentBranches -join ', ')"
|
||||
} else {
|
||||
Write-Info "No new branches found (it's OK if you deleted them after merging)"
|
||||
Write-Hint "To practice: git switch -c your-branch-name"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Check for merge commits by the student
|
||||
# ============================================================================
|
||||
$setupUser = "Workshop Student"
|
||||
$mergeCommits = git log --merges --format="%s" 2>$null
|
||||
|
||||
# Count how many merge commits exist beyond the initial 3
|
||||
$totalMerges = ($mergeCommits | Measure-Object).Count
|
||||
$setupMerges = 3 # feature-login, feature-api, feature-database
|
||||
|
||||
if ($totalMerges -gt $setupMerges) {
|
||||
$studentMerges = $totalMerges - $setupMerges
|
||||
Write-Pass "Performed $studentMerges merge(s) of your own work"
|
||||
} else {
|
||||
Write-Fail "No merge commits found beyond the example merges"
|
||||
Write-Hint "Create a branch, add commits, then merge it: git merge your-branch-name"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Check current branch
|
||||
# ============================================================================
|
||||
$currentBranch = git branch --show-current 2>$null
|
||||
if ($currentBranch -eq $mainBranch) {
|
||||
Write-Pass "Currently on $mainBranch branch"
|
||||
} else {
|
||||
Write-Info "Currently on '$currentBranch' branch"
|
||||
Write-Hint "Typically you merge feature branches INTO $mainBranch"
|
||||
}
|
||||
|
||||
Pop-Location
|
||||
|
||||
# ============================================================================
|
||||
# Final summary
|
||||
# ============================================================================
|
||||
Write-Host ""
|
||||
if ($script:allChecksPassed) {
|
||||
Write-Host "=========================================" -ForegroundColor Green
|
||||
Write-Host " CONGRATULATIONS! CHALLENGE PASSED!" -ForegroundColor Green
|
||||
Write-Host "=========================================" -ForegroundColor Green
|
||||
Write-Host "`nYou've successfully practiced:" -ForegroundColor Cyan
|
||||
Write-Host " ✓ Creating branches" -ForegroundColor White
|
||||
Write-Host " ✓ Making commits on branches" -ForegroundColor White
|
||||
Write-Host " ✓ Merging branches together" -ForegroundColor White
|
||||
Write-Host "`nReady for the next module!" -ForegroundColor Green
|
||||
Write-Host ""
|
||||
exit 0
|
||||
} else {
|
||||
Write-Host "[SUMMARY] Some checks failed. Review the hints above." -ForegroundColor Red
|
||||
Write-Host ""
|
||||
Write-Host "Quick guide:" -ForegroundColor Cyan
|
||||
Write-Host " 1. Create a branch: git switch -c my-feature" -ForegroundColor White
|
||||
Write-Host " 2. Make changes and commit them" -ForegroundColor White
|
||||
Write-Host " 3. Switch to $mainBranch : git switch $mainBranch" -ForegroundColor White
|
||||
Write-Host " 4. Merge your branch: git merge my-feature" -ForegroundColor White
|
||||
Write-Host " 5. Run this verify script again" -ForegroundColor White
|
||||
Write-Host ""
|
||||
exit 1
|
||||
}
|
||||
487
01-essentials/04-merge-conflict/README.md
Normal file
487
01-essentials/04-merge-conflict/README.md
Normal file
@@ -0,0 +1,487 @@
|
||||
# Module 04: Merge Conflicts
|
||||
|
||||
## Learning Objectives
|
||||
|
||||
By the end of this module, you will:
|
||||
- Understand what merge conflicts are and why they occur
|
||||
- Use `git diff` to discover changes between branches
|
||||
- Identify merge conflicts in your repository
|
||||
- Read and interpret conflict markers (`<<<<<<<`, `=======`, `>>>>>>>`)
|
||||
- Resolve merge conflicts manually
|
||||
- Complete a merge after resolving conflicts
|
||||
|
||||
## Setup
|
||||
|
||||
Create the challenge environment:
|
||||
|
||||
```bash
|
||||
.\setup.ps1
|
||||
```
|
||||
|
||||
This creates a repository with two feature branches that have conflicting changes.
|
||||
|
||||
## Overview
|
||||
|
||||
A **merge conflict** occurs when Git cannot automatically combine changes because both branches modified the same part of the same file in different ways.
|
||||
|
||||
**When do conflicts happen?**
|
||||
- ✅ Two branches modify the same lines in a file
|
||||
- ✅ One branch deletes a file that another branch modifies
|
||||
- ✅ Complex changes Git can't merge automatically
|
||||
- ❌ Different files are changed (no conflict!)
|
||||
- ❌ Different parts of the same file are changed (no conflict!)
|
||||
|
||||
**Don't fear conflicts!** They're a normal part of collaborative development. Git just needs your help to decide what the final code should look like.
|
||||
|
||||
## Your Task
|
||||
|
||||
### Part 1: Discover the Changes
|
||||
|
||||
Before merging, it's good practice to see what each branch changed:
|
||||
|
||||
```bash
|
||||
cd challenge
|
||||
|
||||
# Check which branch you're on
|
||||
git branch
|
||||
|
||||
# View all branches
|
||||
git branch --all
|
||||
```
|
||||
|
||||
You'll see three branches: `main`, `add-timeout`, and `add-debug`.
|
||||
|
||||
**Discover what each branch changed:**
|
||||
|
||||
```bash
|
||||
# Compare main with add-timeout
|
||||
git diff main add-timeout
|
||||
|
||||
# Compare main with add-debug
|
||||
git diff main add-debug
|
||||
|
||||
# Compare the two feature branches directly
|
||||
git diff add-timeout add-debug
|
||||
```
|
||||
|
||||
**What did you discover?**
|
||||
- Both branches modified `config.json`
|
||||
- They both added a line in the same location (after `"port": 3000`)
|
||||
- One adds `"timeout": 5000`
|
||||
- The other adds `"debug": true`
|
||||
|
||||
This is a recipe for a conflict!
|
||||
|
||||
### Part 2: Merge the First Branch (No Conflict)
|
||||
|
||||
Let's merge `add-timeout` first:
|
||||
|
||||
```bash
|
||||
# Make sure you're on main
|
||||
git switch main
|
||||
|
||||
# Merge the first branch
|
||||
git merge add-timeout
|
||||
```
|
||||
|
||||
✅ **Success!** This merge works because main hasn't changed since add-timeout was created.
|
||||
|
||||
```bash
|
||||
# View the updated config
|
||||
cat config.json
|
||||
|
||||
# Check the history
|
||||
git log --oneline --graph --all
|
||||
```
|
||||
|
||||
### Part 3: Try to Merge the Second Branch (Conflict!)
|
||||
|
||||
Now let's try to merge `add-debug`:
|
||||
|
||||
```bash
|
||||
# Still on main
|
||||
git merge add-debug
|
||||
```
|
||||
|
||||
💥 **Boom!** You'll see:
|
||||
|
||||
```
|
||||
Auto-merging config.json
|
||||
CONFLICT (content): Merge conflict in config.json
|
||||
Automatic merge failed; fix conflicts and then commit the result.
|
||||
```
|
||||
|
||||
**Don't panic!** This is expected. Git is asking for your help.
|
||||
|
||||
### Part 4: Check the Status
|
||||
|
||||
```bash
|
||||
git status
|
||||
```
|
||||
|
||||
You'll see:
|
||||
|
||||
```
|
||||
On branch main
|
||||
You have unmerged paths.
|
||||
(fix conflicts and run "git commit")
|
||||
(use "git merge --abort" to abort the merge)
|
||||
|
||||
Unmerged paths:
|
||||
(use "git add <file>..." to mark resolution)
|
||||
both modified: config.json
|
||||
```
|
||||
|
||||
This tells you that `config.json` needs your attention!
|
||||
|
||||
### Part 5: Open and Examine the Conflicted File
|
||||
|
||||
Open `config.json` in your text editor:
|
||||
|
||||
```bash
|
||||
# On Windows
|
||||
notepad config.json
|
||||
|
||||
# Or use VS Code
|
||||
code config.json
|
||||
```
|
||||
|
||||
You'll see special **conflict markers**:
|
||||
|
||||
```json
|
||||
{
|
||||
"app": {
|
||||
"name": "MyApp",
|
||||
"version": "1.0.0",
|
||||
"port": 3000,
|
||||
<<<<<<< HEAD
|
||||
"timeout": 5000
|
||||
=======
|
||||
"debug": true
|
||||
>>>>>>> add-debug
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Part 6: Understand the Conflict Markers
|
||||
|
||||
Let's break down what you're seeing:
|
||||
|
||||
```
|
||||
<<<<<<< HEAD
|
||||
"timeout": 5000 ← Your current branch (main, which has add-timeout merged)
|
||||
=======
|
||||
"debug": true ← The branch you're merging (add-debug)
|
||||
>>>>>>> add-debug
|
||||
```
|
||||
|
||||
**What each marker means:**
|
||||
- `<<<<<<< HEAD` - Start of your changes (current branch)
|
||||
- `=======` - Separator between the two versions
|
||||
- `>>>>>>> add-debug` - End of their changes (branch being merged)
|
||||
|
||||
### Part 7: Resolve the Conflict
|
||||
|
||||
You have three options:
|
||||
|
||||
**Option 1: Keep ONLY your changes (timeout)**
|
||||
```json
|
||||
"timeout": 5000
|
||||
```
|
||||
|
||||
**Option 2: Keep ONLY their changes (debug)**
|
||||
```json
|
||||
"debug": true
|
||||
```
|
||||
|
||||
**Option 3: Keep BOTH changes** ← **Do this!**
|
||||
```json
|
||||
"timeout": 5000,
|
||||
"debug": true
|
||||
```
|
||||
|
||||
### Part 8: Edit the File
|
||||
|
||||
For this challenge, we want **both settings**, so:
|
||||
|
||||
1. Delete ALL the conflict markers:
|
||||
- Remove `<<<<<<< HEAD`
|
||||
- Remove `=======`
|
||||
- Remove `>>>>>>> add-debug`
|
||||
|
||||
2. Keep both settings:
|
||||
|
||||
**Before (with conflict markers):**
|
||||
```json
|
||||
{
|
||||
"app": {
|
||||
"name": "MyApp",
|
||||
"version": "1.0.0",
|
||||
"port": 3000,
|
||||
<<<<<<< HEAD
|
||||
"timeout": 5000
|
||||
=======
|
||||
"debug": true
|
||||
>>>>>>> add-debug
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**After (resolved):**
|
||||
```json
|
||||
{
|
||||
"app": {
|
||||
"name": "MyApp",
|
||||
"version": "1.0.0",
|
||||
"port": 3000,
|
||||
"timeout": 5000,
|
||||
"debug": true
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Important:**
|
||||
- Remove ALL markers
|
||||
- Add a comma after `"timeout": 5000` (for valid JSON)
|
||||
- Ensure the file is valid JSON
|
||||
|
||||
3. Save the file
|
||||
|
||||
### Part 9: Mark the Conflict as Resolved
|
||||
|
||||
Tell Git you've resolved the conflict:
|
||||
|
||||
```bash
|
||||
# Stage the resolved file
|
||||
git add config.json
|
||||
|
||||
# Check status
|
||||
git status
|
||||
```
|
||||
|
||||
You should see:
|
||||
|
||||
```
|
||||
On branch main
|
||||
All conflicts fixed but you are still merging.
|
||||
(use "git commit" to conclude merge)
|
||||
```
|
||||
|
||||
Perfect! Git confirms the conflict is resolved.
|
||||
|
||||
### Part 10: Complete the Merge
|
||||
|
||||
Commit the merge:
|
||||
|
||||
```bash
|
||||
git commit
|
||||
```
|
||||
|
||||
Git will open an editor with a default merge message. You can accept it or customize it, then save and close.
|
||||
|
||||
**Done!** Your merge is complete!
|
||||
|
||||
```bash
|
||||
# View the final result
|
||||
cat config.json
|
||||
|
||||
# View the history
|
||||
git log --oneline --graph --all
|
||||
```
|
||||
|
||||
You should see both `timeout` and `debug` in the config!
|
||||
|
||||
### Part 11: Verify Your Solution
|
||||
|
||||
From the module directory (not inside challenge/):
|
||||
|
||||
```bash
|
||||
.\verify.ps1
|
||||
```
|
||||
|
||||
## Understanding Conflict Markers
|
||||
|
||||
### Anatomy of a Conflict
|
||||
|
||||
```
|
||||
<<<<<<< HEAD ← Marker: Start of your version
|
||||
Your changes here
|
||||
======= ← Marker: Separator
|
||||
Their changes here
|
||||
>>>>>>> branch-name ← Marker: End of their version
|
||||
```
|
||||
|
||||
### Common Conflict Patterns
|
||||
|
||||
**Simple conflict:**
|
||||
```
|
||||
<<<<<<< HEAD
|
||||
print("Hello")
|
||||
=======
|
||||
print("Hi")
|
||||
>>>>>>> feature
|
||||
```
|
||||
Decision: Which greeting do you want?
|
||||
|
||||
**Both are needed:**
|
||||
```
|
||||
<<<<<<< HEAD
|
||||
timeout: 5000
|
||||
=======
|
||||
debug: true
|
||||
>>>>>>> feature
|
||||
```
|
||||
Decision: Keep both (add comma)!
|
||||
|
||||
**Deletion conflict:**
|
||||
```
|
||||
<<<<<<< HEAD
|
||||
# Function deleted on your branch
|
||||
=======
|
||||
def old_function():
|
||||
pass
|
||||
>>>>>>> feature
|
||||
```
|
||||
Decision: Delete or keep the function?
|
||||
|
||||
## Common Mistakes to Avoid
|
||||
|
||||
❌ **Forgetting to remove conflict markers**
|
||||
```json
|
||||
<<<<<<< HEAD ← Don't leave these in!
|
||||
"timeout": 5000,
|
||||
"debug": true
|
||||
>>>>>>> add-debug ← Don't leave these in!
|
||||
```
|
||||
This breaks your code! Always remove ALL markers.
|
||||
|
||||
❌ **Committing without staging**
|
||||
```bash
|
||||
git commit # Error! You didn't add the file
|
||||
```
|
||||
Always `git add` the resolved file first!
|
||||
|
||||
❌ **Keeping only one side when both are needed**
|
||||
If you delete one setting, you lose that work!
|
||||
|
||||
❌ **Breaking syntax**
|
||||
```json
|
||||
"timeout": 5000 ← Missing comma!
|
||||
"debug": true
|
||||
```
|
||||
Always verify your file is valid after resolving!
|
||||
|
||||
❌ **Not testing the result**
|
||||
Always check that your resolved code works!
|
||||
|
||||
## Aborting a Merge
|
||||
|
||||
Changed your mind? You can abort the merge anytime before committing:
|
||||
|
||||
```bash
|
||||
git merge --abort
|
||||
```
|
||||
|
||||
This returns your repository to the state before you started the merge. No harm done!
|
||||
|
||||
## Key Commands
|
||||
|
||||
```bash
|
||||
# Discover changes before merging
|
||||
git diff branch1 branch2
|
||||
|
||||
# Attempt a merge
|
||||
git merge <branch-name>
|
||||
|
||||
# Check which files have conflicts
|
||||
git status
|
||||
|
||||
# Abort the merge and start over
|
||||
git merge --abort
|
||||
|
||||
# After resolving conflicts:
|
||||
git add <resolved-file>
|
||||
git commit
|
||||
|
||||
# View conflicts in a different style
|
||||
git diff --ours # Your changes
|
||||
git diff --theirs # Their changes
|
||||
git diff --base # Original version
|
||||
```
|
||||
|
||||
## Pro Tips
|
||||
|
||||
💡 **Use `git diff` first**
|
||||
Always compare branches before merging:
|
||||
```bash
|
||||
git diff main..feature-branch
|
||||
```
|
||||
|
||||
💡 **Prevent conflicts**
|
||||
- Pull changes frequently
|
||||
- Communicate with your team about who's working on what
|
||||
- Keep branches short-lived and merge often
|
||||
|
||||
💡 **Make conflicts easier**
|
||||
- Work on different files when possible
|
||||
- Make small, focused commits
|
||||
- If editing the same file, coordinate with teammates
|
||||
|
||||
💡 **When stuck**
|
||||
- Read the conflict markers carefully
|
||||
- Look at `git log` to understand what each side changed
|
||||
- Use `git diff` to see the changes
|
||||
- Ask a teammate to review your resolution
|
||||
- Use a merge tool: `git mergetool`
|
||||
|
||||
## Merge Tools
|
||||
|
||||
Git supports visual merge tools that make resolving conflicts easier:
|
||||
|
||||
```bash
|
||||
# Configure a merge tool (one-time setup)
|
||||
git config --global merge.tool vscode # or meld, kdiff3, etc.
|
||||
|
||||
# Use the merge tool during a conflict
|
||||
git mergetool
|
||||
```
|
||||
|
||||
This opens a visual interface showing both versions side-by-side.
|
||||
|
||||
## Real-World Scenario
|
||||
|
||||
This exercise simulates a common real-world situation:
|
||||
|
||||
**Scenario:** Two developers working on the same file
|
||||
- Alice adds a timeout configuration
|
||||
- Bob adds debug mode configuration
|
||||
- Both push their changes
|
||||
- When Bob tries to merge, he gets a conflict
|
||||
- Bob resolves it by keeping both changes
|
||||
- Everyone's work is preserved!
|
||||
|
||||
This happens all the time in team development. Conflicts are normal!
|
||||
|
||||
## What You've Learned
|
||||
|
||||
After completing this module, you understand:
|
||||
|
||||
- ✅ Merge conflicts happen when the same lines are changed differently
|
||||
- ✅ `git diff` helps you discover changes before merging
|
||||
- ✅ Conflict markers show both versions
|
||||
- ✅ You decide what the final code should look like
|
||||
- ✅ Remove all markers before committing
|
||||
- ✅ Test your resolution to ensure it works
|
||||
- ✅ Conflicts are normal and easy to resolve with practice
|
||||
|
||||
## Next Steps
|
||||
|
||||
Ready to continue? The next module covers **cherry-picking** - selectively applying specific commits from one branch to another.
|
||||
|
||||
To start over:
|
||||
```bash
|
||||
.\reset.ps1
|
||||
.\setup.ps1
|
||||
```
|
||||
|
||||
**Need help?** Review the steps above, or run `git status` to see what Git suggests!
|
||||
23
01-essentials/04-merge-conflict/reset.ps1
Normal file
23
01-essentials/04-merge-conflict/reset.ps1
Normal file
@@ -0,0 +1,23 @@
|
||||
#!/usr/bin/env pwsh
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Resets the Module 04 challenge environment.
|
||||
|
||||
.DESCRIPTION
|
||||
This script removes the challenge directory, allowing you to start fresh.
|
||||
Run setup.ps1 again after resetting to recreate the environment.
|
||||
#>
|
||||
|
||||
Write-Host "`n=== Resetting Module 04 Challenge ===" -ForegroundColor Cyan
|
||||
|
||||
if (Test-Path "challenge") {
|
||||
Write-Host "Removing challenge directory..." -ForegroundColor Yellow
|
||||
Remove-Item -Recurse -Force "challenge"
|
||||
Write-Host "`n[SUCCESS] Challenge environment reset complete!" -ForegroundColor Green
|
||||
Write-Host "`nRun .\setup.ps1 to create a fresh challenge environment." -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
} else {
|
||||
Write-Host "`n[INFO] No challenge directory found. Nothing to reset." -ForegroundColor Yellow
|
||||
Write-Host "Run .\setup.ps1 to create the challenge environment." -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
}
|
||||
138
01-essentials/04-merge-conflict/setup.ps1
Normal file
138
01-essentials/04-merge-conflict/setup.ps1
Normal file
@@ -0,0 +1,138 @@
|
||||
#!/usr/bin/env pwsh
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Sets up the Module 04 challenge environment for merge conflicts.
|
||||
|
||||
.DESCRIPTION
|
||||
This script creates a challenge directory with a Git repository containing
|
||||
two feature branches that have conflicting changes to the same file.
|
||||
Students will learn to identify, understand, and resolve merge conflicts.
|
||||
#>
|
||||
|
||||
Write-Host "`n=== Setting up Module 04: Merge Conflicts ===" -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"
|
||||
|
||||
# Detect the default branch name (could be main, master, etc.)
|
||||
# First commit creates the branch, so we detect it after that
|
||||
$mainBranch = git branch --show-current
|
||||
if (-not $mainBranch) {
|
||||
# Fallback: Get default branch name from git config
|
||||
$mainBranch = git config --get init.defaultBranch
|
||||
if (-not $mainBranch) {
|
||||
# Ultimate fallback: use "main"
|
||||
$mainBranch = "main"
|
||||
}
|
||||
}
|
||||
Write-Host "Default branch detected: $mainBranch" -ForegroundColor Yellow
|
||||
|
||||
# ============================================================================
|
||||
# Create base project
|
||||
# ============================================================================
|
||||
Write-Host "Creating base project..." -ForegroundColor Cyan
|
||||
|
||||
# Initial commit with config file
|
||||
$configContent = @"
|
||||
{
|
||||
"app": {
|
||||
"name": "MyApp",
|
||||
"version": "1.0.0",
|
||||
"port": 3000
|
||||
}
|
||||
}
|
||||
"@
|
||||
Set-Content -Path "config.json" -Value $configContent
|
||||
git add .
|
||||
git commit -m "Initial commit with config" | Out-Null
|
||||
|
||||
# Add README
|
||||
$readmeContent = @"
|
||||
# My Application
|
||||
|
||||
A simple application for learning merge conflicts.
|
||||
|
||||
## Configuration
|
||||
|
||||
Edit `config.json` to configure the application.
|
||||
"@
|
||||
Set-Content -Path "README.md" -Value $readmeContent
|
||||
git add .
|
||||
git commit -m "Add README" | Out-Null
|
||||
|
||||
# ============================================================================
|
||||
# Branch 1: add-timeout (adds timeout setting)
|
||||
# ============================================================================
|
||||
Write-Host "Creating add-timeout branch..." -ForegroundColor Cyan
|
||||
git switch -c add-timeout | Out-Null
|
||||
|
||||
$timeoutConfig = @"
|
||||
{
|
||||
"app": {
|
||||
"name": "MyApp",
|
||||
"version": "1.0.0",
|
||||
"port": 3000,
|
||||
"timeout": 5000
|
||||
}
|
||||
}
|
||||
"@
|
||||
Set-Content -Path "config.json" -Value $timeoutConfig
|
||||
git add .
|
||||
git commit -m "Add timeout configuration" | Out-Null
|
||||
|
||||
# ============================================================================
|
||||
# Branch 2: add-debug (adds debug setting - CONFLICTS with timeout!)
|
||||
# ============================================================================
|
||||
Write-Host "Creating add-debug branch..." -ForegroundColor Cyan
|
||||
git switch $mainBranch | Out-Null
|
||||
git switch -c add-debug | Out-Null
|
||||
|
||||
$debugConfig = @"
|
||||
{
|
||||
"app": {
|
||||
"name": "MyApp",
|
||||
"version": "1.0.0",
|
||||
"port": 3000,
|
||||
"debug": true
|
||||
}
|
||||
}
|
||||
"@
|
||||
Set-Content -Path "config.json" -Value $debugConfig
|
||||
git add .
|
||||
git commit -m "Add debug mode configuration" | Out-Null
|
||||
|
||||
# Switch back to main branch
|
||||
git switch $mainBranch | Out-Null
|
||||
|
||||
# Return to module directory
|
||||
Set-Location ..
|
||||
|
||||
Write-Host "`n=== Setup Complete! ===" -ForegroundColor Green
|
||||
Write-Host "`nYour challenge environment is ready in the 'challenge/' directory." -ForegroundColor Cyan
|
||||
Write-Host "`nThe repository contains:" -ForegroundColor Yellow
|
||||
Write-Host " - $mainBranch branch: base configuration" -ForegroundColor White
|
||||
Write-Host " - add-timeout branch: adds timeout setting" -ForegroundColor White
|
||||
Write-Host " - add-debug branch: adds debug setting" -ForegroundColor White
|
||||
Write-Host "`nBoth branches modify the same part of config.json!" -ForegroundColor Red
|
||||
Write-Host "This will cause a merge conflict when you try to merge both." -ForegroundColor Red
|
||||
Write-Host "`nNext steps:" -ForegroundColor Cyan
|
||||
Write-Host " 1. Read the README.md for detailed instructions" -ForegroundColor White
|
||||
Write-Host " 2. cd challenge" -ForegroundColor White
|
||||
Write-Host " 3. Follow the guide to discover and resolve the conflict" -ForegroundColor White
|
||||
Write-Host ""
|
||||
232
01-essentials/04-merge-conflict/verify.ps1
Normal file
232
01-essentials/04-merge-conflict/verify.ps1
Normal file
@@ -0,0 +1,232 @@
|
||||
#!/usr/bin/env pwsh
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Verifies the Module 04 challenge solution.
|
||||
|
||||
.DESCRIPTION
|
||||
This script checks that you've successfully resolved the merge conflict by:
|
||||
- Merging both branches into main
|
||||
- Resolving the conflict in config.json
|
||||
- Keeping both timeout and debug settings
|
||||
- Ensuring valid JSON syntax
|
||||
#>
|
||||
|
||||
$script:allChecksPassed = $true
|
||||
|
||||
# ============================================================================
|
||||
# Helper Functions
|
||||
# ============================================================================
|
||||
|
||||
function Write-Pass {
|
||||
param([string]$Message)
|
||||
Write-Host "[PASS] $Message" -ForegroundColor Green
|
||||
}
|
||||
|
||||
function Write-Fail {
|
||||
param([string]$Message)
|
||||
Write-Host "[FAIL] $Message" -ForegroundColor Red
|
||||
$script:allChecksPassed = $false
|
||||
}
|
||||
|
||||
function Write-Hint {
|
||||
param([string]$Message)
|
||||
Write-Host "[HINT] $Message" -ForegroundColor Yellow
|
||||
}
|
||||
|
||||
function Write-Info {
|
||||
param([string]$Message)
|
||||
Write-Host "[INFO] $Message" -ForegroundColor Cyan
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Check challenge directory exists
|
||||
# ============================================================================
|
||||
|
||||
if (-not (Test-Path "challenge")) {
|
||||
Write-Host "[ERROR] Challenge directory not found." -ForegroundColor Red
|
||||
Write-Host "Run .\setup.ps1 first to create the challenge environment." -ForegroundColor Yellow
|
||||
exit 1
|
||||
}
|
||||
|
||||
Push-Location "challenge"
|
||||
|
||||
if (-not (Test-Path ".git")) {
|
||||
Write-Host "[ERROR] Not a git repository." -ForegroundColor Red
|
||||
Write-Host "Run ..\setup.ps1 first to create the challenge environment." -ForegroundColor Yellow
|
||||
Pop-Location
|
||||
exit 1
|
||||
}
|
||||
|
||||
Write-Host "`n=== Verifying Module 04: Merge Conflicts ===" -ForegroundColor Cyan
|
||||
|
||||
# ============================================================================
|
||||
# Detect the main branch name (could be main, master, etc.)
|
||||
# ============================================================================
|
||||
# Try to get the default branch from remote origin first
|
||||
$mainBranch = git symbolic-ref refs/remotes/origin/HEAD 2>$null | Split-Path -Leaf
|
||||
if (-not $mainBranch) {
|
||||
# Fallback: try to detect from local branches
|
||||
$allBranches = git branch --list 2>$null | ForEach-Object { $_.Trim('* ') }
|
||||
if ($allBranches -contains "main") {
|
||||
$mainBranch = "main"
|
||||
} elseif ($allBranches -contains "master") {
|
||||
$mainBranch = "master"
|
||||
} else {
|
||||
# Get the default branch from git config
|
||||
$mainBranch = git config --get init.defaultBranch
|
||||
if (-not $mainBranch) {
|
||||
# Ultimate fallback: use the first branch
|
||||
$mainBranch = $allBranches | Select-Object -First 1
|
||||
if (-not $mainBranch) { $mainBranch = "main" }
|
||||
}
|
||||
}
|
||||
}
|
||||
Write-Host "Detected main branch: $mainBranch" -ForegroundColor Cyan
|
||||
|
||||
# ============================================================================
|
||||
# Check current branch
|
||||
# ============================================================================
|
||||
$currentBranch = git branch --show-current 2>$null
|
||||
if ($currentBranch -eq $mainBranch) {
|
||||
Write-Pass "Currently on $mainBranch branch"
|
||||
} else {
|
||||
Write-Fail "Should be on $mainBranch branch (currently on: $currentBranch)"
|
||||
Write-Hint "Switch to $mainBranch with: git switch $mainBranch"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Check that merge is not in progress
|
||||
# ============================================================================
|
||||
if (Test-Path ".git/MERGE_HEAD") {
|
||||
Write-Fail "Merge is still in progress (conflicts not resolved)"
|
||||
Write-Hint "Resolve conflicts in config.json, then: git add config.json && git commit"
|
||||
Pop-Location
|
||||
exit 1
|
||||
} else {
|
||||
Write-Pass "No merge in progress (conflicts resolved)"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Check if config.json exists
|
||||
# ============================================================================
|
||||
if (-not (Test-Path "config.json")) {
|
||||
Write-Fail "File 'config.json' not found"
|
||||
Write-Hint "The config.json file should exist"
|
||||
Pop-Location
|
||||
exit 1
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Verify config.json is valid JSON
|
||||
# ============================================================================
|
||||
try {
|
||||
$configContent = Get-Content "config.json" -Raw
|
||||
$config = $configContent | ConvertFrom-Json -ErrorAction Stop
|
||||
Write-Pass "File 'config.json' is valid JSON"
|
||||
} catch {
|
||||
Write-Fail "File 'config.json' is not valid JSON"
|
||||
Write-Hint "Make sure you removed all conflict markers (<<<<<<<, =======, >>>>>>>)"
|
||||
Write-Hint "Check for missing commas or brackets"
|
||||
Pop-Location
|
||||
exit 1
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Check for conflict markers
|
||||
# ============================================================================
|
||||
if ($configContent -match '<<<<<<<|=======|>>>>>>>') {
|
||||
Write-Fail "Conflict markers still present in config.json"
|
||||
Write-Hint "Remove all conflict markers (<<<<<<<, =======, >>>>>>>)"
|
||||
Pop-Location
|
||||
exit 1
|
||||
} else {
|
||||
Write-Pass "No conflict markers in config.json"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Verify both settings are present (timeout and debug)
|
||||
# ============================================================================
|
||||
if ($config.app.timeout -eq 5000) {
|
||||
Write-Pass "Timeout setting preserved (5000)"
|
||||
} else {
|
||||
Write-Fail "Timeout setting missing or incorrect"
|
||||
Write-Hint "Keep the timeout: 5000 setting from add-timeout branch"
|
||||
}
|
||||
|
||||
if ($config.app.debug -eq $true) {
|
||||
Write-Pass "Debug setting preserved (true)"
|
||||
} else {
|
||||
Write-Fail "Debug setting missing or incorrect"
|
||||
Write-Hint "Keep the debug: true setting from add-debug branch"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Verify both branches were merged
|
||||
# ============================================================================
|
||||
$addTimeoutMerged = git log --oneline --grep="add-timeout" 2>$null | Select-String "Merge"
|
||||
$addDebugMerged = git log --oneline --grep="add-debug" 2>$null | Select-String "Merge"
|
||||
|
||||
if ($addTimeoutMerged) {
|
||||
Write-Pass "add-timeout branch has been merged"
|
||||
} else {
|
||||
# Check if it was a fast-forward merge (commits exist but no merge commit)
|
||||
$timeoutCommit = git log --oneline --grep="Add timeout configuration" 2>$null
|
||||
if ($timeoutCommit) {
|
||||
Write-Pass "add-timeout branch changes are in main"
|
||||
} else {
|
||||
Write-Fail "add-timeout branch not merged"
|
||||
Write-Hint "Merge with: git merge add-timeout"
|
||||
}
|
||||
}
|
||||
|
||||
if ($addDebugMerged) {
|
||||
Write-Pass "add-debug branch has been merged"
|
||||
} else {
|
||||
Write-Fail "add-debug branch not merged"
|
||||
Write-Hint "Merge with: git merge add-debug"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Check commit count (should have both merges)
|
||||
# ============================================================================
|
||||
$totalCommits = [int](git rev-list --count HEAD 2>$null)
|
||||
if ($totalCommits -ge 5) {
|
||||
Write-Pass "Repository has $totalCommits commits (all merges complete)"
|
||||
} else {
|
||||
Write-Info "Repository has $totalCommits commits"
|
||||
Write-Hint "Make sure both branches are merged"
|
||||
}
|
||||
|
||||
Pop-Location
|
||||
|
||||
# ============================================================================
|
||||
# Final summary
|
||||
# ============================================================================
|
||||
Write-Host ""
|
||||
if ($script:allChecksPassed) {
|
||||
Write-Host "=========================================" -ForegroundColor Green
|
||||
Write-Host " CONGRATULATIONS! CHALLENGE PASSED!" -ForegroundColor Green
|
||||
Write-Host "=========================================" -ForegroundColor Green
|
||||
Write-Host "`nYou've successfully:" -ForegroundColor Cyan
|
||||
Write-Host " ✓ Discovered changes using git diff" -ForegroundColor White
|
||||
Write-Host " ✓ Merged the first branch" -ForegroundColor White
|
||||
Write-Host " ✓ Encountered a merge conflict" -ForegroundColor White
|
||||
Write-Host " ✓ Resolved the conflict by keeping both changes" -ForegroundColor White
|
||||
Write-Host " ✓ Completed the merge" -ForegroundColor White
|
||||
Write-Host "`nYou're now ready to handle merge conflicts in real projects!" -ForegroundColor Green
|
||||
Write-Host ""
|
||||
exit 0
|
||||
} else {
|
||||
Write-Host "[SUMMARY] Some checks failed. Review the hints above." -ForegroundColor Red
|
||||
Write-Host ""
|
||||
Write-Host "Quick guide:" -ForegroundColor Cyan
|
||||
Write-Host " 1. Make sure you're on $mainBranch : git switch $mainBranch" -ForegroundColor White
|
||||
Write-Host " 2. Merge first branch: git merge add-timeout" -ForegroundColor White
|
||||
Write-Host " 3. Merge second branch: git merge add-debug" -ForegroundColor White
|
||||
Write-Host " 4. Resolve conflict: edit config.json, remove markers, keep both settings" -ForegroundColor White
|
||||
Write-Host " 5. Stage resolved file: git add config.json" -ForegroundColor White
|
||||
Write-Host " 6. Complete merge: git commit" -ForegroundColor White
|
||||
Write-Host " 7. Run this verify script again" -ForegroundColor White
|
||||
Write-Host ""
|
||||
exit 1
|
||||
}
|
||||
363
01-essentials/05-cherry-pick/README.md
Normal file
363
01-essentials/05-cherry-pick/README.md
Normal file
@@ -0,0 +1,363 @@
|
||||
# Module 05: Cherry-Pick
|
||||
|
||||
## Learning Objectives
|
||||
|
||||
By the end of this module, you will:
|
||||
- Understand what cherry-picking is and how it works
|
||||
- Know when to use cherry-pick vs merge or rebase
|
||||
- Apply specific commits from one branch to another
|
||||
- Understand common use cases for cherry-picking
|
||||
- Learn how cherry-pick creates new commits with different hashes
|
||||
|
||||
## Setup
|
||||
|
||||
Create the challenge environment:
|
||||
|
||||
```pwsh
|
||||
.\setup.ps1
|
||||
```
|
||||
|
||||
This creates a repository with a `development` branch containing both bug fixes and experimental features.
|
||||
|
||||
## Overview
|
||||
|
||||
**Cherry-pick** allows you to copy specific commits from one branch to another. Unlike merging (which brings ALL commits), cherry-pick lets you be surgical about exactly which changes you want to apply.
|
||||
|
||||
Think of it like picking cherries from a tree - you select only the ripe ones you want, leaving the rest behind.
|
||||
|
||||
### Why Use Cherry-Pick?
|
||||
|
||||
- **Selective deployment** - Apply critical bug fixes without merging unfinished features
|
||||
- **Hotfixes** - Quickly move a fix from development to production
|
||||
- **Backporting** - Apply fixes to older release branches
|
||||
- **Wrong branch** - Move commits you accidentally made on the wrong branch
|
||||
- **Duplicate commits** - Apply the same fix across multiple branches
|
||||
|
||||
## Your Task
|
||||
|
||||
### The Scenario
|
||||
|
||||
You're working on a project where:
|
||||
- The `main` branch is stable and in production
|
||||
- The `development` branch has new features being tested
|
||||
- Development has critical bug fixes that need to go to production NOW
|
||||
- But development also has experimental features that aren't ready yet
|
||||
|
||||
You need to cherry-pick ONLY the bug fixes to main, leaving the experimental features behind.
|
||||
|
||||
### Part 1: Explore the Development Branch
|
||||
|
||||
First, see what commits are on the development branch:
|
||||
|
||||
```pwsh
|
||||
cd challenge
|
||||
|
||||
# View all commits on development branch
|
||||
git log --oneline development
|
||||
|
||||
# View the full commit graph
|
||||
git log --oneline --graph --all
|
||||
```
|
||||
|
||||
**Study the commits:**
|
||||
- Look for commits with "Fix" in the message (these are bug fixes)
|
||||
- Look for commits with "experimental" or "beta" (these should stay on development)
|
||||
- Note the commit hashes (the 7-character codes like `abc1234`)
|
||||
|
||||
**Inspect specific commits:**
|
||||
```pwsh
|
||||
# See what files a commit changed
|
||||
git show <commit-hash>
|
||||
|
||||
# Example:
|
||||
# git show abc1234
|
||||
```
|
||||
|
||||
You should see:
|
||||
- 2 commits that fix bugs (security and performance)
|
||||
- 2 commits that add experimental features
|
||||
|
||||
### Part 2: Switch to Main Branch
|
||||
|
||||
Before cherry-picking, you need to be on the target branch (main):
|
||||
|
||||
```pwsh
|
||||
# Switch to main branch
|
||||
git switch main
|
||||
|
||||
# Verify you're on main
|
||||
git branch
|
||||
```
|
||||
|
||||
The `*` should be next to `main`.
|
||||
|
||||
**Check what's currently on main:**
|
||||
```pwsh
|
||||
# See main's commits
|
||||
git log --oneline
|
||||
|
||||
# See what files exist
|
||||
ls
|
||||
```
|
||||
|
||||
Main should only have the initial app and README - no bug fixes yet, no experimental features.
|
||||
|
||||
### Part 3: Cherry-Pick the Bug Fixes
|
||||
|
||||
Now copy the bug fix commits from development to main:
|
||||
|
||||
1. Find the security fix commit hash by looking at your earlier `git log --oneline --graph --all`
|
||||
- Look for a commit message like "Fix security vulnerability in input validation"
|
||||
- Note its hash (first 7 characters)
|
||||
|
||||
2. Cherry-pick the security fix:
|
||||
```pwsh
|
||||
git cherry-pick <security-fix-hash>
|
||||
|
||||
# Example if the hash is abc1234:
|
||||
# git cherry-pick abc1234
|
||||
```
|
||||
3. Verify it worked: Check that security.py, with `ls` or check your file explorer in VSCode, now exists and check that the commit has been added to the main branch with `git log --oneline --graph --all`
|
||||
4. Find the performance fix commit hash
|
||||
- Look for "Fix performance issue with data caching"
|
||||
- Note its hash
|
||||
|
||||
5. Cherry-pick the performance fix:
|
||||
```pwsh
|
||||
git cherry-pick <performance-fix-hash>
|
||||
```
|
||||
|
||||
6. Verify both fixes are now on main:
|
||||
```pwsh
|
||||
# You should see both security.py and cache.py
|
||||
ls
|
||||
|
||||
# View the graph showing both branches
|
||||
git log --oneline --graph --all
|
||||
```
|
||||
|
||||
### Part 4: Verify Your Solution
|
||||
|
||||
Check that you completed the challenge correctly:
|
||||
|
||||
```pwsh
|
||||
# From inside the module directory
|
||||
.\verify.ps1
|
||||
```
|
||||
|
||||
The verification checks:
|
||||
- ✅ You're on the main branch
|
||||
- ✅ Security fix is applied to main
|
||||
- ✅ Performance fix is applied to main
|
||||
- ✅ Experimental features are NOT on main
|
||||
- ✅ Development branch still has all commits
|
||||
|
||||
## Understanding Cherry-Pick
|
||||
|
||||
### What Actually Happens?
|
||||
|
||||
When you cherry-pick a commit, Git:
|
||||
1. Looks at what changed in that specific commit
|
||||
2. Applies those same changes to your current branch
|
||||
3. Creates a NEW commit with those changes
|
||||
|
||||
```
|
||||
Before cherry-pick:
|
||||
|
||||
development: A---B---C---D
|
||||
/
|
||||
main: E---F
|
||||
|
||||
After: git switch main && git cherry-pick C
|
||||
|
||||
development: A---B---C---D
|
||||
/
|
||||
main: E---F---C'
|
||||
```
|
||||
|
||||
Notice:
|
||||
- `C'` is a NEW commit (different hash than original `C`)
|
||||
- Original `C` still exists on development
|
||||
- Main now has the changes from C, but not B or D
|
||||
|
||||
### Cherry-Pick vs Merge
|
||||
|
||||
**Merge brings everything:**
|
||||
```pwsh
|
||||
git switch main
|
||||
git merge development
|
||||
# Result: A, B, C, and D all come to main
|
||||
```
|
||||
|
||||
**Cherry-pick is selective:**
|
||||
```pwsh
|
||||
git switch main
|
||||
git cherry-pick C
|
||||
# Result: Only C comes to main (as C')
|
||||
```
|
||||
|
||||
### Important: New Commits, New Hashes
|
||||
|
||||
Cherry-picked commits are COPIES, not moves:
|
||||
- Original commit stays on source branch
|
||||
- New commit created on target branch
|
||||
- Different commit hash (because different parent)
|
||||
- Same changes, same message, different identity
|
||||
|
||||
## Key Commands
|
||||
|
||||
### Viewing Commits
|
||||
|
||||
```pwsh
|
||||
# See commits on another branch
|
||||
git log branch-name --oneline
|
||||
|
||||
# See what a specific commit changed
|
||||
git show <commit-hash>
|
||||
|
||||
# See commit graph
|
||||
git log --oneline --graph --all
|
||||
|
||||
# See only commit message (not changes)
|
||||
git log --oneline
|
||||
```
|
||||
|
||||
### Cherry-Picking
|
||||
|
||||
```pwsh
|
||||
# Cherry-pick a single commit
|
||||
git cherry-pick <commit-hash>
|
||||
|
||||
# Cherry-pick multiple commits (in order)
|
||||
git cherry-pick <hash1> <hash2> <hash3>
|
||||
|
||||
# Cherry-pick a range of commits
|
||||
git cherry-pick <start-hash>..<end-hash>
|
||||
|
||||
# Abort a cherry-pick if something goes wrong
|
||||
git cherry-pick --abort
|
||||
```
|
||||
|
||||
### After Cherry-Pick
|
||||
|
||||
```pwsh
|
||||
# Verify the commit was added
|
||||
git log --oneline
|
||||
|
||||
# See what files changed
|
||||
git show HEAD
|
||||
|
||||
# Compare branches
|
||||
git log main..development --oneline
|
||||
```
|
||||
|
||||
## Common Workflows
|
||||
|
||||
### Hotfix to Production
|
||||
|
||||
Critical bug found in production:
|
||||
|
||||
```pwsh
|
||||
# You're on feature-new-ui branch
|
||||
# You just committed a critical security fix
|
||||
|
||||
git log --oneline
|
||||
# Note the hash of your fix commit
|
||||
|
||||
# Switch to production branch
|
||||
git switch production
|
||||
|
||||
# Apply just that fix
|
||||
git cherry-pick <fix-commit-hash>
|
||||
|
||||
# Deploy to production
|
||||
# Your fix is live, but new UI stays in development
|
||||
```
|
||||
|
||||
### Backporting to Old Versions
|
||||
|
||||
```pwsh
|
||||
# You fixed a bug on main
|
||||
git switch main
|
||||
git log --oneline
|
||||
# Note the fix commit hash
|
||||
|
||||
# Apply to older release branch
|
||||
git switch release-2.5
|
||||
git cherry-pick <fix-commit-hash>
|
||||
|
||||
# Apply to even older release
|
||||
git switch release-2.0
|
||||
git cherry-pick <fix-commit-hash>
|
||||
|
||||
# Same fix now on three branches!
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### "I can't remember the commit hash!"
|
||||
|
||||
```pwsh
|
||||
# See commits on the source branch
|
||||
git log development --oneline
|
||||
|
||||
# Search for specific text in commit messages
|
||||
git log development --oneline --grep="security"
|
||||
|
||||
# See recent commits with more detail
|
||||
git log development --oneline -n 10
|
||||
```
|
||||
|
||||
### "I cherry-picked in the wrong order!"
|
||||
|
||||
Order matters! If commit B depends on commit A, cherry-pick A first:
|
||||
|
||||
```pwsh
|
||||
# Wrong order might cause issues
|
||||
git cherry-pick B # Might fail if it needs changes from A
|
||||
|
||||
# Correct order
|
||||
git cherry-pick A
|
||||
git cherry-pick B
|
||||
```
|
||||
|
||||
### "How do I see what will change before cherry-picking?"
|
||||
|
||||
```pwsh
|
||||
# See what changes are in a commit
|
||||
git show <commit-hash>
|
||||
|
||||
# Compare your current branch with a commit
|
||||
git diff HEAD <commit-hash>
|
||||
```
|
||||
|
||||
## Tips for Success
|
||||
|
||||
💡 **Copy the commit hashes** - Write them down before switching branches
|
||||
💡 **Cherry-pick oldest first** - Apply commits in chronological order
|
||||
💡 **Check your branch** - Always verify you're on the target branch first with `git branch`
|
||||
💡 **Verify after each pick** - Run `git log --oneline` to confirm it worked
|
||||
💡 **Use the graph** - `git log --oneline --graph --all` shows the full picture
|
||||
💡 **Original stays put** - Cherry-pick copies, doesn't move commits
|
||||
|
||||
## What You've Learned
|
||||
|
||||
After completing this module, you understand:
|
||||
|
||||
- ✅ Cherry-pick copies specific commits between branches
|
||||
- ✅ `git cherry-pick <hash>` applies a commit to current branch
|
||||
- ✅ Cherry-picked commits get new hashes but same changes
|
||||
- ✅ Use cherry-pick for selective deployment of changes
|
||||
- ✅ Cherry-pick is different from merge (selective vs all)
|
||||
- ✅ Original commit stays on source branch
|
||||
|
||||
## Next Steps
|
||||
|
||||
Ready to continue? Cherry-pick is a powerful tool for selective change management. Next modules will cover more advanced Git operations.
|
||||
|
||||
To start over:
|
||||
```pwsh
|
||||
.\reset.ps1
|
||||
```
|
||||
|
||||
**Need help?** Run `git status` to see what Git suggests, or `git log --oneline --graph --all` to see the full picture!
|
||||
@@ -26,7 +26,8 @@ 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 branch
|
||||
# Detect the default branch name (could be main, master, etc.)
|
||||
# First commit creates the branch, so we detect it after the first commit below
|
||||
$app = @"
|
||||
class App:
|
||||
def __init__(self):
|
||||
@@ -40,6 +41,14 @@ Set-Content -Path "app.py" -Value $app
|
||||
git add app.py
|
||||
git commit -m "Initial app implementation" | Out-Null
|
||||
|
||||
# Detect the main branch name after first commit
|
||||
$mainBranch = git branch --show-current
|
||||
if (-not $mainBranch) {
|
||||
$mainBranch = git config --get init.defaultBranch
|
||||
if (-not $mainBranch) { $mainBranch = "main" }
|
||||
}
|
||||
Write-Host "Default branch detected: $mainBranch" -ForegroundColor Yellow
|
||||
|
||||
$readme = @"
|
||||
# Application
|
||||
|
||||
@@ -119,40 +128,29 @@ git add app.py
|
||||
git commit -m "Add beta features framework" | Out-Null
|
||||
|
||||
# Commit 4: Performance bug fix (SHOULD be cherry-picked)
|
||||
$appWithPerformance = @"
|
||||
class App:
|
||||
# This commit adds caching to the existing file to fix performance
|
||||
# It should apply cleanly to main since it doesn't depend on experimental features
|
||||
$performanceCode = @"
|
||||
|
||||
class DataCache:
|
||||
def __init__(self):
|
||||
self.version = '1.0.0'
|
||||
self.experimental_mode = False
|
||||
self.beta_features = []
|
||||
self.cache = {}
|
||||
|
||||
def start(self):
|
||||
print('App started')
|
||||
if self.experimental_mode:
|
||||
self.enable_experimental_features()
|
||||
|
||||
def enable_experimental_features(self):
|
||||
print('Experimental features enabled')
|
||||
|
||||
def add_beta_feature(self, feature):
|
||||
self.beta_features.append(feature)
|
||||
|
||||
def get_data(self, key):
|
||||
def get(self, key):
|
||||
# Use cache to improve performance
|
||||
if key in self.cache:
|
||||
return self.cache[key]
|
||||
data = self.fetch_data(key)
|
||||
self.cache[key] = data
|
||||
return data
|
||||
return None
|
||||
|
||||
def fetch_data(self, key):
|
||||
# Simulate data fetching
|
||||
return {'key': key, 'value': 'data'}
|
||||
def set(self, key, value):
|
||||
self.cache[key] = value
|
||||
|
||||
def clear(self):
|
||||
self.cache.clear()
|
||||
"@
|
||||
|
||||
Set-Content -Path "app.py" -Value $appWithPerformance
|
||||
git add app.py
|
||||
Set-Content -Path "cache.py" -Value $performanceCode
|
||||
git add cache.py
|
||||
git commit -m "Fix performance issue with data caching" | Out-Null
|
||||
|
||||
# Return to module directory
|
||||
@@ -164,12 +162,13 @@ Write-Host "========================================" -ForegroundColor Green
|
||||
Write-Host "`nYou are on the 'development' branch with multiple commits:" -ForegroundColor Cyan
|
||||
Write-Host "- Experimental features (not ready for production)" -ForegroundColor Yellow
|
||||
Write-Host "- Critical bug fixes (needed in production NOW)" -ForegroundColor Green
|
||||
Write-Host "`nDetected main branch: $mainBranch" -ForegroundColor Cyan
|
||||
Write-Host "`nYour task:" -ForegroundColor Yellow
|
||||
Write-Host "1. Navigate to the challenge directory: cd challenge" -ForegroundColor White
|
||||
Write-Host "2. View the development branch commits: git log --oneline" -ForegroundColor White
|
||||
Write-Host "3. Identify which commits are bug fixes (look for 'Fix' in messages)" -ForegroundColor White
|
||||
Write-Host "4. Switch to main branch: git checkout main" -ForegroundColor White
|
||||
Write-Host "5. Cherry-pick ONLY the bug fix commits to main" -ForegroundColor White
|
||||
Write-Host "6. Do NOT bring the experimental features to main" -ForegroundColor White
|
||||
Write-Host "4. Switch to $mainBranch branch: git checkout $mainBranch" -ForegroundColor White
|
||||
Write-Host "5. Cherry-pick ONLY the bug fix commits to $mainBranch" -ForegroundColor White
|
||||
Write-Host "6. Do NOT bring the experimental features to $mainBranch" -ForegroundColor White
|
||||
Write-Host "`nHint: Look for commits mentioning 'security' and 'performance' fixes" -ForegroundColor Cyan
|
||||
Write-Host "Run '../verify.ps1' from the challenge directory to check your solution.`n" -ForegroundColor Cyan
|
||||
@@ -32,12 +32,27 @@ if (-not (Test-Path ".git")) {
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Detect the main branch name
|
||||
$allBranches = git branch --list 2>$null | ForEach-Object { $_.Trim('* ') }
|
||||
if ($allBranches -contains "main") {
|
||||
$mainBranch = "main"
|
||||
} elseif ($allBranches -contains "master") {
|
||||
$mainBranch = "master"
|
||||
} else {
|
||||
$mainBranch = git config --get init.defaultBranch
|
||||
if (-not $mainBranch) {
|
||||
$mainBranch = $allBranches | Select-Object -First 1
|
||||
if (-not $mainBranch) { $mainBranch = "main" }
|
||||
}
|
||||
}
|
||||
Write-Host "Detected main branch: $mainBranch" -ForegroundColor Cyan
|
||||
|
||||
# Check current branch
|
||||
$currentBranch = git branch --show-current 2>$null
|
||||
if ($currentBranch -ne "main") {
|
||||
Write-Host "[FAIL] You should be on the 'main' branch." -ForegroundColor Red
|
||||
if ($currentBranch -ne $mainBranch) {
|
||||
Write-Host "[FAIL] You should be on the '$mainBranch' branch." -ForegroundColor Red
|
||||
Write-Host "Current branch: $currentBranch" -ForegroundColor Yellow
|
||||
Write-Host "Hint: Use 'git checkout main' to switch to main branch" -ForegroundColor Yellow
|
||||
Write-Host "Hint: Use 'git checkout $mainBranch' to switch to $mainBranch branch" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
@@ -54,15 +69,15 @@ if (Test-Path ".git/CHERRY_PICK_HEAD") {
|
||||
}
|
||||
|
||||
# Check commit count on main (should be 4: 2 initial + 2 cherry-picked)
|
||||
$mainCommitCount = (git rev-list --count main 2>$null)
|
||||
$mainCommitCount = (git rev-list --count $mainBranch 2>$null)
|
||||
if ($mainCommitCount -ne 4) {
|
||||
Write-Host "[FAIL] Expected 4 commits on main branch, found $mainCommitCount" -ForegroundColor Red
|
||||
Write-Host "[FAIL] Expected 4 commits on $mainBranch branch, found $mainCommitCount" -ForegroundColor Red
|
||||
if ($mainCommitCount -lt 4) {
|
||||
Write-Host "Hint: You should cherry-pick 2 bug fix commits to main" -ForegroundColor Yellow
|
||||
Write-Host "Hint: You should cherry-pick 2 bug fix commits to $mainBranch" -ForegroundColor Yellow
|
||||
} else {
|
||||
Write-Host "Hint: You should cherry-pick ONLY the 2 bug fix commits, not all commits" -ForegroundColor Yellow
|
||||
}
|
||||
Write-Host "`nExpected commits on main:" -ForegroundColor Yellow
|
||||
Write-Host "`nExpected commits on ${mainBranch}:" -ForegroundColor Yellow
|
||||
Write-Host " 1. Initial app implementation" -ForegroundColor White
|
||||
Write-Host " 2. Add README" -ForegroundColor White
|
||||
Write-Host " 3. Fix security vulnerability in input validation (cherry-picked)" -ForegroundColor White
|
||||
@@ -72,9 +87,9 @@ if ($mainCommitCount -ne 4) {
|
||||
}
|
||||
|
||||
# Check for merge commits (should be none - cherry-pick doesn't create merge commits)
|
||||
$mergeCommits = git log --merges --oneline main 2>$null
|
||||
$mergeCommits = git log --merges --oneline $mainBranch 2>$null
|
||||
if ($mergeCommits) {
|
||||
Write-Host "[FAIL] Found merge commits on main. You should use cherry-pick, not merge." -ForegroundColor Red
|
||||
Write-Host "[FAIL] Found merge commits on $mainBranch. You should use cherry-pick, not merge." -ForegroundColor Red
|
||||
Write-Host "Hint: Use 'git cherry-pick <commit-hash>' instead of 'git merge'" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
@@ -82,7 +97,7 @@ if ($mergeCommits) {
|
||||
|
||||
# Check that security.py exists (from the security fix commit)
|
||||
if (-not (Test-Path "security.py")) {
|
||||
Write-Host "[FAIL] security.py not found on main branch." -ForegroundColor Red
|
||||
Write-Host "[FAIL] security.py not found on $mainBranch branch." -ForegroundColor Red
|
||||
Write-Host "Hint: You need to cherry-pick the 'Fix security vulnerability' commit" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
@@ -110,23 +125,32 @@ if (-not (Test-Path "app.py")) {
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check that app.py has the performance fix (cache) but NOT experimental features
|
||||
$appContent = Get-Content "app.py" -Raw
|
||||
|
||||
# Should have cache (from performance fix)
|
||||
if ($appContent -notmatch "cache") {
|
||||
Write-Host "[FAIL] app.py is missing the performance fix (cache)." -ForegroundColor Red
|
||||
# Check that cache.py exists (from performance fix)
|
||||
if (-not (Test-Path "cache.py")) {
|
||||
Write-Host "[FAIL] cache.py not found on $mainBranch branch." -ForegroundColor Red
|
||||
Write-Host "Hint: You need to cherry-pick the 'Fix performance issue' commit" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
if ($appContent -notmatch "get_data") {
|
||||
Write-Host "[FAIL] app.py is missing the get_data method from performance fix." -ForegroundColor Red
|
||||
# Check that cache.py has the DataCache class
|
||||
$cacheContent = Get-Content "cache.py" -Raw
|
||||
|
||||
if ($cacheContent -notmatch "DataCache") {
|
||||
Write-Host "[FAIL] cache.py is missing the DataCache class." -ForegroundColor Red
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
if ($cacheContent -notmatch "def get\(") {
|
||||
Write-Host "[FAIL] cache.py is missing the get method." -ForegroundColor Red
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check that app.py does NOT have experimental features
|
||||
$appContent = Get-Content "app.py" -Raw
|
||||
|
||||
# Should NOT have experimental features
|
||||
if ($appContent -match "experimental_mode") {
|
||||
Write-Host "[FAIL] app.py contains experimental features (experimental_mode)." -ForegroundColor Red
|
||||
@@ -151,7 +175,7 @@ if ($appContent -match "enable_experimental_features") {
|
||||
}
|
||||
|
||||
# Check commit messages to verify cherry-picks
|
||||
$commits = git log --pretty=format:"%s" main 2>$null
|
||||
$commits = git log --pretty=format:"%s" $mainBranch 2>$null
|
||||
$commitArray = $commits -split "`n"
|
||||
|
||||
$hasSecurityFix = $false
|
||||
@@ -167,14 +191,14 @@ foreach ($commit in $commitArray) {
|
||||
}
|
||||
|
||||
if (-not $hasSecurityFix) {
|
||||
Write-Host "[FAIL] Security fix commit not found on main branch." -ForegroundColor Red
|
||||
Write-Host "[FAIL] Security fix commit not found on $mainBranch branch." -ForegroundColor Red
|
||||
Write-Host "Hint: Cherry-pick the 'Fix security vulnerability' commit from development" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
if (-not $hasPerformanceFix) {
|
||||
Write-Host "[FAIL] Performance fix commit not found on main branch." -ForegroundColor Red
|
||||
Write-Host "[FAIL] Performance fix commit not found on $mainBranch branch." -ForegroundColor Red
|
||||
Write-Host "Hint: Cherry-pick the 'Fix performance issue' commit from development" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
@@ -194,8 +218,8 @@ 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 "- Cherry-picked the security vulnerability fix to main" -ForegroundColor White
|
||||
Write-Host "- Cherry-picked the performance issue fix to main" -ForegroundColor White
|
||||
Write-Host "- Cherry-picked the security vulnerability fix to $mainBranch" -ForegroundColor White
|
||||
Write-Host "- Cherry-picked the performance issue fix to $mainBranch" -ForegroundColor White
|
||||
Write-Host "- Left experimental features on development branch only" -ForegroundColor White
|
||||
Write-Host "- Kept development branch intact with all commits" -ForegroundColor White
|
||||
Write-Host "`nPerfect use of cherry-pick!" -ForegroundColor Green
|
||||
400
01-essentials/06-revert/README.md
Normal file
400
01-essentials/06-revert/README.md
Normal file
@@ -0,0 +1,400 @@
|
||||
# Module 06: Git Revert - Safe Undoing
|
||||
|
||||
## About This Module
|
||||
|
||||
Welcome to Module 06, 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 commits safely while preserving surrounding changes
|
||||
2. Understand how revert creates new commits instead of erasing history
|
||||
3. Revert multiple commits at once
|
||||
4. 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 (Module 03)
|
||||
|
||||
## Setup
|
||||
|
||||
Run the setup script to create the challenge environment:
|
||||
|
||||
```pwsh
|
||||
.\setup.ps1
|
||||
```
|
||||
|
||||
This creates a `challenge/` directory with two branches demonstrating different revert scenarios:
|
||||
- `regular-revert` - Basic 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:**
|
||||
```pwsh
|
||||
cd challenge
|
||||
```
|
||||
|
||||
2. **Check which branch you're on** (you should be on `regular-revert`):
|
||||
```pwsh
|
||||
git branch
|
||||
```
|
||||
The `*` should be next to `regular-revert`.
|
||||
|
||||
3. **View the commit history:**
|
||||
```pwsh
|
||||
git log --oneline
|
||||
```
|
||||
|
||||
4. **Find the commit with message:** "Add broken divide function - needs to be reverted!"
|
||||
- Note the commit hash (the 7-character code at the start, like `a1b2c3d`)
|
||||
- Write it down or copy it
|
||||
|
||||
5. **Revert that specific commit** (replace `<commit-hash>` with the actual hash):
|
||||
```pwsh
|
||||
git revert <commit-hash>
|
||||
```
|
||||
|
||||
6. **Visual Studio Code will open** with the revert commit message:
|
||||
- The default message is fine (it says "Revert 'Add broken divide function...'")
|
||||
- Close the editor window to accept the commit message
|
||||
- Git will create the revert commit
|
||||
|
||||
### What to Observe
|
||||
|
||||
After reverting, check your work:
|
||||
|
||||
```pwsh
|
||||
# View the new revert commit in history
|
||||
git log --oneline
|
||||
|
||||
# Check that divide.py file is gone (reverted)
|
||||
ls
|
||||
# You should see calculator.py but NOT divide.py
|
||||
|
||||
# Check that modulo function still exists in calculator.py (it came after the bad commit)
|
||||
cat calculator.py
|
||||
# You should see def modulo
|
||||
|
||||
# Check that multiply function still exists (it came before the bad commit)
|
||||
# (You already see it when you cat the file above)
|
||||
```
|
||||
|
||||
**Key insight:** Revert creates a NEW commit that undoes the changes from the target commit, but leaves all other commits intact.
|
||||
|
||||
### 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 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:**
|
||||
```pwsh
|
||||
git switch multi-revert
|
||||
```
|
||||
|
||||
2. **View the commit history:**
|
||||
```pwsh
|
||||
git log --oneline
|
||||
```
|
||||
|
||||
Find the two bad commits:
|
||||
- "Add broken square_root - REVERT THIS!"
|
||||
- "Add broken logarithm - REVERT THIS TOO!"
|
||||
|
||||
Note both commit hashes (write them down)
|
||||
|
||||
3. **Revert both commits in one command** (replace with actual hashes):
|
||||
```pwsh
|
||||
git revert <commit-hash-1> <commit-hash-2>
|
||||
```
|
||||
|
||||
**Important:** List commits from **oldest to newest** for cleanest history (square_root first, then logarithm).
|
||||
|
||||
**Alternatively**, revert them one at a time:
|
||||
```pwsh
|
||||
git revert <commit-hash-1>
|
||||
git revert <commit-hash-2>
|
||||
```
|
||||
|
||||
4. **Visual Studio Code will open TWICE** (once for each revert):
|
||||
- Close the editor each time to accept the default commit message
|
||||
- Git will create two revert commits
|
||||
|
||||
5. **Verify the result:**
|
||||
```pwsh
|
||||
# View files - sqrt.py and logarithm.py should be gone
|
||||
ls
|
||||
# You should see calculator.py but NOT sqrt.py or logarithm.py
|
||||
|
||||
# Check that good functions remain in calculator.py
|
||||
cat calculator.py
|
||||
# You should see def power and def absolute
|
||||
```
|
||||
|
||||
### Multi-Revert Strategies
|
||||
|
||||
**Reverting a range of commits:**
|
||||
|
||||
```pwsh
|
||||
# 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:**
|
||||
|
||||
```pwsh
|
||||
# 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
|
||||
|
||||
After completing both challenges, verify your solutions:
|
||||
|
||||
```pwsh
|
||||
cd .. # Return to module directory (if you're in challenge/)
|
||||
.\verify.ps1
|
||||
```
|
||||
|
||||
Or from inside the challenge directory:
|
||||
|
||||
```pwsh
|
||||
..\verify.ps1
|
||||
```
|
||||
|
||||
The script checks that:
|
||||
- ✅ Revert commits were created (not destructive deletion)
|
||||
- ✅ Bad code is removed
|
||||
- ✅ Good code before and after is preserved
|
||||
|
||||
## Command Reference
|
||||
|
||||
### Basic Revert
|
||||
|
||||
```pwsh
|
||||
# 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
|
||||
```
|
||||
|
||||
### Reverting Old Commits
|
||||
|
||||
```pwsh
|
||||
# Revert a specific commit from any point in history
|
||||
git revert <commit-hash>
|
||||
|
||||
# Revert a commit from 5 commits ago
|
||||
git revert HEAD~5
|
||||
|
||||
# View what a commit changed before reverting
|
||||
git show <commit-hash>
|
||||
```
|
||||
|
||||
### Multiple Commits
|
||||
|
||||
```pwsh
|
||||
# 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
|
||||
|
||||
```pwsh
|
||||
# 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 (advanced module)
|
||||
- ❌ **Just want to edit a commit** - Use `git commit --amend`
|
||||
- ❌ **Haven't pushed yet** - Reset is cleaner for local cleanup, but more dangerous, stick to revert if in doubt
|
||||
- ❌ **Need to combine commits** - Use interactive rebase IF nothing has been pushed to cloud
|
||||
- ❌ **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:
|
||||
|
||||
```pwsh
|
||||
# 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:
|
||||
```pwsh
|
||||
git add <resolved-files>
|
||||
```
|
||||
3. Continue the revert:
|
||||
```pwsh
|
||||
git revert --continue
|
||||
```
|
||||
|
||||
Or abort if you change your mind:
|
||||
```pwsh
|
||||
git revert --abort
|
||||
```
|
||||
|
||||
## Common Mistakes
|
||||
|
||||
### 1. Using Reset on Pushed Commits
|
||||
|
||||
```pwsh
|
||||
# ❌ NEVER do this with pushed commits. Or at least try your best to avoid it.
|
||||
git reset --hard HEAD~3
|
||||
|
||||
# ✅ Do this instead
|
||||
git revert HEAD~3..HEAD
|
||||
```
|
||||
|
||||
### 2. Reverting Commits in Wrong Order
|
||||
|
||||
When reverting multiple related commits, revert from newest to oldest:
|
||||
|
||||
```pwsh
|
||||
# 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:**
|
||||
```pwsh
|
||||
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
|
||||
24
01-essentials/06-revert/reset.ps1
Normal file
24
01-essentials/06-revert/reset.ps1
Normal 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 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"
|
||||
199
01-essentials/06-revert/setup.ps1
Normal file
199
01-essentials/06-revert/setup.ps1
Normal file
@@ -0,0 +1,199 @@
|
||||
#!/usr/bin/env pwsh
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Sets up the Module 06 challenge environment for learning git revert.
|
||||
|
||||
.DESCRIPTION
|
||||
This script creates a challenge directory with two branches demonstrating
|
||||
different revert scenarios:
|
||||
- regular-revert: Basic revert of a single bad commit
|
||||
- multi-revert: Reverting multiple commits at once
|
||||
#>
|
||||
|
||||
Write-Host "`n=== Setting up Module 06: 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"
|
||||
|
||||
# Detect the default branch name after first commit (created below)
|
||||
# Will be detected after the initial commit in SCENARIO 1
|
||||
|
||||
# ============================================================================
|
||||
# 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
|
||||
|
||||
# Detect the main branch name after first commit
|
||||
$mainBranch = git branch --show-current
|
||||
if (-not $mainBranch) {
|
||||
$mainBranch = git config --get init.defaultBranch
|
||||
if (-not $mainBranch) { $mainBranch = "main" }
|
||||
}
|
||||
Write-Host "Default branch detected: $mainBranch" -ForegroundColor Yellow
|
||||
|
||||
# Create regular-revert branch
|
||||
git switch -c regular-revert | Out-Null
|
||||
|
||||
# Good commit: Add multiply using append
|
||||
$multiplyFunc = @"
|
||||
|
||||
def multiply(a, b):
|
||||
"""Multiply two numbers."""
|
||||
return a * b
|
||||
"@
|
||||
Add-Content -Path "calculator.py" -Value $multiplyFunc
|
||||
git add .
|
||||
git commit -m "Add multiply function" | Out-Null
|
||||
|
||||
# BAD commit: Add broken divide function using separate file
|
||||
$divideContent = @"
|
||||
# divide.py - Division functionality
|
||||
|
||||
def divide(a, b):
|
||||
"""Divide a by b - BROKEN: doesn't handle division by zero!"""
|
||||
return a / b # This will crash if b is 0!
|
||||
"@
|
||||
Set-Content -Path "divide.py" -Value $divideContent
|
||||
git add .
|
||||
git commit -m "Add broken divide function - needs to be reverted!" | Out-Null
|
||||
|
||||
# Good commit: Add modulo (after bad commit) using append
|
||||
$moduloFunc = @"
|
||||
|
||||
def modulo(a, b):
|
||||
"""Return remainder of a divided by b."""
|
||||
return a % b
|
||||
"@
|
||||
Add-Content -Path "calculator.py" -Value $moduloFunc
|
||||
git add .
|
||||
git commit -m "Add modulo function" | Out-Null
|
||||
|
||||
Write-Host "[CREATED] regular-revert branch with bad divide commit" -ForegroundColor Green
|
||||
|
||||
# ============================================================================
|
||||
# SCENARIO 2: Multi Revert (Multiple Bad Commits)
|
||||
# ============================================================================
|
||||
Write-Host "`nScenario 2: Creating multi-revert branch..." -ForegroundColor Cyan
|
||||
|
||||
# Switch back to main
|
||||
git switch $mainBranch | 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 using append
|
||||
$powerFunc = @"
|
||||
|
||||
def power(a, b):
|
||||
"""Raise a to the power of b."""
|
||||
return a ** b
|
||||
"@
|
||||
Add-Content -Path "calculator.py" -Value $powerFunc
|
||||
git add .
|
||||
git commit -m "Add power function" | Out-Null
|
||||
|
||||
# BAD commit 1: Add broken square_root in separate file
|
||||
$sqrtContent = @"
|
||||
# sqrt.py - Square root functionality
|
||||
|
||||
def square_root(a):
|
||||
"""BROKEN: Returns wrong result for negative numbers!"""
|
||||
return a ** 0.5 # This returns NaN for negative numbers!
|
||||
"@
|
||||
Set-Content -Path "sqrt.py" -Value $sqrtContent
|
||||
git add .
|
||||
git commit -m "Add broken square_root - REVERT THIS!" | Out-Null
|
||||
|
||||
# BAD commit 2: Add broken logarithm in separate file
|
||||
$logContent = @"
|
||||
# logarithm.py - Logarithm functionality
|
||||
|
||||
def logarithm(a):
|
||||
"""BROKEN: Doesn't handle zero or negative numbers!"""
|
||||
import math
|
||||
return math.log(a) # This crashes for a <= 0!
|
||||
"@
|
||||
Set-Content -Path "logarithm.py" -Value $logContent
|
||||
git add .
|
||||
git commit -m "Add broken logarithm - REVERT THIS TOO!" | Out-Null
|
||||
|
||||
# Good commit: Add absolute value (after bad commits) using append
|
||||
$absoluteFunc = @"
|
||||
|
||||
def absolute(a):
|
||||
"""Return absolute value of a."""
|
||||
return abs(a)
|
||||
"@
|
||||
Add-Content -Path "calculator.py" -Value $absoluteFunc
|
||||
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 "`nTwo revert scenarios have been created:" -ForegroundColor Cyan
|
||||
Write-Host " 1. regular-revert - Revert a single bad commit" -ForegroundColor White
|
||||
Write-Host " 2. 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 ""
|
||||
173
01-essentials/06-revert/verify.ps1
Normal file
173
01-essentials/06-revert/verify.ps1
Normal file
@@ -0,0 +1,173 @@
|
||||
#!/usr/bin/env pwsh
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Verifies the Module 06 challenge solutions.
|
||||
|
||||
.DESCRIPTION
|
||||
Checks that both revert scenarios have been completed correctly:
|
||||
- regular-revert: Single commit reverted
|
||||
- multi-revert: Multiple commits reverted
|
||||
#>
|
||||
|
||||
Write-Host "`n=== Verifying Module 06: 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 divide.py is removed (was reverted)
|
||||
if (-not (Test-Path "divide.py")) {
|
||||
Write-Host "[PASS] Broken divide.py successfully reverted (file removed)" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "[FAIL] divide.py still exists (should be reverted)" -ForegroundColor Red
|
||||
Write-Host "[HINT] The bad commit should be reverted, removing divide.py" -ForegroundColor Yellow
|
||||
$allChecksPassed = $false
|
||||
}
|
||||
|
||||
# Check that calculator.py exists and has correct content
|
||||
if (Test-Path "calculator.py") {
|
||||
$calcContent = Get-Content "calculator.py" -Raw
|
||||
|
||||
# Check that 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: Multi Revert Verification
|
||||
# ============================================================================
|
||||
Write-Host "`n=== Scenario 2: 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 that sqrt.py is removed (reverted)
|
||||
if (-not (Test-Path "sqrt.py")) {
|
||||
Write-Host "[PASS] Broken sqrt.py successfully reverted (file removed)" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "[FAIL] sqrt.py still exists (should be reverted)" -ForegroundColor Red
|
||||
$allChecksPassed = $false
|
||||
}
|
||||
|
||||
# Check that logarithm.py is removed (reverted)
|
||||
if (-not (Test-Path "logarithm.py")) {
|
||||
Write-Host "[PASS] Broken logarithm.py successfully reverted (file removed)" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "[FAIL] logarithm.py still exists (should be reverted)" -ForegroundColor Red
|
||||
$allChecksPassed = $false
|
||||
}
|
||||
|
||||
# Check calculator.py content
|
||||
if (Test-Path "calculator.py") {
|
||||
$calcContent = Get-Content "calculator.py" -Raw
|
||||
|
||||
# Check that 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 commits safely without erasing history" -ForegroundColor White
|
||||
Write-Host " ✓ Reverting multiple commits at once" -ForegroundColor White
|
||||
Write-Host " ✓ Preserving all other commits while undoing specific changes" -ForegroundColor White
|
||||
Write-Host "`nReady for Module 07: 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
|
||||
}
|
||||
387
01-essentials/07-stash/README.md
Normal file
387
01-essentials/07-stash/README.md
Normal file
@@ -0,0 +1,387 @@
|
||||
# Module 07: Git Stash - Temporary Storage
|
||||
|
||||
## Learning Objectives
|
||||
|
||||
By the end of this module, you will:
|
||||
- Understand what git stash is and when to use it
|
||||
- Temporarily save work without committing
|
||||
- Switch between branches without losing uncommitted changes
|
||||
- Manage multiple stashes
|
||||
- Apply and remove stashed changes
|
||||
- Understand the difference between stash pop and stash apply
|
||||
|
||||
## Challenge Description
|
||||
|
||||
You're working on a feature when your teammate reports a critical bug in production. You need to switch to the main branch to fix it immediately, but your current work is incomplete and not ready to commit.
|
||||
|
||||
Your task is to:
|
||||
1. Stash your incomplete work
|
||||
2. Switch to the main branch
|
||||
3. Fix the urgent bug and commit it
|
||||
4. Return to your feature branch
|
||||
5. Restore your stashed changes
|
||||
6. Complete your feature and commit it
|
||||
|
||||
## Key Concepts
|
||||
|
||||
### What is Git Stash?
|
||||
|
||||
Git stash temporarily saves your uncommitted changes (both staged and unstaged) and reverts your working directory to match the HEAD commit. Think of it as a clipboard for your changes.
|
||||
|
||||
### When to Use Stash
|
||||
|
||||
Use stash when you need to:
|
||||
- Switch branches but have uncommitted changes
|
||||
- Pull updates from remote but have local modifications
|
||||
- Quickly test something on a clean working directory
|
||||
- Save work temporarily without creating a commit
|
||||
- Context-switch between tasks
|
||||
|
||||
### Stash vs Commit
|
||||
|
||||
**Stash:**
|
||||
- Temporary storage
|
||||
- Not part of project history
|
||||
- Can be applied to different branches
|
||||
- Easy to discard if not needed
|
||||
- Local only (not pushed to remote)
|
||||
|
||||
**Commit:**
|
||||
- Permanent part of history
|
||||
- Creates a snapshot in the project timeline
|
||||
- Associated with a specific branch
|
||||
- Should be meaningful and complete
|
||||
- Can be pushed to remote
|
||||
|
||||
### The Stash Stack
|
||||
|
||||
Git stash works like a stack (LIFO - Last In, First Out):
|
||||
```
|
||||
stash@{0} <- Most recent stash (top of stack)
|
||||
stash@{1}
|
||||
stash@{2} <- Oldest stash
|
||||
```
|
||||
|
||||
You can have multiple stashes and apply any of them.
|
||||
|
||||
## Setup
|
||||
|
||||
Run the setup script to create the challenge environment:
|
||||
|
||||
```pwsh
|
||||
.\setup.ps1
|
||||
```
|
||||
|
||||
This creates a `challenge/` directory where you're working on a login feature with uncommitted changes, and a critical bug needs fixing on main.
|
||||
|
||||
## Your Task
|
||||
|
||||
### The Scenario
|
||||
|
||||
You're working on a login feature on the `feature-login` branch. Your work is incomplete (has TODOs), so it's not ready to commit.
|
||||
|
||||
Suddenly, your teammate reports a **critical security bug** in production! You need to:
|
||||
1. Temporarily save your incomplete work
|
||||
2. Switch to the main branch
|
||||
3. Fix the urgent bug
|
||||
4. Return to your feature and continue working
|
||||
|
||||
### Step-by-Step Instructions
|
||||
|
||||
1. **Navigate to the challenge directory:**
|
||||
```pwsh
|
||||
cd challenge
|
||||
```
|
||||
|
||||
2. **Check your current status:**
|
||||
```pwsh
|
||||
git status
|
||||
```
|
||||
You should see modified `login.py` (uncommitted changes)
|
||||
|
||||
3. **Stash your work with a message:**
|
||||
```pwsh
|
||||
git stash save "WIP: login feature"
|
||||
```
|
||||
|
||||
4. **Verify working directory is clean:**
|
||||
```pwsh
|
||||
git status
|
||||
```
|
||||
Should say "nothing to commit, working tree clean"
|
||||
|
||||
5. **Switch to main branch:**
|
||||
```pwsh
|
||||
git switch main
|
||||
```
|
||||
|
||||
6. **Open app.py and find the bug:**
|
||||
```pwsh
|
||||
cat app.py
|
||||
```
|
||||
Look for the comment "# BUG: This allows unauthenticated access!"
|
||||
|
||||
7. **Fix the bug** by editing app.py:
|
||||
- Remove the buggy comment line
|
||||
- You can leave the implementation as-is or improve it
|
||||
- The important thing is removing the comment that says "allows unauthenticated access"
|
||||
|
||||
8. **Commit the fix:**
|
||||
```pwsh
|
||||
git add app.py
|
||||
git commit -m "Fix critical security bug"
|
||||
```
|
||||
|
||||
9. **Switch back to your feature branch:**
|
||||
```pwsh
|
||||
git switch feature-login
|
||||
```
|
||||
|
||||
10. **Restore your stashed work:**
|
||||
```pwsh
|
||||
git stash pop
|
||||
```
|
||||
This applies the stash and removes it from the stash stack
|
||||
|
||||
11. **Complete the TODOs in login.py:**
|
||||
- Open login.py in your editor
|
||||
- Complete the login method (verify password and return session)
|
||||
- Add a logout method
|
||||
- Remove all TODO comments
|
||||
|
||||
12. **Commit your completed feature:**
|
||||
```pwsh
|
||||
git add login.py
|
||||
git commit -m "Complete login feature"
|
||||
```
|
||||
|
||||
13. **Verify your solution:**
|
||||
```pwsh
|
||||
..\verify.ps1
|
||||
```
|
||||
|
||||
## Key Stash Commands
|
||||
|
||||
### Basic Operations
|
||||
|
||||
```pwsh
|
||||
# Stash current changes with a message
|
||||
git stash save "description"
|
||||
|
||||
# Stash without a message (not recommended)
|
||||
git stash
|
||||
|
||||
# Stash including untracked files
|
||||
git stash -u
|
||||
|
||||
# List all stashes
|
||||
git stash list
|
||||
|
||||
# Show what's in the most recent stash
|
||||
git stash show
|
||||
|
||||
# Show full diff of stash
|
||||
git stash show -p
|
||||
```
|
||||
|
||||
### Applying Stashes
|
||||
|
||||
```pwsh
|
||||
# Apply most recent stash and remove it (RECOMMENDED)
|
||||
git stash pop
|
||||
|
||||
# Apply most recent stash but keep it in stack
|
||||
git stash apply
|
||||
|
||||
# Apply a specific stash
|
||||
git stash apply stash@{1}
|
||||
|
||||
# Apply a specific stash by number
|
||||
git stash apply 1
|
||||
```
|
||||
|
||||
### Managing Stashes
|
||||
|
||||
```pwsh
|
||||
# Drop (delete) the most recent stash
|
||||
git stash drop
|
||||
|
||||
# Drop a specific stash
|
||||
git stash drop stash@{1}
|
||||
|
||||
# Clear all stashes
|
||||
git stash clear
|
||||
|
||||
# Create a new branch from a stash
|
||||
git stash branch new-branch-name
|
||||
```
|
||||
|
||||
## Understanding Stash vs Pop vs Apply
|
||||
|
||||
### Stash Pop (Recommended)
|
||||
```pwsh
|
||||
git stash pop
|
||||
```
|
||||
- Applies the stash to your working directory
|
||||
- **Removes** the stash from the stack
|
||||
- Use this most of the time
|
||||
|
||||
### Stash Apply (Keep Stash)
|
||||
```pwsh
|
||||
git stash apply
|
||||
```
|
||||
- Applies the stash to your working directory
|
||||
- **Keeps** the stash in the stack
|
||||
- Useful if you want to apply the same changes to multiple branches
|
||||
|
||||
### When to Use Which
|
||||
|
||||
**Use `pop` when:**
|
||||
- You're done with the stash and won't need it again (99% of the time)
|
||||
- You want to keep your stash list clean
|
||||
|
||||
**Use `apply` when:**
|
||||
- You want to test the same changes on different branches
|
||||
- You're not sure if you want to keep the stash yet
|
||||
|
||||
## Verification
|
||||
|
||||
Run the verification script to check your solution:
|
||||
|
||||
```pwsh
|
||||
..\verify.ps1
|
||||
```
|
||||
|
||||
Or from the module directory:
|
||||
|
||||
```pwsh
|
||||
.\verify.ps1
|
||||
```
|
||||
|
||||
The verification will check that:
|
||||
- ✅ The bug fix commit exists on main
|
||||
- ✅ Your feature is completed on the feature-login branch
|
||||
- ✅ All TODOs are removed from login.py
|
||||
- ✅ No uncommitted changes remain
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### "Cannot switch branches - you have uncommitted changes"
|
||||
|
||||
**Problem:** Git won't let you switch branches with uncommitted changes.
|
||||
|
||||
**Solution:**
|
||||
```pwsh
|
||||
# Stash your changes first
|
||||
git stash save "work in progress"
|
||||
|
||||
# Now you can switch
|
||||
git switch other-branch
|
||||
|
||||
# When you come back, restore your work
|
||||
git switch original-branch
|
||||
git stash pop
|
||||
```
|
||||
|
||||
### "I don't remember what's in my stash"
|
||||
|
||||
**Problem:** You stashed something but forgot what it was.
|
||||
|
||||
**Solution:**
|
||||
```pwsh
|
||||
# List all stashes
|
||||
git stash list
|
||||
|
||||
# Show summary of what changed
|
||||
git stash show stash@{0}
|
||||
|
||||
# Show full diff
|
||||
git stash show -p stash@{0}
|
||||
```
|
||||
|
||||
### "Stash conflicts when I apply"
|
||||
|
||||
**Problem:** Applying a stash causes merge conflicts.
|
||||
|
||||
**Solution:**
|
||||
1. Git marks conflicts in your files with `<<<<<<<` markers
|
||||
2. Open the files and resolve conflicts manually
|
||||
3. Stage the resolved files: `git add <file>`
|
||||
4. If you used `pop`, the stash is automatically dropped
|
||||
5. If you used `apply`, manually drop it: `git stash drop`
|
||||
|
||||
### "I accidentally cleared my stash"
|
||||
|
||||
**Problem:** You deleted a stash you still needed.
|
||||
|
||||
**Unfortunately:** Stashes are hard to recover once deleted. Lessons learned:
|
||||
- Use `git stash pop` instead of `git stash drop` when you're unsure
|
||||
- Use `git stash show -p` to preview before dropping
|
||||
- Consider committing work instead of stashing for important changes
|
||||
|
||||
## Tips for Success
|
||||
|
||||
💡 **Always add a message** - `git stash save "your message"` helps you remember what you stashed
|
||||
💡 **Use pop, not apply** - Pop removes the stash automatically, keeping your stash list clean
|
||||
💡 **Stash before pulling** - Avoid merge conflicts when pulling updates
|
||||
💡 **Preview before applying** - Use `git stash show -p` to see what's in a stash
|
||||
💡 **Stashes are local** - They don't get pushed to remote repositories
|
||||
💡 **Clean working directory** - Always verify with `git status` after stashing
|
||||
|
||||
## Common Use Cases
|
||||
|
||||
### Quick Branch Switch
|
||||
```pwsh
|
||||
# You're working on feature-A
|
||||
git stash save "feature A progress"
|
||||
git switch hotfix-branch
|
||||
# Fix the issue, commit
|
||||
git switch feature-A
|
||||
git stash pop
|
||||
```
|
||||
|
||||
### Pull with Local Changes
|
||||
```pwsh
|
||||
# You have uncommitted changes
|
||||
git stash save "local changes"
|
||||
git pull
|
||||
git stash pop
|
||||
# Resolve conflicts if any
|
||||
```
|
||||
|
||||
### Test Clean State
|
||||
```pwsh
|
||||
# Stash changes to test on clean code
|
||||
git stash save "testing clean state"
|
||||
# Run tests
|
||||
git stash pop # Restore your changes
|
||||
```
|
||||
|
||||
## What You've Learned
|
||||
|
||||
After completing this module, you understand:
|
||||
|
||||
- ✅ Stash temporarily saves uncommitted changes
|
||||
- ✅ Stash lets you switch contexts without committing
|
||||
- ✅ `git stash pop` applies and removes the stash
|
||||
- ✅ `git stash apply` applies but keeps the stash
|
||||
- ✅ Stashes are local and not pushed to remote
|
||||
- ✅ Stash is essential for handling interruptions and urgent fixes
|
||||
|
||||
**Key Takeaway:** Stash is your "temporary clipboard" for incomplete work. It helps you stay productive when you need to context-switch without making messy "WIP" commits.
|
||||
|
||||
## Next Steps
|
||||
|
||||
Ready to continue? You've now mastered the essential Git commands for daily development:
|
||||
- Committing and history
|
||||
- Branching and merging
|
||||
- Cherry-picking specific changes
|
||||
- Safely reverting commits
|
||||
- Temporarily stashing work
|
||||
|
||||
Move on to **Module 08: Multiplayer Git** to practice collaborating with others!
|
||||
|
||||
To start over:
|
||||
```pwsh
|
||||
.\reset.ps1
|
||||
```
|
||||
@@ -25,6 +25,9 @@ git init | Out-Null
|
||||
git config user.name "Workshop User" | Out-Null
|
||||
git config user.email "user@workshop.local" | Out-Null
|
||||
|
||||
# Detect the default branch name after first commit (created below)
|
||||
# Will be detected after the initial commit
|
||||
|
||||
# Create initial application on main
|
||||
$app = @"
|
||||
class Application:
|
||||
@@ -45,6 +48,14 @@ Set-Content -Path "app.py" -Value $app
|
||||
git add app.py
|
||||
git commit -m "Initial application" | Out-Null
|
||||
|
||||
# Detect the main branch name after first commit
|
||||
$mainBranch = git branch --show-current
|
||||
if (-not $mainBranch) {
|
||||
$mainBranch = git config --get init.defaultBranch
|
||||
if (-not $mainBranch) { $mainBranch = "main" }
|
||||
}
|
||||
Write-Host "Default branch detected: $mainBranch" -ForegroundColor Yellow
|
||||
|
||||
$readme = @"
|
||||
# MyApp
|
||||
|
||||
@@ -78,7 +89,7 @@ git add login.py
|
||||
git commit -m "Start login service implementation" | Out-Null
|
||||
|
||||
# Add a critical bug to main branch (simulating a bug that was introduced)
|
||||
git checkout main | Out-Null
|
||||
git checkout $mainBranch | Out-Null
|
||||
|
||||
$appWithBug = @"
|
||||
class Application:
|
||||
@@ -140,16 +151,17 @@ Write-Host "========================================" -ForegroundColor Green
|
||||
Write-Host "`nSituation:" -ForegroundColor Cyan
|
||||
Write-Host "You're working on the login feature (feature-login branch)" -ForegroundColor White
|
||||
Write-Host "You have uncommitted changes - the feature is NOT complete yet" -ForegroundColor Yellow
|
||||
Write-Host "`nUrgent: A critical security bug was found in production (main branch)!" -ForegroundColor Red
|
||||
Write-Host "`nUrgent: A critical security bug was found in production ($mainBranch branch)!" -ForegroundColor Red
|
||||
Write-Host "You need to fix it immediately, but your current work isn't ready to commit." -ForegroundColor Red
|
||||
Write-Host "`nDetected main branch: $mainBranch" -ForegroundColor Cyan
|
||||
Write-Host "`nYour task:" -ForegroundColor Yellow
|
||||
Write-Host "1. Navigate to the challenge directory: cd challenge" -ForegroundColor White
|
||||
Write-Host "2. Check your status: git status (see uncommitted changes)" -ForegroundColor White
|
||||
Write-Host "3. Stash your work: git stash save 'WIP: login feature'" -ForegroundColor White
|
||||
Write-Host "4. Switch to main: git checkout main" -ForegroundColor White
|
||||
Write-Host "4. Switch to ${mainBranch}: git switch $mainBranch" -ForegroundColor White
|
||||
Write-Host "5. Fix the security bug in app.py (remove the comment and fix the auth)" -ForegroundColor White
|
||||
Write-Host "6. Commit the fix: git add app.py && git commit -m 'Fix critical security bug'" -ForegroundColor White
|
||||
Write-Host "7. Switch back: git checkout feature-login" -ForegroundColor White
|
||||
Write-Host "7. Switch back: git switch feature-login" -ForegroundColor White
|
||||
Write-Host "8. Restore your work: git stash pop" -ForegroundColor White
|
||||
Write-Host "9. Complete the TODOs in login.py" -ForegroundColor White
|
||||
Write-Host "10. Commit your completed feature" -ForegroundColor White
|
||||
@@ -32,6 +32,21 @@ if (-not (Test-Path ".git")) {
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Detect the main branch name
|
||||
$allBranches = git branch --list 2>$null | ForEach-Object { $_.Trim('* ') }
|
||||
if ($allBranches -contains "main") {
|
||||
$mainBranch = "main"
|
||||
} elseif ($allBranches -contains "master") {
|
||||
$mainBranch = "master"
|
||||
} else {
|
||||
$mainBranch = git config --get init.defaultBranch
|
||||
if (-not $mainBranch) {
|
||||
$mainBranch = $allBranches | Select-Object -First 1
|
||||
if (-not $mainBranch) { $mainBranch = "main" }
|
||||
}
|
||||
}
|
||||
Write-Host "Detected main branch: $mainBranch" -ForegroundColor Cyan
|
||||
|
||||
# Check current branch
|
||||
$currentBranch = git branch --show-current 2>$null
|
||||
if ($currentBranch -ne "feature-login") {
|
||||
@@ -53,14 +68,22 @@ if ($status) {
|
||||
}
|
||||
|
||||
# Verify main branch has the security fix
|
||||
Write-Host "`nChecking main branch for bug fix..." -ForegroundColor Cyan
|
||||
git checkout main 2>$null | Out-Null
|
||||
Write-Host "`nChecking $mainBranch branch for bug fix..." -ForegroundColor Cyan
|
||||
git checkout $mainBranch 2>$null | Out-Null
|
||||
|
||||
# Check for bug fix commit
|
||||
$mainCommits = git log --pretty=format:"%s" main 2>$null
|
||||
if ($mainCommits -notmatch "security bug|Fix.*bug|security fix") {
|
||||
Write-Host "[FAIL] No security bug fix commit found on main branch." -ForegroundColor Red
|
||||
Write-Host "Hint: After stashing, switch to main and commit a bug fix" -ForegroundColor Yellow
|
||||
$mainCommits = git log --pretty=format:"%s" $mainBranch 2>$null
|
||||
$hasSecurityFix = $false
|
||||
foreach ($commit in $mainCommits) {
|
||||
if ($commit -match "security|Fix.*bug") {
|
||||
$hasSecurityFix = $true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if (-not $hasSecurityFix) {
|
||||
Write-Host "[FAIL] No security bug fix commit found on $mainBranch branch." -ForegroundColor Red
|
||||
Write-Host "Hint: After stashing, switch to $mainBranch and commit a bug fix" -ForegroundColor Yellow
|
||||
git checkout feature-login 2>$null | Out-Null
|
||||
Set-Location ..
|
||||
exit 1
|
||||
@@ -68,7 +91,7 @@ if ($mainCommits -notmatch "security bug|Fix.*bug|security fix") {
|
||||
|
||||
# Check that app.py has been fixed
|
||||
if (-not (Test-Path "app.py")) {
|
||||
Write-Host "[FAIL] app.py not found on main branch." -ForegroundColor Red
|
||||
Write-Host "[FAIL] app.py not found on $mainBranch branch." -ForegroundColor Red
|
||||
git checkout feature-login 2>$null | Out-Null
|
||||
Set-Location ..
|
||||
exit 1
|
||||
@@ -113,7 +136,7 @@ if (-not (Test-Path "login.py")) {
|
||||
$loginContent = Get-Content "login.py" -Raw
|
||||
|
||||
# Check that login method exists and is implemented
|
||||
if ($loginContent -notmatch "login\(username, password\)") {
|
||||
if ($loginContent -notmatch "def login") {
|
||||
Write-Host "[FAIL] login.py should have a login method." -ForegroundColor Red
|
||||
Set-Location ..
|
||||
exit 1
|
||||
237
01-essentials/08-multiplayer/01_FACILITATOR.md
Normal file
237
01-essentials/08-multiplayer/01_FACILITATOR.md
Normal file
@@ -0,0 +1,237 @@
|
||||
# Facilitator Setup Guide
|
||||
|
||||
This guide helps workshop facilitators set up the cloud-based multiplayer Git module using Azure DevOps.
|
||||
|
||||
## Overview
|
||||
|
||||
The Number Challenge is a collaborative Git exercise where students work together on a shared repository hosted on **Azure DevOps**.
|
||||
|
||||
**What participants will do:**
|
||||
- Clone a real repository from Azure DevOps
|
||||
- Collaborate to sort numbers 0-20 into the correct order
|
||||
- Experience push/pull workflow and merge conflicts
|
||||
- Learn to communicate and coordinate with teammates
|
||||
- Use SSH keys for secure authentication
|
||||
|
||||
---
|
||||
|
||||
## Prerequisites
|
||||
|
||||
### Azure DevOps Setup
|
||||
|
||||
You need:
|
||||
- **Azure DevOps Organization** - Free tier is sufficient
|
||||
- Sign up at [dev.azure.com](https://dev.azure.com)
|
||||
- **Project created** within your organization
|
||||
- **Admin access** to create repositories and manage users
|
||||
|
||||
### Workshop Materials
|
||||
|
||||
Participants need:
|
||||
- Git installed (version 2.23+)
|
||||
- VS Code (or any text editor)
|
||||
- SSH keys configured
|
||||
|
||||
---
|
||||
|
||||
## Pre-Workshop Setup
|
||||
|
||||
### Step 1: Add User Accounts
|
||||
|
||||
Add workshop participants to your Azure DevOps organization.
|
||||
|
||||
1. Navigate to **Organization Settings** → **Users**
|
||||
2. Click **Add users**
|
||||
3. Enter participant email addresses (Microsoft accounts)
|
||||
4. Select your workshop project
|
||||
5. Select **Access level**: Stakeholder (free) or Basic
|
||||
6. Click **Add**
|
||||
|
||||
### Step 2: Create the Repository
|
||||
|
||||
Create the shared repository: **number-challenge**
|
||||
|
||||
1. Sign in to Azure DevOps at [dev.azure.com](https://dev.azure.com)
|
||||
2. Navigate to your **Project**
|
||||
3. Click **Repos** in the left navigation
|
||||
4. Click the repo dropdown → **New repository**
|
||||
5. Fill in details:
|
||||
- **Name:** `number-challenge`
|
||||
- **Add a README:** Checked
|
||||
6. Click **Create**
|
||||
|
||||
### Step 3: Add the Starter File
|
||||
|
||||
Create `numbers.txt` with numbers 0-20 in random order.
|
||||
|
||||
**Option A: Via Azure DevOps web UI**
|
||||
|
||||
1. In your repository, click **+ New** → **File**
|
||||
2. Name it `numbers.txt`
|
||||
3. Add this content (numbers 0-20 shuffled):
|
||||
|
||||
```
|
||||
17
|
||||
3
|
||||
12
|
||||
8
|
||||
19
|
||||
1
|
||||
14
|
||||
6
|
||||
11
|
||||
0
|
||||
20
|
||||
9
|
||||
4
|
||||
16
|
||||
2
|
||||
18
|
||||
7
|
||||
13
|
||||
5
|
||||
15
|
||||
10
|
||||
```
|
||||
|
||||
4. Click **Commit**
|
||||
|
||||
**Option B: Via command line**
|
||||
|
||||
```powershell
|
||||
git clone git@ssh.dev.azure.com:v3/{organization}/{project}/number-challenge
|
||||
cd number-challenge
|
||||
|
||||
# Create numbers.txt with shuffled numbers
|
||||
@"
|
||||
17
|
||||
3
|
||||
12
|
||||
8
|
||||
19
|
||||
1
|
||||
14
|
||||
6
|
||||
11
|
||||
0
|
||||
20
|
||||
9
|
||||
4
|
||||
16
|
||||
2
|
||||
18
|
||||
7
|
||||
13
|
||||
5
|
||||
15
|
||||
10
|
||||
"@ | Out-File -FilePath numbers.txt -Encoding UTF8
|
||||
|
||||
git add numbers.txt
|
||||
git commit -m "feat: add shuffled numbers for challenge"
|
||||
git push
|
||||
```
|
||||
|
||||
### Step 4: Verify Student Access
|
||||
|
||||
Students added to the project automatically have access. Verify:
|
||||
|
||||
1. Go to **Project Settings** → **Repositories** → **number-challenge**
|
||||
2. Click **Security** tab
|
||||
3. Verify project team has **Contribute** permission
|
||||
|
||||
---
|
||||
|
||||
## During the Workshop
|
||||
|
||||
### Getting Started
|
||||
|
||||
1. Ensure all students have cloned the repository
|
||||
2. Have everyone open `numbers.txt` to see the shuffled numbers
|
||||
3. Explain the goal: sort numbers 0-20 into correct order
|
||||
|
||||
### The Exercise Flow
|
||||
|
||||
1. **Students pull** the latest changes
|
||||
2. **One person** moves a number to its correct position
|
||||
3. **They commit and push**
|
||||
4. **Others pull** and see the change
|
||||
5. **Repeat** until sorted
|
||||
|
||||
### Creating Conflicts (The Learning Moment)
|
||||
|
||||
Conflicts happen naturally when multiple people edit at once. You can encourage this:
|
||||
|
||||
- Have two students deliberately edit at the same time
|
||||
- Watch them experience the push rejection
|
||||
- Guide them through pulling and resolving the conflict
|
||||
|
||||
### Monitoring Progress
|
||||
|
||||
Check progress in Azure DevOps:
|
||||
|
||||
- **Repos → Commits**: See who's contributing
|
||||
- **Repos → Files → numbers.txt**: See current state
|
||||
|
||||
### Common Issues
|
||||
|
||||
**"I can't push!"**
|
||||
- Did they pull first? Run `git pull`
|
||||
- Is SSH set up? Check with `ssh -T git@ssh.dev.azure.com`
|
||||
|
||||
**"Merge conflict!"**
|
||||
- Walk them through removing conflict markers
|
||||
- Help them understand both sides of the conflict
|
||||
|
||||
**"Numbers are duplicated/missing!"**
|
||||
- Someone resolved a conflict incorrectly
|
||||
- Have the team review and fix together
|
||||
|
||||
---
|
||||
|
||||
## Success
|
||||
|
||||
When complete, `numbers.txt` should contain:
|
||||
|
||||
```
|
||||
0
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
6
|
||||
7
|
||||
8
|
||||
9
|
||||
10
|
||||
11
|
||||
12
|
||||
13
|
||||
14
|
||||
15
|
||||
16
|
||||
17
|
||||
18
|
||||
19
|
||||
20
|
||||
```
|
||||
|
||||
Celebrate the team's success!
|
||||
|
||||
---
|
||||
|
||||
## Post-Workshop Cleanup
|
||||
|
||||
To reuse the repository:
|
||||
|
||||
1. Reset `numbers.txt` to shuffled state
|
||||
2. Or delete and recreate the repository
|
||||
|
||||
---
|
||||
|
||||
## Tips
|
||||
|
||||
- **Keep groups small** (2 people per repository) for more interaction
|
||||
- **Encourage communication** - the exercise works best when people talk
|
||||
- **Let conflicts happen** - they're the best learning opportunity
|
||||
237
01-essentials/08-multiplayer/02_AZURE-DEVOPS-SSH-SETUP.md
Normal file
237
01-essentials/08-multiplayer/02_AZURE-DEVOPS-SSH-SETUP.md
Normal file
@@ -0,0 +1,237 @@
|
||||
# Azure DevOps SSH Setup - Best Practices Guide
|
||||
|
||||
This guide provides comprehensive instructions for setting up SSH authentication with Azure DevOps. SSH is the recommended authentication method for secure Git operations.
|
||||
|
||||
## Why SSH is Best Practice
|
||||
|
||||
SSH (Secure Shell) keys provide a secure way to authenticate with Azure DevOps without exposing passwords or tokens. Here's why SSH is the security best practice:
|
||||
|
||||
**Security Benefits:**
|
||||
- **No Password Exposure**: Your credentials never travel over the network
|
||||
- **Strong Encryption**: Uses RSA cryptographic algorithms
|
||||
- **No Credential Prompts**: Seamless authentication after initial setup
|
||||
- **Revocable**: Individual keys can be removed without changing passwords
|
||||
- **Auditable**: Track which key was used for each operation
|
||||
|
||||
---
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Before starting, ensure you have:
|
||||
|
||||
- **Git 2.23 or higher** installed
|
||||
```powershell
|
||||
git --version
|
||||
```
|
||||
|
||||
- **Azure DevOps account** with access to your organization/project
|
||||
- If you don't have one, create a free account at [dev.azure.com](https://dev.azure.com)
|
||||
|
||||
- **PowerShell 7+ or Bash terminal** for running commands
|
||||
```powershell
|
||||
pwsh --version
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Step 1: Generate SSH Key Pair
|
||||
|
||||
SSH authentication uses a key pair: a private key (stays on your computer) and a public key (uploaded to Azure DevOps).
|
||||
|
||||
### Generate RSA Key
|
||||
|
||||
Open your terminal and run:
|
||||
|
||||
```powershell
|
||||
ssh-keygen -t rsa
|
||||
```
|
||||
|
||||
**Note about RSA:** Azure DevOps currently only supports RSA SSH keys. While newer algorithms like Ed25519 offer better security and performance, they are not yet supported by Azure DevOps. See the note at the end of this guide for more information.
|
||||
|
||||
### Save Location
|
||||
|
||||
When prompted for the file location, press `Enter` to accept the default:
|
||||
|
||||
```
|
||||
Enter file in which to save the key (/Users/yourname/.ssh/id_rsa):
|
||||
```
|
||||
|
||||
**Default locations:**
|
||||
- **Windows**: `C:\Users\YourName\.ssh\id_rsa` and `C:\Users\YourName\.ssh\id_rsa.pub`
|
||||
|
||||
### Passphrase (Optional but Recommended)
|
||||
|
||||
You'll be prompted to enter a passphrase, just press `Enter` no password is needed (recommended but not needed):
|
||||
|
||||
```
|
||||
Enter passphrase (empty for no passphrase):
|
||||
Enter same passphrase again:
|
||||
```
|
||||
|
||||
### Verify Key Generation
|
||||
|
||||
Check that your keys were created:
|
||||
|
||||
**Windows PowerShell:**
|
||||
```powershell
|
||||
dir $HOME\.ssh\
|
||||
```
|
||||
|
||||
You should see two files:
|
||||
- `id_rsa` - Private key (NEVER share this)
|
||||
- `id_rsa.pub` - Public key (safe to share for upload to Azure DevOps)
|
||||
|
||||
---
|
||||
|
||||
## Step 2: Add SSH Public Key to Azure DevOps
|
||||
|
||||
Now you'll upload your public key to Azure DevOps.
|
||||
|
||||
### Navigate to SSH Public Keys Settings
|
||||
|
||||
1. Sign in to Azure DevOps at [https://dev.azure.com](https://dev.azure.com)
|
||||
2. Click your **profile icon** in the top-right corner
|
||||
3. Select **User settings** from the dropdown menu
|
||||
4. Click **SSH Public Keys**
|
||||
|
||||

|
||||
*Navigate to your user settings by clicking the profile icon in the top-right corner*
|
||||
|
||||
### Add New SSH Key
|
||||
|
||||
5. Click the **+ New Key** button
|
||||
|
||||

|
||||
*Click '+ New Key' to begin adding your SSH public key*
|
||||
|
||||
### Copy Your Public Key
|
||||
|
||||
Open your terminal and display your public key:
|
||||
|
||||
**Linux/Mac:**
|
||||
```bash
|
||||
cat ~/.ssh/id_rsa.pub
|
||||
```
|
||||
|
||||
**Windows PowerShell:**
|
||||
```powershell
|
||||
type $HOME\.ssh\id_rsa.pub
|
||||
```
|
||||
|
||||
**Windows Command Prompt:**
|
||||
```cmd
|
||||
type %USERPROFILE%\.ssh\id_rsa.pub
|
||||
```
|
||||
|
||||
The output will look like this:
|
||||
```
|
||||
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC2YbXnrSK5TTflZSwUv9KUedvI4p3JJ4dHgwp/SeJGqMNWnOMDbzQQzYT7E39w9Q8ItrdWsK4vRLGY2B1rQ+BpS6nn4KhTanMXLTaUFDlg6I1Yn5S3cTTe8dMAoa14j3CZfoSoRRgK8E+ktNb0o0nBMuZJlLkgEtPIz28fwU1vcHoSK7jFp5KL0pjf37RYZeHkbpI7hdCG2qHtdrC35gzdirYPJOekErF5VFRrLZaIRSSsX0V4XzwY2k1hxM037o/h6qcTLWfi5ugbyrdscL8BmhdGNH4Giwqd1k3MwSyiswRuAuclYv27oKnFVBRT+n649px4g3Vqa8dh014wM2HDjMGENIkHx0hcV9BWdfBfTSCJengmosGW+wQfmaNUo4WpAbwZD73ALNsoLg5Yl1tB6ZZ5mHwLRY3LG2BbQZMZRCELUyvbh8ZsRksNN/2zcS44RIQdObV8/4hcLse30+NQ7GRaMnJeAMRz4Rpzbb02y3w0wNQFp/evj1nN4WTz6l8= your@email.com
|
||||
```
|
||||
|
||||
**Copy the entire output** (from `ssh-rsa` to your email address).
|
||||
|
||||
|
||||
### Paste and Name Your Key
|
||||
|
||||

|
||||
|
||||
6. In the Azure DevOps dialog:
|
||||
- **Name**: Give your key a descriptive name (e.g., "Workshop Laptop 2026", "Home Desktop", "Work MacBook")
|
||||
- **Public Key Data**: Paste the entire public key you just copied
|
||||
7. Click **Save**
|
||||
|
||||
**Naming tip**: Use names that help you identify which machine uses each key. This makes it easier to revoke keys later if needed.
|
||||
|
||||
---
|
||||
|
||||
## Step 3: Using SSH with Git
|
||||
|
||||
Now that SSH is configured, you can use it for all Git operations.
|
||||
|
||||
### Clone a Repository with SSH
|
||||
|
||||
To clone a repository using SSH:
|
||||
|
||||
```bash
|
||||
git clone git@ssh.dev.azure.com:v3/{organization}/{project}/{repository}
|
||||
```
|
||||
|
||||
**Example** (replace placeholders with your actual values):
|
||||
```bash
|
||||
git clone git@ssh.dev.azure.com:v3/myorg/git-workshop/great-print-project
|
||||
```
|
||||
|
||||
**How to find your SSH URL:**
|
||||
1. Navigate to your repository in Azure DevOps
|
||||

|
||||
2. Click **Clone** in the top-right
|
||||
3. Select **SSH** from the dropdown
|
||||
4. Copy the SSH URL
|
||||
|
||||

|
||||
*Select SSH from the clone dialog to get your repository's SSH URL*
|
||||
|
||||
### Daily Git Operations
|
||||
|
||||
All standard Git commands now work seamlessly with SSH:
|
||||
|
||||
```bash
|
||||
# Pull latest changes
|
||||
git pull
|
||||
|
||||
# Push your commits
|
||||
git push
|
||||
|
||||
# Fetch from remote
|
||||
git fetch
|
||||
|
||||
# Push a new branch
|
||||
git push -u origin feature-branch
|
||||
```
|
||||
|
||||
**No more credential prompts!** SSH authentication happens automatically.
|
||||
|
||||
---
|
||||
|
||||
## Additional Resources
|
||||
- **Azure DevOps SSH Documentation**: [https://docs.microsoft.com/en-us/azure/devops/repos/git/use-ssh-keys-to-authenticate](https://docs.microsoft.com/en-us/azure/devops/repos/git/use-ssh-keys-to-authenticate)
|
||||
- **SSH Key Best Practices**: [https://security.stackexchange.com/questions/tagged/ssh-keys](https://security.stackexchange.com/questions/tagged/ssh-keys)
|
||||
- **Git with SSH**: [https://git-scm.com/book/en/v2/Git-on-the-Server-Generating-Your-SSH-Public-Key](https://git-scm.com/book/en/v2/Git-on-the-Server-Generating-Your-SSH-Public-Key)
|
||||
|
||||
---
|
||||
|
||||
## Quick Reference
|
||||
|
||||
### Common Commands
|
||||
|
||||
```bash
|
||||
# Generate RSA key
|
||||
ssh-keygen -t
|
||||
|
||||
# Display public key (Linux/Mac)
|
||||
cat ~/.ssh/id_rsa.pub
|
||||
|
||||
# Display public key (Windows)
|
||||
type $HOME\.ssh\id_rsa.pub
|
||||
|
||||
# Clone with SSH
|
||||
git clone git@ssh.dev.azure.com:v3/{org}/{project}/{repo}
|
||||
|
||||
# Check remote URL
|
||||
git remote -v
|
||||
```
|
||||
|
||||
### SSH URL Format
|
||||
|
||||
```
|
||||
git@ssh.dev.azure.com:v3/{organization}/{project}/{repository}
|
||||
```
|
||||
|
||||
**Example:**
|
||||
```
|
||||
git@ssh.dev.azure.com:v3/novenco/software/git-workshop
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
**You're all set!** SSH authentication with RSA keys is now configured for secure, passwordless Git operations with Azure DevOps.
|
||||
167
01-essentials/08-multiplayer/03_README.md
Normal file
167
01-essentials/08-multiplayer/03_README.md
Normal file
@@ -0,0 +1,167 @@
|
||||
# Multiplayer Git
|
||||
|
||||
Work with others using branches and pull requests.
|
||||
|
||||
## Goal
|
||||
|
||||
Learn to collaborate on a shared repository using:
|
||||
- **Branches** - work independently without breaking main
|
||||
- **Pull Requests** - review and merge changes safely
|
||||
|
||||
## The Workflow
|
||||
|
||||
```
|
||||
1. Create branch → 2. Make changes → 3. Push branch
|
||||
↓
|
||||
6. Delete branch ← 5. Merge PR ← 4. Create PR
|
||||
```
|
||||
|
||||
This is how professional teams work together on code.
|
||||
|
||||
---
|
||||
|
||||
## Step 1: Clone the Repository
|
||||
|
||||
Get the repository URL from your facilitator, then:
|
||||
|
||||
```powershell
|
||||
git clone <repository-url>
|
||||
code <repository-name>
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Step 2: Create a Branch
|
||||
|
||||
Never work directly on `main`. Create your own branch:
|
||||
|
||||
```powershell
|
||||
git switch -c <branch>
|
||||
```
|
||||
|
||||
This creates a new branch and switches to it.
|
||||
|
||||
---
|
||||
|
||||
## Step 3: Make Changes
|
||||
|
||||
1. Open `numbers.txt` in VS Code
|
||||
2. Move one number to its correct position
|
||||
3. Save the file (`Ctrl+S`)
|
||||
|
||||
---
|
||||
|
||||
## Step 4: Commit and Push
|
||||
|
||||
```powershell
|
||||
git add .
|
||||
git commit -m "fix: move 7 to correct position"
|
||||
git push <branch-name>
|
||||
```
|
||||
|
||||
Your branch is now on Azure DevOps.
|
||||
|
||||
---
|
||||
|
||||
## Step 5: Create a Pull Request
|
||||
|
||||
[Detailed guide](https://learn.microsoft.com/en-us/azure/devops/repos/git/pull-requests?view=azure-devops&tabs=browser#create-a-pull-request)
|
||||
|
||||
1. Go to Azure DevOps in your browser
|
||||
2. Navigate to **Repos** → **Pull Requests** 3. Click **New Pull Request**
|
||||
4. Set:
|
||||
- **Source branch:** `<branch-name>`
|
||||
- **Target branch:** `main`
|
||||
5. Add a title describing your change
|
||||
6. Click **Create**
|
||||
|
||||
---
|
||||
|
||||
## Step 6: Review and Merge
|
||||
|
||||
1. Review the changes shown in the PR (Person B)
|
||||
2. If everything looks good, click **Complete**
|
||||
3. Select **Complete merge**
|
||||
4. Your changes are now in `main`
|
||||
|
||||
---
|
||||
|
||||
## Step 7: Update Your Local Main
|
||||
|
||||
After merging, update your local copy:
|
||||
|
||||
```powershell
|
||||
git switch main
|
||||
git pull
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Step 8: Repeat
|
||||
|
||||
1. Create a new branch for your next change
|
||||
2. Make changes, commit, push
|
||||
3. Create another PR
|
||||
4. Continue until all numbers are sorted
|
||||
|
||||
---
|
||||
|
||||
## Step 9: Create a merge conflict
|
||||
|
||||
1. Both people should create a branch with changes to `feature-1` and `feature-2`, you task is to change the position of number 5. Where you place it is up to you.
|
||||
2. Now both people should push their respective branch `git push <the-branch>`
|
||||
3. Now merge `feature-1` branch first, going throught the Pull Request flow.
|
||||
4. Then merge `feature-2` branch second, and notice you'll get a MERGE CONFLICT.
|
||||
5. It is not the owner of `feature-2` branch to resolve the conflict. This is done by merge the `main` branch into `feature-2` locally and so the owner of `feature-2` has to do the following
|
||||
```pwsh
|
||||
# First get the latest changes on main
|
||||
git switch main
|
||||
git pull
|
||||
|
||||
# Then go back to the branch you can from
|
||||
git switch feature-2
|
||||
|
||||
# Now we resolve the merge. We're merging the main branch INTO the feature-2 branch.
|
||||
git merge main
|
||||
# Resolve the merge conflict in numbers.txt
|
||||
# Once resolved
|
||||
git add numbers.txt
|
||||
git commit
|
||||
# VSCode will open up with a default message of "Merge main into feature-2"
|
||||
# finish the commit. And push the changes
|
||||
git push
|
||||
```
|
||||
6. Now the owner of `feature-2` can checkout the pull request on azure again and see that the merge conflict has been resolved and can therefore "Complete" the merge request, using the button in the top right corner with the name "Complete"
|
||||
|
||||
## Quick Reference
|
||||
|
||||
| Command | What It Does |
|
||||
|---------|--------------|
|
||||
| `git switch -c <name>` | Create and switch to new branch |
|
||||
| `git push -u origin <branch>` | Push branch to Azure DevOps |
|
||||
| `git switch main` | Switch to main branch |
|
||||
| `git pull` | Get latest changes from remote |
|
||||
|
||||
---
|
||||
|
||||
## Common Issues
|
||||
|
||||
### "My PR has conflicts"
|
||||
1. Update your branch with latest main:
|
||||
```powershell
|
||||
git switch main
|
||||
git pull
|
||||
git switch <branch-name>
|
||||
git merge main
|
||||
```
|
||||
2. Resolve conflicts in VS Code
|
||||
3. Commit and push again
|
||||
|
||||
### "I need to make more changes to my PR"
|
||||
|
||||
Just commit and push to the same branch - the PR updates automatically:
|
||||
```powershell
|
||||
git add .
|
||||
git commit -m "fix: address review feedback"
|
||||
git push
|
||||
```
|
||||
148
01-essentials/08-multiplayer/README.md
Normal file
148
01-essentials/08-multiplayer/README.md
Normal file
@@ -0,0 +1,148 @@
|
||||
# Multiplayer Git
|
||||
|
||||
Work with others using branches and pull requests.
|
||||
|
||||
## Goal
|
||||
|
||||
Learn to collaborate on a shared repository using:
|
||||
- **Branches** - work independently without breaking main
|
||||
- **Pull Requests** - review and merge changes safely
|
||||
|
||||
## The Workflow
|
||||
|
||||
```
|
||||
1. Create branch → 2. Make changes → 3. Push branch
|
||||
↓
|
||||
6. Delete branch ← 5. Merge PR ← 4. Create PR
|
||||
```
|
||||
|
||||
This is how professional teams work together on code.
|
||||
|
||||
---
|
||||
|
||||
## Step 1: Clone the Repository
|
||||
|
||||
Get the repository URL from your facilitator, then:
|
||||
|
||||
```powershell
|
||||
git clone <repository-url>
|
||||
code <repository-name>
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Step 2: Create a Branch
|
||||
|
||||
Never work directly on `main`. Create your own branch:
|
||||
|
||||
```powershell
|
||||
git switch -c <branch>
|
||||
```
|
||||
|
||||
This creates a new branch and switches to it.
|
||||
|
||||
---
|
||||
|
||||
## Step 3: Make Changes
|
||||
|
||||
1. Open `numbers.txt` in VS Code
|
||||
2. Move one number to its correct position
|
||||
3. Save the file (`Ctrl+S`)
|
||||
|
||||
---
|
||||
|
||||
## Step 4: Commit and Push
|
||||
|
||||
```powershell
|
||||
git add .
|
||||
git commit -m "fix: move 7 to correct position"
|
||||
git push feature-2
|
||||
```
|
||||
|
||||
Your branch is now on Azure DevOps.
|
||||
|
||||
---
|
||||
|
||||
## Step 5: Create a Pull Request
|
||||
|
||||
1. Go to Azure DevOps in your browser
|
||||
2. Navigate to **Repos** → **Pull Requests**
|
||||
3. Click **New Pull Request**
|
||||
4. Set:
|
||||
- **Source branch:** `feature/<your-name>`
|
||||
- **Target branch:** `main`
|
||||
5. Add a title describing your change
|
||||
6. Click **Create**
|
||||
|
||||
---
|
||||
|
||||
## Step 6: Review and Merge
|
||||
|
||||
1. Review the changes shown in the PR
|
||||
2. If everything looks good, click **Complete**
|
||||
3. Select **Complete merge**
|
||||
4. Your changes are now in `main`
|
||||
|
||||
---
|
||||
|
||||
## Step 7: Update Your Local Main
|
||||
|
||||
After merging, update your local copy:
|
||||
|
||||
```powershell
|
||||
git switch main
|
||||
git pull
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Step 8: Repeat
|
||||
|
||||
1. Create a new branch for your next change
|
||||
2. Make changes, commit, push
|
||||
3. Create another PR
|
||||
4. Continue until all numbers are sorted
|
||||
|
||||
---
|
||||
|
||||
## Quick Reference
|
||||
|
||||
| Command | What It Does |
|
||||
|---------|--------------|
|
||||
| `git switch -c <name>` | Create and switch to new branch |
|
||||
| `git push -u origin <branch>` | Push branch to Azure DevOps |
|
||||
| `git switch main` | Switch to main branch |
|
||||
| `git pull` | Get latest changes from remote |
|
||||
|
||||
---
|
||||
|
||||
## Common Issues
|
||||
|
||||
### "I accidentally committed to main"
|
||||
|
||||
Switch to a new branch and push from there:
|
||||
```powershell
|
||||
git switch -c feature/<your-name>
|
||||
git push -u origin feature/<your-name>
|
||||
```
|
||||
|
||||
### "My PR has conflicts"
|
||||
|
||||
1. Update your branch with latest main:
|
||||
```powershell
|
||||
git switch main
|
||||
git pull
|
||||
git switch feature/<your-name>
|
||||
git merge main
|
||||
```
|
||||
2. Resolve conflicts in VS Code
|
||||
3. Commit and push again
|
||||
|
||||
### "I need to make more changes to my PR"
|
||||
|
||||
Just commit and push to the same branch - the PR updates automatically:
|
||||
```powershell
|
||||
git add .
|
||||
git commit -m "fix: address review feedback"
|
||||
git push
|
||||
```
|
||||
BIN
01-essentials/08-multiplayer/images/01_settings.png
Normal file
BIN
01-essentials/08-multiplayer/images/01_settings.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 33 KiB |
BIN
01-essentials/08-multiplayer/images/02_ssh_option.png
Normal file
BIN
01-essentials/08-multiplayer/images/02_ssh_option.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 91 KiB |
BIN
01-essentials/08-multiplayer/images/03_add_new_key.png
Normal file
BIN
01-essentials/08-multiplayer/images/03_add_new_key.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 75 KiB |
BIN
01-essentials/08-multiplayer/images/04_copy_paste_key.png
Normal file
BIN
01-essentials/08-multiplayer/images/04_copy_paste_key.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 170 KiB |
BIN
01-essentials/08-multiplayer/images/05_repos.png
Normal file
BIN
01-essentials/08-multiplayer/images/05_repos.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 53 KiB |
BIN
01-essentials/08-multiplayer/images/06_choose_ssh.png
Normal file
BIN
01-essentials/08-multiplayer/images/06_choose_ssh.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 165 KiB |
11
01-essentials/08-multiplayer/numbers.txt
Normal file
11
01-essentials/08-multiplayer/numbers.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
0
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
6
|
||||
7
|
||||
8
|
||||
9
|
||||
10
|
||||
146
BEST-PRACTICES.md
Normal file
146
BEST-PRACTICES.md
Normal file
@@ -0,0 +1,146 @@
|
||||
# Best Practices for Cloud-Based Git Collaboration
|
||||
|
||||
When multiple people work on the same project, merge conflicts are inevitable. But with good habits, you can dramatically reduce how often they happen and how painful they are to resolve.
|
||||
|
||||
## Pull Early, Pull Often
|
||||
|
||||
The most common cause of merge conflicts is working on outdated code. The longer you work without syncing, the more your code drifts from what others are doing.
|
||||
|
||||
**Do this:**
|
||||
```bash
|
||||
# Start every work session by pulling
|
||||
git pull
|
||||
|
||||
# Pull again before pushing
|
||||
git pull
|
||||
git push
|
||||
```
|
||||
|
||||
**Why it works:** Small, frequent syncs mean small, manageable conflicts. A conflict in 3 lines is easy to fix. A conflict in 300 lines is a nightmare.
|
||||
|
||||
## Keep Commits Small and Focused
|
||||
|
||||
Large commits that touch many files are conflict magnets.
|
||||
|
||||
**Instead of this:**
|
||||
```
|
||||
"Implemented user authentication, fixed navbar, updated styles, refactored database"
|
||||
```
|
||||
|
||||
**Do this:**
|
||||
```
|
||||
"Add login form to auth page"
|
||||
"Add password validation"
|
||||
"Connect login form to auth API"
|
||||
"Add logout button to navbar"
|
||||
```
|
||||
|
||||
**Why it works:**
|
||||
- Smaller changes = smaller chance of overlap with others
|
||||
- If a conflict does happen, it's easier to understand and resolve
|
||||
- Easier to review, revert, or cherry-pick specific changes
|
||||
|
||||
## Communicate About Shared Files
|
||||
|
||||
Some files are conflict hotspots because everyone needs to edit them:
|
||||
- Configuration files
|
||||
- Route definitions
|
||||
- Database schemas
|
||||
- Shared constants or types
|
||||
|
||||
**Do this:**
|
||||
- Tell your team when you're editing shared files
|
||||
- Make those changes in dedicated commits
|
||||
- Push changes to shared files quickly - don't let them sit
|
||||
|
||||
## Use Short-Lived Branches
|
||||
|
||||
Long-running branches drift further from the main branch every day.
|
||||
|
||||
```
|
||||
main: A───B───C───D───E───F───G───H
|
||||
\
|
||||
feature: X───────────────────────Y
|
||||
(2 weeks of drift = painful merge)
|
||||
```
|
||||
|
||||
**Better approach:**
|
||||
```
|
||||
main: A───B───C───D───E───F───G───H
|
||||
\ \ \
|
||||
feature: X───────Y───────Z
|
||||
(merge main frequently)
|
||||
```
|
||||
|
||||
**Do this:**
|
||||
- Merge main into your branch regularly (daily if active)
|
||||
- Keep features small enough to complete in days, not weeks
|
||||
- Break large features into smaller incremental changes
|
||||
|
||||
## Organize Code to Minimize Overlap
|
||||
|
||||
How you structure your code affects how often people collide.
|
||||
|
||||
**Conflict-prone structure:**
|
||||
```
|
||||
src/
|
||||
app.py # 2000 lines, everyone edits this
|
||||
utils.py # 500 lines of mixed utilities
|
||||
```
|
||||
|
||||
**Better structure:**
|
||||
```
|
||||
src/
|
||||
auth/
|
||||
login.py
|
||||
logout.py
|
||||
users/
|
||||
profile.py
|
||||
settings.py
|
||||
utils/
|
||||
dates.py
|
||||
strings.py
|
||||
```
|
||||
|
||||
**Why it works:** When each file has a clear purpose, different team members naturally work in different files.
|
||||
|
||||
## Avoid Reformatting Wars
|
||||
|
||||
Nothing creates unnecessary conflicts like two people reformatting the same file differently.
|
||||
|
||||
**Do this:**
|
||||
- Agree on code formatting standards as a team
|
||||
- Use automatic formatters (Prettier, Black, etc.)
|
||||
- Configure your editor to format on save
|
||||
- Run formatters before committing
|
||||
|
||||
**Important:** If you need to reformat a file, do it in a dedicated commit with no other changes. This keeps the reformatting separate from your actual work.
|
||||
|
||||
## Coordinate Large Refactors
|
||||
|
||||
Renaming a widely-used function or moving files around will conflict with almost everyone's work.
|
||||
|
||||
**Do this:**
|
||||
- Announce refactors to the team before starting
|
||||
- Do them quickly and push immediately
|
||||
- Consider doing them when others aren't actively working
|
||||
- Keep refactoring commits separate from feature work
|
||||
|
||||
## The Golden Rules
|
||||
|
||||
1. **Sync frequently** - Pull before you start, pull before you push
|
||||
2. **Commit small** - Many small commits beat one large commit
|
||||
3. **Talk to your team** - A quick message prevents hours of conflict resolution
|
||||
4. **Stay focused** - One branch = one purpose
|
||||
5. **Push promptly** - Don't sit on finished work
|
||||
|
||||
## When Conflicts Do Happen
|
||||
|
||||
Even with best practices, conflicts will occur. When they do:
|
||||
|
||||
1. **Don't panic** - Conflicts are normal, not failures
|
||||
2. **Read carefully** - Understand both sides before choosing
|
||||
3. **Test after resolving** - Make sure the merged code actually works
|
||||
4. **Ask if unsure** - If you don't understand the other person's code, ask them
|
||||
|
||||
Remember: merge conflicts are a communication problem as much as a technical one. The best tool for reducing conflicts is talking to your team.
|
||||
123
COMMIT-MESSAGES.md
Normal file
123
COMMIT-MESSAGES.md
Normal file
@@ -0,0 +1,123 @@
|
||||
# Writing Good Commit Messages
|
||||
|
||||
A good commit message explains **what** the change does and **why** it matters. Your future self (and your teammates) will thank you.
|
||||
|
||||
## The Golden Rule: Write the Intent
|
||||
|
||||
Write your message as a command - what will this commit **do** when applied?
|
||||
|
||||
**Good (imperative, present tense):**
|
||||
```
|
||||
Add login button to navbar
|
||||
Fix crash when username is empty
|
||||
Remove unused database connection
|
||||
```
|
||||
|
||||
**Avoid (past tense, describing what you did):**
|
||||
```
|
||||
Added login button to navbar
|
||||
Fixed crash when username is empty
|
||||
Removed unused database connection
|
||||
```
|
||||
|
||||
**Why?** Think of it as completing this sentence:
|
||||
> "If applied, this commit will... **add login button to navbar**"
|
||||
|
||||
## Use Prefixes to Categorize
|
||||
|
||||
Start your message with a prefix that tells readers what kind of change this is:
|
||||
|
||||
| Prefix | Use For | Example |
|
||||
|--------|---------|---------|
|
||||
| `feat:` | New features | `feat: add password reset flow` |
|
||||
| `fix:` | Bug fixes | `fix: prevent duplicate form submission` |
|
||||
| `docs:` | Documentation only | `docs: add API examples to README` |
|
||||
| `style:` | Formatting, no code change | `style: fix indentation in auth module` |
|
||||
| `refactor:` | Code change that doesn't fix or add | `refactor: extract validation logic` |
|
||||
| `test:` | Adding or fixing tests | `test: add unit tests for login` |
|
||||
| `chore:` | Maintenance, dependencies | `chore: update pytest to 8.0` |
|
||||
|
||||
## Keep It Short
|
||||
|
||||
The first line should be **50 characters or less**. This ensures it displays properly in:
|
||||
- Git log output
|
||||
- GitHub/Azure DevOps commit lists
|
||||
- Email notifications
|
||||
|
||||
```
|
||||
fix: resolve memory leak in image processing
|
||||
│ │
|
||||
└──────────── 45 characters ─────────────────┘
|
||||
```
|
||||
|
||||
## Add Details When Needed
|
||||
|
||||
For complex changes, add a blank line and then more context:
|
||||
|
||||
```
|
||||
fix: prevent crash when user uploads empty file
|
||||
|
||||
The application crashed because we tried to read the first byte
|
||||
of an empty file. Now we check file size before processing.
|
||||
|
||||
Closes #142
|
||||
```
|
||||
|
||||
Use the body to explain:
|
||||
- **Why** this change was necessary
|
||||
- **What** was the problem or context
|
||||
- **How** does this approach solve it (if not obvious)
|
||||
|
||||
## Examples
|
||||
|
||||
**Simple bug fix:**
|
||||
```
|
||||
fix: correct tax calculation for EU customers
|
||||
```
|
||||
|
||||
**New feature:**
|
||||
```
|
||||
feat: add dark mode toggle to settings
|
||||
```
|
||||
|
||||
**With context:**
|
||||
```
|
||||
refactor: split user service into smaller modules
|
||||
|
||||
The user service had grown to 800 lines and was handling
|
||||
authentication, profile management, and notifications.
|
||||
Split into three focused modules for maintainability.
|
||||
```
|
||||
|
||||
**Documentation:**
|
||||
```
|
||||
docs: add setup instructions for Windows
|
||||
```
|
||||
|
||||
## Quick Reference
|
||||
|
||||
```
|
||||
<prefix>: <what this commit does>
|
||||
|
||||
[optional body: why and how]
|
||||
|
||||
[optional footer: references issues]
|
||||
```
|
||||
|
||||
**Checklist:**
|
||||
- [ ] Starts with a prefix (`feat:`, `fix:`, etc.)
|
||||
- [ ] Uses imperative mood ("add" not "added")
|
||||
- [ ] First line under 50 characters
|
||||
- [ ] Explains why, not just what (for complex changes)
|
||||
|
||||
## Common Mistakes
|
||||
|
||||
| Instead of... | Write... |
|
||||
|---------------|----------|
|
||||
| `fixed bug` | `fix: prevent null pointer in search` |
|
||||
| `updates` | `feat: add email notifications` |
|
||||
| `WIP` | `feat: add basic form validation` |
|
||||
| `stuff` | `chore: clean up unused imports` |
|
||||
| `fix: fix the bug` | `fix: handle empty input in calculator` |
|
||||
|
||||
Your commit history tells the story of your project. Make it a story worth reading.
|
||||
503
GIT-CHEATSHEET.md
Normal file
503
GIT-CHEATSHEET.md
Normal file
@@ -0,0 +1,503 @@
|
||||
# Git Command Cheatsheet
|
||||
|
||||
A comprehensive reference for all Git commands covered in this workshop.
|
||||
|
||||
---
|
||||
|
||||
## Essentials
|
||||
|
||||
### Repository Initialization
|
||||
|
||||
```bash
|
||||
git init
|
||||
```
|
||||
Initialize a new Git repository in the current directory.
|
||||
|
||||
### Staging & Committing
|
||||
|
||||
```bash
|
||||
git status
|
||||
```
|
||||
Show the current state of your working directory and staging area.
|
||||
|
||||
```bash
|
||||
git add <file>
|
||||
```
|
||||
Add a file to the staging area (prepare it for commit).
|
||||
|
||||
```bash
|
||||
git add .
|
||||
```
|
||||
Add all changed files in the current directory to the staging area.
|
||||
|
||||
```bash
|
||||
git commit -m "message"
|
||||
```
|
||||
Create a new commit with the staged changes and a descriptive message.
|
||||
|
||||
```bash
|
||||
git commit -am "message"
|
||||
```
|
||||
Stage all modified tracked files and commit in one step (doesn't include new files).
|
||||
|
||||
### Viewing History
|
||||
|
||||
```bash
|
||||
git log
|
||||
```
|
||||
Show the commit history for the current branch.
|
||||
|
||||
```bash
|
||||
git log --oneline
|
||||
```
|
||||
Show commit history in compact one-line format.
|
||||
|
||||
```bash
|
||||
git log --oneline --graph --all
|
||||
```
|
||||
Show commit history as a graph with all branches.
|
||||
|
||||
```bash
|
||||
git log --stat
|
||||
```
|
||||
Show commit history with statistics about which files changed.
|
||||
|
||||
```bash
|
||||
git show <commit>
|
||||
```
|
||||
Display detailed information about a specific commit.
|
||||
|
||||
```bash
|
||||
git show <commit>:<file>
|
||||
```
|
||||
View the contents of a file from a specific commit.
|
||||
|
||||
### Comparing Changes
|
||||
|
||||
```bash
|
||||
git diff
|
||||
```
|
||||
Show unstaged changes in your working directory.
|
||||
|
||||
```bash
|
||||
git diff --staged
|
||||
```
|
||||
Show staged changes (what will be committed).
|
||||
|
||||
```bash
|
||||
git diff <commit1> <commit2>
|
||||
```
|
||||
Compare two commits.
|
||||
|
||||
```bash
|
||||
git diff <commit1> <commit2> <file>
|
||||
```
|
||||
Compare changes to a specific file between two commits.
|
||||
|
||||
**Understanding diff output:**
|
||||
- Lines with `+` (green) = added
|
||||
- Lines with `-` (red) = removed
|
||||
- Lines with ` ` (space) = unchanged (context)
|
||||
- `@@ -1,5 +1,7 @@` = location of changes (old line/count, new line/count)
|
||||
|
||||
For a detailed guide on reading diff output, see Module 02 README.md.
|
||||
|
||||
### Branching
|
||||
|
||||
```bash
|
||||
git branch
|
||||
```
|
||||
List all local branches (current branch marked with *).
|
||||
|
||||
```bash
|
||||
git branch <branch-name>
|
||||
```
|
||||
Create a new branch.
|
||||
|
||||
```bash
|
||||
git branch -d <branch-name>
|
||||
```
|
||||
Delete a branch (only if it's been merged).
|
||||
|
||||
```bash
|
||||
git branch -D <branch-name>
|
||||
```
|
||||
Force delete a branch (even if not merged).
|
||||
|
||||
```bash
|
||||
git switch <branch-name>
|
||||
```
|
||||
Switch to a different branch.
|
||||
|
||||
```bash
|
||||
git switch -c <branch-name>
|
||||
```
|
||||
Create a new branch and switch to it in one command.
|
||||
|
||||
```bash
|
||||
git switch -
|
||||
```
|
||||
Switch back to the previous branch.
|
||||
|
||||
### Merging
|
||||
|
||||
```bash
|
||||
git merge <branch-name>
|
||||
```
|
||||
Merge the specified branch into your current branch.
|
||||
|
||||
```bash
|
||||
git merge --abort
|
||||
```
|
||||
Abort a merge in progress and return to the pre-merge state.
|
||||
|
||||
### Merge Conflict Resolution
|
||||
|
||||
```bash
|
||||
git status
|
||||
```
|
||||
During a merge conflict, shows which files have conflicts.
|
||||
|
||||
```bash
|
||||
# After resolving conflicts manually:
|
||||
git add <resolved-file>
|
||||
git commit
|
||||
```
|
||||
Stage resolved files and complete the merge.
|
||||
|
||||
### Restoring Files
|
||||
|
||||
```bash
|
||||
git restore <file>
|
||||
```
|
||||
Discard changes in working directory (restore from last commit).
|
||||
|
||||
```bash
|
||||
git restore --staged <file>
|
||||
```
|
||||
Unstage a file (remove from staging area but keep changes).
|
||||
|
||||
```bash
|
||||
git restore --source=<commit> <file>
|
||||
```
|
||||
Restore a file to its state in a specific commit.
|
||||
|
||||
---
|
||||
|
||||
## Advanced
|
||||
|
||||
### Rebasing
|
||||
|
||||
```bash
|
||||
git rebase <branch>
|
||||
```
|
||||
Reapply your commits on top of another branch (creates linear history).
|
||||
|
||||
```bash
|
||||
git rebase -i <commit>
|
||||
```
|
||||
Interactive rebase - edit, reorder, squash, or drop commits.
|
||||
|
||||
```bash
|
||||
git rebase --continue
|
||||
```
|
||||
Continue rebasing after resolving conflicts.
|
||||
|
||||
```bash
|
||||
git rebase --abort
|
||||
```
|
||||
Abort a rebase and return to the original state.
|
||||
|
||||
### Reset (History Manipulation)
|
||||
|
||||
```bash
|
||||
git reset --soft HEAD~<n>
|
||||
```
|
||||
Undo last n commits but keep changes staged.
|
||||
|
||||
```bash
|
||||
git reset --mixed HEAD~<n>
|
||||
```
|
||||
Undo last n commits and unstage changes (default mode).
|
||||
|
||||
```bash
|
||||
git reset --hard HEAD~<n>
|
||||
```
|
||||
Undo last n commits and discard all changes (DESTRUCTIVE).
|
||||
|
||||
```bash
|
||||
git reset --hard <commit>
|
||||
```
|
||||
Reset your branch to a specific commit, discarding all changes.
|
||||
|
||||
### Revert (Safe Undo)
|
||||
|
||||
```bash
|
||||
git revert <commit>
|
||||
```
|
||||
Create a new commit that undoes changes from a specific commit (safe for shared branches).
|
||||
|
||||
```bash
|
||||
git revert --no-commit <commit>
|
||||
```
|
||||
Revert changes but don't create the commit yet (allows editing).
|
||||
|
||||
```bash
|
||||
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
|
||||
git cherry-pick <commit>
|
||||
```
|
||||
Apply the changes from a specific commit to your current branch.
|
||||
|
||||
```bash
|
||||
git cherry-pick <commit1> <commit2> <commit3>
|
||||
```
|
||||
Cherry-pick multiple commits.
|
||||
|
||||
```bash
|
||||
git cherry-pick --continue
|
||||
```
|
||||
Continue cherry-picking after resolving conflicts.
|
||||
|
||||
```bash
|
||||
git cherry-pick --abort
|
||||
```
|
||||
Abort a cherry-pick in progress.
|
||||
|
||||
### Stash (Temporary Storage)
|
||||
|
||||
```bash
|
||||
git stash
|
||||
```
|
||||
Save your uncommitted changes temporarily and revert to a clean working directory.
|
||||
|
||||
```bash
|
||||
git stash save "description"
|
||||
```
|
||||
Stash changes with a descriptive message.
|
||||
|
||||
```bash
|
||||
git stash list
|
||||
```
|
||||
Show all stashed changes.
|
||||
|
||||
```bash
|
||||
git stash pop
|
||||
```
|
||||
Apply the most recent stash and remove it from the stash list.
|
||||
|
||||
```bash
|
||||
git stash apply
|
||||
```
|
||||
Apply the most recent stash but keep it in the stash list.
|
||||
|
||||
```bash
|
||||
git stash apply stash@{n}
|
||||
```
|
||||
Apply a specific stash by index.
|
||||
|
||||
```bash
|
||||
git stash drop
|
||||
```
|
||||
Delete the most recent stash.
|
||||
|
||||
```bash
|
||||
git stash drop stash@{n}
|
||||
```
|
||||
Delete a specific stash.
|
||||
|
||||
```bash
|
||||
git stash clear
|
||||
```
|
||||
Delete all stashes.
|
||||
|
||||
### Working with Remotes
|
||||
|
||||
```bash
|
||||
git clone <url>
|
||||
```
|
||||
Create a local copy of a remote repository.
|
||||
|
||||
```bash
|
||||
git remote
|
||||
```
|
||||
List all remote repositories.
|
||||
|
||||
```bash
|
||||
git remote -v
|
||||
```
|
||||
List all remote repositories with their URLs.
|
||||
|
||||
```bash
|
||||
git remote add <name> <url>
|
||||
```
|
||||
Add a new remote repository.
|
||||
|
||||
```bash
|
||||
git remote remove <name>
|
||||
```
|
||||
Remove a remote repository.
|
||||
|
||||
```bash
|
||||
git fetch <remote>
|
||||
```
|
||||
Download changes from remote but don't merge them.
|
||||
|
||||
```bash
|
||||
git pull <remote> <branch>
|
||||
```
|
||||
Fetch and merge changes from remote branch to current branch.
|
||||
|
||||
```bash
|
||||
git pull
|
||||
```
|
||||
Fetch and merge from the tracked remote branch.
|
||||
|
||||
```bash
|
||||
git push <remote> <branch>
|
||||
```
|
||||
Upload your commits to a remote branch.
|
||||
|
||||
```bash
|
||||
git push -u <remote> <branch>
|
||||
```
|
||||
Push and set up tracking relationship (use -u first time pushing a branch).
|
||||
|
||||
```bash
|
||||
git push
|
||||
```
|
||||
Push to the tracked remote branch.
|
||||
|
||||
### Worktrees (Multiple Working Directories)
|
||||
|
||||
```bash
|
||||
git worktree add <path> <branch>
|
||||
```
|
||||
Create a new working directory linked to your repository for a specific branch.
|
||||
|
||||
```bash
|
||||
git worktree add <path> -b <new-branch>
|
||||
```
|
||||
Create a new branch and working directory for it.
|
||||
|
||||
```bash
|
||||
git worktree list
|
||||
```
|
||||
Show all worktrees associated with the repository.
|
||||
|
||||
```bash
|
||||
git worktree remove <path>
|
||||
```
|
||||
Remove a worktree.
|
||||
|
||||
```bash
|
||||
git worktree prune
|
||||
```
|
||||
Clean up stale worktree administrative data.
|
||||
|
||||
### Bisect (Binary Search for Bugs)
|
||||
|
||||
```bash
|
||||
git bisect start
|
||||
```
|
||||
Start a bisect session to find which commit introduced a bug.
|
||||
|
||||
```bash
|
||||
git bisect bad
|
||||
```
|
||||
Mark the current commit as bad (contains the bug).
|
||||
|
||||
```bash
|
||||
git bisect good <commit>
|
||||
```
|
||||
Mark a commit as good (doesn't contain the bug).
|
||||
|
||||
```bash
|
||||
git bisect reset
|
||||
```
|
||||
End the bisect session and return to the original branch.
|
||||
|
||||
```bash
|
||||
git bisect run <script>
|
||||
```
|
||||
Automatically test commits using a script to find the bad commit.
|
||||
|
||||
---
|
||||
|
||||
## Useful Tips
|
||||
|
||||
### Viewing Changes
|
||||
|
||||
- Use `git log --oneline --graph --all` to visualize branch structure
|
||||
- Use `git log -p` to show the actual changes (patch) in each commit
|
||||
- Use `git log --author="name"` to filter commits by author
|
||||
- Use `git log --since="2 weeks ago"` to filter commits by date
|
||||
|
||||
### Undoing Changes
|
||||
|
||||
- **Haven't committed yet?** Use `git restore <file>` to discard changes
|
||||
- **Staged but want to unstage?** Use `git restore --staged <file>`
|
||||
- **Committed to wrong branch?** Use `git cherry-pick` to copy the commit to the right branch
|
||||
- **Want to change last commit message?** Use `git commit --amend`
|
||||
- **Pushed to remote already?** Use `git revert` (not reset) to safely undo
|
||||
|
||||
### Best Practices
|
||||
|
||||
- Commit often with clear, descriptive messages
|
||||
- Pull before you push to avoid conflicts
|
||||
- Use branches for new features or experiments
|
||||
- Don't rebase or reset commits that have been pushed to shared branches
|
||||
- Use `git stash` when you need to switch contexts quickly
|
||||
- Test your changes before committing
|
||||
|
||||
### Getting Help
|
||||
|
||||
```bash
|
||||
git help <command>
|
||||
```
|
||||
Show detailed help for any Git command.
|
||||
|
||||
```bash
|
||||
git <command> --help
|
||||
```
|
||||
Alternative way to show help.
|
||||
|
||||
```bash
|
||||
git <command> -h
|
||||
```
|
||||
Show brief usage summary.
|
||||
|
||||
---
|
||||
|
||||
## Module Reference
|
||||
|
||||
Each workshop module focuses on specific commands:
|
||||
|
||||
- **Module 01**: init, add, commit, status
|
||||
- **Module 02**: log, show, diff
|
||||
- **Module 03**: branch, switch
|
||||
- **Module 04**: merge (fast-forward and three-way)
|
||||
- **Module 05**: merge (conflict resolution)
|
||||
- **Module 06**: rebase
|
||||
- **Module 07**: reset (interactive rebase alternative)
|
||||
- **Module 08**: cherry-pick
|
||||
- **Module 09**: reset vs revert
|
||||
- **Module 10**: stash
|
||||
- **Module 11**: clone, remote, push, pull, fetch
|
||||
- **Module 12**: worktree
|
||||
- **Module 13**: bisect
|
||||
337
INSTALLATION.md
Normal file
337
INSTALLATION.md
Normal file
@@ -0,0 +1,337 @@
|
||||
# Installation Guide for Windows 11
|
||||
|
||||
This guide will help you install everything needed for the Git Workshop on Windows 11.
|
||||
|
||||
## Quick Start (Automated Installation)
|
||||
|
||||
**Easiest option:** Run our oneshot installation script that installs all prerequisites and clones the repository automatically.
|
||||
|
||||
```powershell
|
||||
irm https://git.frod.dk/floppydiscen/git-workshop/raw/branch/main/install.ps1 | iex
|
||||
```
|
||||
|
||||
This will:
|
||||
- Install PowerShell 7, Git 2.23+, and Visual Studio Code
|
||||
- Clone the git-workshop repository to `~/git-workshop`
|
||||
- Leave you ready to start the workshop immediately
|
||||
|
||||
**Alternative:** If you've already cloned the repository, you can run the local installation script:
|
||||
|
||||
1. Open **PowerShell** or **Windows Terminal**
|
||||
2. Navigate to the git-workshop directory
|
||||
3. Run the installation script:
|
||||
|
||||
```powershell
|
||||
.\install-prerequisites.ps1
|
||||
```
|
||||
|
||||
The script will:
|
||||
- Check if tools are already installed
|
||||
- Install PowerShell 7, Git 2.23+, and Visual Studio Code
|
||||
- Prompt you for optional tools (Python 3.12, Windows Terminal)
|
||||
- Show clear progress and verify each installation
|
||||
- Display Git configuration instructions when complete
|
||||
|
||||
**If you prefer manual installation**, continue with the detailed steps below.
|
||||
|
||||
---
|
||||
|
||||
## Prerequisites
|
||||
|
||||
You'll need administrator access to install software on your Windows 11 machine.
|
||||
|
||||
## What You'll Install
|
||||
|
||||
1. **PowerShell 7** - Modern cross-platform PowerShell (replaces the older Windows PowerShell 5.1)
|
||||
2. **Git** - Version control system (2.23 or later)
|
||||
3. **Visual Studio Code** - Modern code editor with excellent Git integration
|
||||
|
||||
## Manual Installation Steps
|
||||
|
||||
### 1. Install PowerShell 7
|
||||
|
||||
PowerShell 7 is the modern, cross-platform version of PowerShell. Windows 11 comes with PowerShell 5.1, but we recommend PowerShell 7 for the best experience.
|
||||
|
||||
**Option A: Using winget (Recommended)**
|
||||
|
||||
Open **Windows Terminal** or **Command Prompt** and run:
|
||||
|
||||
```powershell
|
||||
winget install --id Microsoft.PowerShell --source winget
|
||||
```
|
||||
|
||||
**Option B: Manual Download**
|
||||
|
||||
1. Visit https://github.com/PowerShell/PowerShell/releases/latest
|
||||
2. Download the file ending in `-win-x64.msi` (e.g., `PowerShell-7.4.1-win-x64.msi`)
|
||||
3. Run the installer
|
||||
4. Accept all defaults
|
||||
|
||||
**Verify Installation:**
|
||||
|
||||
Open a new terminal and run:
|
||||
|
||||
```powershell
|
||||
pwsh --version
|
||||
```
|
||||
|
||||
You should see version 7.x.x or higher.
|
||||
|
||||
**Important:** After installing PowerShell 7, use it instead of the older "Windows PowerShell 5.1". Look for "PowerShell 7" in your Start menu or Windows Terminal.
|
||||
|
||||
### 2. Install Git
|
||||
|
||||
Git is the version control system you'll learn in this workshop. You need version 2.23 or later.
|
||||
|
||||
**Option A: Using winget (Recommended)**
|
||||
|
||||
```powershell
|
||||
winget install --id Git.Git -e --source winget
|
||||
```
|
||||
|
||||
**Option B: Manual Download**
|
||||
|
||||
1. Visit https://git-scm.com/downloads
|
||||
2. Click "Windows"
|
||||
3. Download the 64-bit installer
|
||||
4. Run the installer with these recommended settings:
|
||||
- **Default editor**: Choose "Visual Studio Code" (we'll install it next)
|
||||
- **PATH environment**: Select "Git from the command line and also from 3rd-party software"
|
||||
- **Line ending conversions**: Choose "Checkout Windows-style, commit Unix-style line endings"
|
||||
- **Terminal emulator**: Choose "Use Windows' default console window"
|
||||
- All other settings: Accept defaults
|
||||
|
||||
**Verify Installation:**
|
||||
|
||||
Open a **new** PowerShell window and run:
|
||||
|
||||
```powershell
|
||||
git --version
|
||||
```
|
||||
|
||||
You should see version 2.23 or higher (e.g., `git version 2.43.0`).
|
||||
|
||||
### 3. Install Visual Studio Code
|
||||
|
||||
VS Code is a free, powerful code editor with excellent Git integration.
|
||||
|
||||
**Option A: Using winget (Recommended)**
|
||||
|
||||
```powershell
|
||||
winget install --id Microsoft.VisualStudioCode --source winget
|
||||
```
|
||||
|
||||
**Option B: Manual Download**
|
||||
|
||||
1. Visit https://code.visualstudio.com/
|
||||
2. Click "Download for Windows"
|
||||
3. Run the installer
|
||||
4. During installation, check these options:
|
||||
- ✅ Add "Open with Code" action to Windows Explorer file context menu
|
||||
- ✅ Add "Open with Code" action to Windows Explorer directory context menu
|
||||
- ✅ Register Code as an editor for supported file types
|
||||
- ✅ Add to PATH
|
||||
|
||||
**Verify Installation:**
|
||||
|
||||
```powershell
|
||||
code --version
|
||||
```
|
||||
|
||||
You should see version information.
|
||||
|
||||
**Recommended VS Code Extensions:**
|
||||
|
||||
Open VS Code and install these extensions for the best Git experience:
|
||||
|
||||
1. **GitLens** - Supercharge Git capabilities
|
||||
- Press `Ctrl+Shift+X` to open Extensions
|
||||
- Search for "GitLens"
|
||||
- Click Install
|
||||
|
||||
2. **Git Graph** - View Git history visually
|
||||
- Search for "Git Graph"
|
||||
- Click Install
|
||||
|
||||
3. **PowerShell** - Better PowerShell support
|
||||
- Search for "PowerShell"
|
||||
- Install the one from Microsoft
|
||||
|
||||
## Configure Git
|
||||
|
||||
Before making your first commit, tell Git who you are:
|
||||
|
||||
```powershell
|
||||
git config --global user.name "Your Name"
|
||||
git config --global user.email "your.email@example.com"
|
||||
```
|
||||
|
||||
**Verify your configuration:**
|
||||
|
||||
```powershell
|
||||
git config --global user.name
|
||||
git config --global user.email
|
||||
```
|
||||
|
||||
You should see your name and email printed.
|
||||
|
||||
**Optional: Set VS Code as Git's Default Editor**
|
||||
|
||||
If you installed Git before VS Code, configure Git to use VS Code:
|
||||
|
||||
```powershell
|
||||
git config --global core.editor "code --wait"
|
||||
```
|
||||
|
||||
## PowerShell Execution Policy
|
||||
|
||||
When running PowerShell scripts (`.ps1` files) in this workshop, you might encounter an error about execution policies.
|
||||
|
||||
**If you see an error like "script cannot be loaded because running scripts is disabled":**
|
||||
|
||||
Open **PowerShell 7 as Administrator** and run:
|
||||
|
||||
```powershell
|
||||
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
|
||||
```
|
||||
|
||||
This allows you to run local scripts while maintaining security for downloaded scripts.
|
||||
|
||||
## Running Scripts in the Workshop
|
||||
|
||||
After installation, you can run workshop scripts using:
|
||||
|
||||
```powershell
|
||||
.\setup.ps1
|
||||
.\verify.ps1
|
||||
.\reset.ps1
|
||||
```
|
||||
|
||||
**Example workflow:**
|
||||
|
||||
```powershell
|
||||
# Navigate to a module
|
||||
cd 01-essentials\01-basics
|
||||
|
||||
# Run the setup script
|
||||
.\setup.ps1
|
||||
|
||||
# Complete the challenge using Git commands
|
||||
# ...
|
||||
|
||||
# Verify your solution
|
||||
.\verify.ps1
|
||||
```
|
||||
|
||||
## Optional: Python (for Module 08 only)
|
||||
|
||||
Module 08 (Multiplayer Git) uses Python for "The Great Print Project". You only need this for that specific module.
|
||||
|
||||
**Install Python 3.12:**
|
||||
|
||||
```powershell
|
||||
winget install --id Python.Python.3.12 --source winget
|
||||
```
|
||||
|
||||
**Verify installation:**
|
||||
|
||||
```powershell
|
||||
python --version
|
||||
```
|
||||
|
||||
You should see Python 3.12.x or higher.
|
||||
|
||||
## Optional: Windows Terminal (Highly Recommended)
|
||||
|
||||
Windows Terminal provides a modern terminal experience with tabs, better colors, and PowerShell 7 integration.
|
||||
|
||||
**Install:**
|
||||
|
||||
```powershell
|
||||
winget install --id Microsoft.WindowsTerminal --source winget
|
||||
```
|
||||
|
||||
Or install from the **Microsoft Store** (search for "Windows Terminal").
|
||||
|
||||
**After installation:**
|
||||
- Press `Win+X` and select "Windows Terminal"
|
||||
- Or search "Terminal" in the Start menu
|
||||
- PowerShell 7 should be the default profile
|
||||
|
||||
## Verify Complete Installation
|
||||
|
||||
Run these commands to verify everything is installed correctly:
|
||||
|
||||
```powershell
|
||||
# PowerShell version (should be 7.x.x)
|
||||
pwsh --version
|
||||
|
||||
# Git version (should be 2.23 or higher)
|
||||
git --version
|
||||
|
||||
# VS Code version
|
||||
code --version
|
||||
|
||||
# Git configuration
|
||||
git config --global user.name
|
||||
git config --global user.email
|
||||
|
||||
# Optional: Python (for Module 08)
|
||||
python --version
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Git command not found
|
||||
|
||||
If `git --version` doesn't work after installation:
|
||||
1. Close and reopen your terminal (Git needs a new terminal to update PATH)
|
||||
2. Restart your computer if the problem persists
|
||||
|
||||
### VS Code command not found
|
||||
|
||||
If `code --version` doesn't work:
|
||||
1. Ensure you checked "Add to PATH" during installation
|
||||
2. Close and reopen your terminal
|
||||
3. If still not working, reinstall VS Code with the PATH option enabled
|
||||
|
||||
### PowerShell execution policy errors
|
||||
|
||||
If you can't run `.ps1` scripts:
|
||||
1. Open PowerShell 7 **as Administrator**
|
||||
2. Run: `Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser`
|
||||
3. Close admin PowerShell and try again in a regular PowerShell window
|
||||
|
||||
### winget command not found
|
||||
|
||||
If `winget` doesn't work:
|
||||
1. Update Windows 11 to the latest version (Settings → Windows Update)
|
||||
2. Install "App Installer" from the Microsoft Store
|
||||
3. Restart your computer
|
||||
|
||||
## You're Ready!
|
||||
|
||||
Once all verification commands work, you're ready to start the workshop!
|
||||
|
||||
```powershell
|
||||
# Clone or download the git-workshop repository
|
||||
# Navigate to it
|
||||
cd path\to\git-workshop
|
||||
|
||||
# Start with Module 01
|
||||
cd 01-essentials\01-basics
|
||||
|
||||
# Read the instructions
|
||||
code README.md
|
||||
|
||||
# Run setup and begin!
|
||||
.\setup.ps1
|
||||
```
|
||||
|
||||
## Next Steps
|
||||
|
||||
- Read the main [README.md](README.md) for workshop overview
|
||||
- Check [GIT-CHEATSHEET.md](GIT-CHEATSHEET.md) for Git command reference
|
||||
- Start with Module 01: `01-essentials\01-basics`
|
||||
|
||||
Happy learning!
|
||||
472
README.md
472
README.md
@@ -1,37 +1,165 @@
|
||||
# Git Workshop
|
||||
|
||||
A hands-on, progressive git workshop with practical challenges ranging from basic to advanced concepts.
|
||||
A comprehensive, hands-on Git workshop with 15 progressive modules covering everything from basic commits to advanced debugging techniques and real-world collaboration. Each module presents a real-world scenario that you must solve using Git commands, with automated verification to confirm your solution.
|
||||
|
||||
Perfect for developers who want to move beyond basic Git usage and master professional workflows including rebasing, conflict resolution, collaboration, and advanced Git techniques.
|
||||
|
||||
## Workshop Structure
|
||||
|
||||
Each module is a self-contained challenge that teaches specific git concepts:
|
||||
The workshop is organized into two tracks:
|
||||
|
||||
- **Module 01**: Git Basics (init, add, commit, status)
|
||||
- **Module 02**: Viewing History (log, diff)
|
||||
- **Module 03**: Branching Basics
|
||||
- **Module 04**: Merging
|
||||
- **Module 05**: Merge Conflicts
|
||||
- **Module 06**: Rebasing
|
||||
- **Module 07**: Interactive Rebase
|
||||
- **Module 08**: Cherry-pick
|
||||
- **Module 09**: Reset vs Revert
|
||||
- **Module 10**: Stash
|
||||
- **Module 11**: Working with Remotes
|
||||
### 01 Essentials - Core Git Skills (8 modules)
|
||||
|
||||
Master fundamental Git concepts and collaborative workflows:
|
||||
|
||||
- **Module 01: Git Basics** - Initialize repositories, stage changes, make commits
|
||||
- **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: 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)
|
||||
|
||||
Advanced Git workflows for power users:
|
||||
|
||||
- **Module 01: Rebasing** - Rebase branches to create linear history
|
||||
- **Module 02: Interactive Rebase** - Clean up commit history before submitting pull requests
|
||||
- **Module 03: Worktrees** - Work on multiple branches simultaneously
|
||||
- **Module 04: Bisect** - Use binary search to find bug-introducing commits
|
||||
- **Module 05: Blame** - Code archaeology - investigate who changed what and when
|
||||
- **Module 06: Merge Strategies** - Master fast-forward vs three-way merges and when to use each
|
||||
|
||||
## How to Use This Workshop
|
||||
|
||||
1. Navigate to a module directory (e.g., `module-01-basics`)
|
||||
### For Local Modules (01-08 in Essentials, all Advanced modules)
|
||||
|
||||
1. Navigate to a module directory (e.g., `01-essentials/01-basics`)
|
||||
2. Read the `README.md` to understand the challenge
|
||||
3. Run `./setup.ps1` to create the challenge environment
|
||||
4. Complete the challenge using git commands
|
||||
5. Run `./verify.ps1` to check if you've solved it correctly
|
||||
6. Move to the next module
|
||||
|
||||
**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 08: Multiplayer Git
|
||||
|
||||
**This module is different!** It uses Azure DevOps for authentic cloud-based collaboration:
|
||||
|
||||
1. Navigate to `01-essentials/08-multiplayer`
|
||||
2. Read the `README.md` for complete instructions
|
||||
3. **No setup script** - you'll clone from Azure DevOps (URL provided by facilitator)
|
||||
4. Work with a partner on shared branches
|
||||
5. Experience real merge conflicts and pull requests
|
||||
6. Use SSH keys for secure authentication (best practice)
|
||||
7. **No verify script** - success is visual (your code appears in the final output)
|
||||
|
||||
**Facilitators**: See `01-essentials/08-multiplayer/FACILITATOR-SETUP.md` for server setup and workshop guidance.
|
||||
|
||||
## Running PowerShell Scripts
|
||||
|
||||
Most modules include setup, verification, and reset scripts.
|
||||
|
||||
If you encounter an "execution policy" error when running scripts, open PowerShell as Administrator and run:
|
||||
|
||||
```powershell
|
||||
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
|
||||
```
|
||||
|
||||
Then run scripts using:
|
||||
```powershell
|
||||
.\setup.ps1
|
||||
.\verify.ps1
|
||||
.\reset.ps1
|
||||
```
|
||||
|
||||
## Requirements
|
||||
|
||||
- Git installed and configured
|
||||
- PowerShell (Windows PowerShell 5.1+ or PowerShell Core 7+)
|
||||
- Basic command line knowledge
|
||||
### Prerequisites
|
||||
|
||||
Install these tools before starting:
|
||||
|
||||
**PowerShell 7+**
|
||||
```powershell
|
||||
winget install Microsoft.PowerShell
|
||||
```
|
||||
|
||||
**Git 2.23+**
|
||||
```powershell
|
||||
winget install Git.Git
|
||||
```
|
||||
|
||||
**Visual Studio Code**
|
||||
```powershell
|
||||
winget install Microsoft.VisualStudioCode
|
||||
```
|
||||
|
||||
### Quick Start
|
||||
|
||||
**Option 1: Oneshot Installation (Recommended)**
|
||||
Install everything and clone the repository in one command:
|
||||
|
||||
```powershell
|
||||
irm https://git.frod.dk/floppydiscen/git-workshop/raw/branch/main/install.ps1 | iex
|
||||
```
|
||||
|
||||
**Option 2: Manual Setup**
|
||||
1. Install the prerequisites above
|
||||
2. Clone this repository:
|
||||
```powershell
|
||||
git clone https://git.frod.dk/floppydiscen/git-workshop.git
|
||||
```
|
||||
3. Configure Git:
|
||||
```powershell
|
||||
git config --global user.name "Your Name"
|
||||
git config --global user.email "your.email@example.com"
|
||||
```
|
||||
|
||||
**Quick Check:**
|
||||
|
||||
You need the following software installed:
|
||||
|
||||
- **Git 2.23+** - Version control system
|
||||
```powershell
|
||||
git --version
|
||||
```
|
||||
|
||||
- **PowerShell 7+**
|
||||
```powershell
|
||||
pwsh --version
|
||||
```
|
||||
|
||||
- **Python 3.6+** (for Module 08 only)
|
||||
```powershell
|
||||
python --version
|
||||
```
|
||||
|
||||
**First-Time Git Configuration:**
|
||||
|
||||
Before making your first commit, configure Git with your identity:
|
||||
|
||||
```powershell
|
||||
git config --global user.name "Your Name"
|
||||
git config --global user.email "your.email@example.com"
|
||||
```
|
||||
|
||||
Verify your configuration:
|
||||
```powershell
|
||||
git config --global user.name
|
||||
git config --global user.email
|
||||
```
|
||||
|
||||
### Basic Command Line Knowledge
|
||||
|
||||
You should know how to:
|
||||
- Navigate directories (`cd`, `ls` or `dir`)
|
||||
- See your current location (`pwd`)
|
||||
- Read text files (`cat` or `type`)
|
||||
|
||||
Don't worry if you're not an expert - we'll guide you through each step!
|
||||
|
||||
## Optional: Install Glow for Pretty Markdown
|
||||
|
||||
@@ -44,31 +172,140 @@ This workshop includes many markdown files with instructions. You can install `g
|
||||
After installation, read markdown files with:
|
||||
|
||||
```powershell
|
||||
# Windows
|
||||
.\bin\glow.exe README.md
|
||||
|
||||
# macOS/Linux
|
||||
./bin/glow README.md
|
||||
```
|
||||
|
||||
**Pro tip**: Use `glow -p` for pager mode on longer files!
|
||||
|
||||
Without glow, you can still read markdown files with any text editor or `cat README.md`.
|
||||
|
||||
## Common Git Terms
|
||||
|
||||
New to Git? Here are the key terms you'll encounter:
|
||||
|
||||
- **Repository (or "repo")**: A project folder tracked by Git, containing your files and their complete history
|
||||
- **Commit**: A snapshot of your project at a specific point in time (like a save point in a video game)
|
||||
- **Staging Area (or "Index")**: A preparation area where you select which changes to include in your next commit
|
||||
- **Working Directory**: The actual files you see and edit on your computer
|
||||
- **Branch**: An independent line of development (like a parallel universe for your code)
|
||||
- **HEAD**: A pointer showing which commit you're currently working from
|
||||
- **main/master**: The primary branch in a repository (main is the modern convention)
|
||||
- **Merge**: Combining changes from different branches
|
||||
- **Remote**: A version of your repository hosted elsewhere (like GitHub, GitLab, or Gitea)
|
||||
- **Pull Request (PR)**: A request to merge your changes into another branch (used for code review)
|
||||
- **Conflict**: When Git can't automatically merge changes because both versions modified the same lines
|
||||
|
||||
Don't worry if these don't make sense yet - you'll learn them hands-on as you progress!
|
||||
|
||||
## Getting Started
|
||||
|
||||
Start with Module 01:
|
||||
**First time?** Make sure you have Git and PowerShell installed - see [INSTALLATION.md](INSTALLATION.md) for Windows 11 setup instructions.
|
||||
|
||||
Once installed, start with Essentials Module 01:
|
||||
|
||||
```powershell
|
||||
cd module-01-basics
|
||||
cd 01-essentials\01-basics
|
||||
.\setup.ps1
|
||||
```
|
||||
|
||||
Follow the instructions in each module's README.md file.
|
||||
|
||||
## What Makes This Workshop Different
|
||||
|
||||
- **Hands-On Practice**: Each module creates a real Git scenario you must solve
|
||||
- **Automated Verification**: Scripts check your solution instantly (modules 01-08)
|
||||
- **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 08 uses an actual Git server for authentic teamwork
|
||||
- **Comprehensive Coverage**: From `git init` to advanced rebasing and bisecting
|
||||
|
||||
## Learning Path
|
||||
|
||||
The modules are designed to build on each other:
|
||||
|
||||
### Recommended Progression
|
||||
|
||||
**Phase 1: Core Fundamentals (Essentials 01-02)**
|
||||
- Git Basics, History
|
||||
- **Goal**: Understand commits and history
|
||||
|
||||
**Phase 2: Collaboration Basics (Essentials 03)**
|
||||
- Branching and Merging (checkpoint-based: branching, merging, conflicts)
|
||||
- **Goal**: Work with multiple branches and resolve conflicts
|
||||
|
||||
**Phase 3: Workflow Tools (Essentials 04-06)**
|
||||
- Cherry-Pick, Reset vs Revert, Stash
|
||||
- **Goal**: Manage your work effectively
|
||||
|
||||
**Phase 4: Real Collaboration (Essentials 07)**
|
||||
- **Multiplayer Git - The Great Print Project**
|
||||
- **Goal**: Apply all skills with real teammates on a cloud server
|
||||
- **Note**: This is a capstone module - bring everything together!
|
||||
|
||||
**Phase 5: Advanced Techniques (Advanced 01-06)**
|
||||
- Rebasing, Interactive Rebase, Worktrees, Bisect, Blame, Merge Strategies
|
||||
- **Goal**: Master professional Git workflows
|
||||
- **When**: After completing Essentials and feeling confident
|
||||
|
||||
### Alternative Paths
|
||||
|
||||
**Fast Track (1 day workshop):**
|
||||
- Essentials 01-03 + Essentials 07 (Multiplayer)
|
||||
|
||||
**Solo Learner:**
|
||||
- Complete Essentials 01-06, skip 07 (requires partners and server)
|
||||
- Or complete all Advanced modules for deep mastery
|
||||
|
||||
**Team Workshop:**
|
||||
- Essentials 01-03 then jump to 07 (Multiplayer) for collaborative practice
|
||||
|
||||
## Tips for Success
|
||||
|
||||
- **Don't skip modules** - each builds on previous concepts
|
||||
- **Read the README.md thoroughly** before starting each challenge
|
||||
- **Keep [GIT-CHEATSHEET.md](GIT-CHEATSHEET.md) open** as a quick reference
|
||||
- **Experiment freely** - you can always run `./reset.ps1` to start over
|
||||
- **Use `git log --oneline --graph --all`** frequently to visualize repository state
|
||||
- **If stuck**, check the Key Concepts section in the module's README
|
||||
- **Consider installing glow** for better markdown reading experience
|
||||
- **For Module 07**, work with a partner - collaboration is the point!
|
||||
|
||||
## Skills You'll Master
|
||||
|
||||
By completing this workshop, you'll be able to:
|
||||
|
||||
### From Essentials Track:
|
||||
- ✅ Create and manage Git repositories with confidence
|
||||
- ✅ Navigate project history and understand what changed when
|
||||
- ✅ Use branches effectively for parallel development
|
||||
- ✅ Merge branches and resolve conflicts like a pro
|
||||
- ✅ Apply specific commits with cherry-pick
|
||||
- ✅ Choose correctly between reset and revert
|
||||
- ✅ Use stash to manage work-in-progress without commits
|
||||
- ✅ **Collaborate with teammates on shared repositories**
|
||||
- ✅ **Resolve real merge conflicts in a team environment**
|
||||
- ✅ **Create and review pull requests**
|
||||
- ✅ **Use Git on a real cloud server (Azure DevOps)**
|
||||
- ✅ **Use SSH keys for secure Git authentication**
|
||||
|
||||
### From Advanced Track:
|
||||
- ✅ Rebase to maintain clean, linear history
|
||||
- ✅ Clean up messy commits before submitting pull requests
|
||||
- ✅ Work on multiple branches simultaneously with worktrees
|
||||
- ✅ Debug efficiently by finding bug-introducing commits with bisect
|
||||
- ✅ Investigate code history with git blame
|
||||
- ✅ Master different merge strategies and when to use each
|
||||
|
||||
These are professional-level Git skills used daily by developers at top tech companies.
|
||||
|
||||
## For Workshop Facilitators
|
||||
|
||||
Before distributing this workshop to attendees:
|
||||
This repository can be used for **self-paced learning** or as a **facilitated workshop**.
|
||||
|
||||
### Self-Paced Distribution
|
||||
|
||||
Before distributing this workshop to attendees for self-study:
|
||||
|
||||
1. **Delete the root `.git` directory**: This prevents confusion and ensures attendees practice git from scratch
|
||||
```powershell
|
||||
@@ -76,3 +313,190 @@ Before distributing this workshop to attendees:
|
||||
```
|
||||
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 08 (Multiplayer) requires you to set up a Git server - see facilitator guide below.
|
||||
|
||||
### Facilitated Workshop
|
||||
|
||||
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 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 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
|
||||
- Create student accounts on your Git server
|
||||
|
||||
The workshop format combines instructor-led sessions with self-paced hands-on modules for an engaging learning experience.
|
||||
|
||||
### Setting Up Module 08: Multiplayer Git
|
||||
|
||||
Module 08 requires a Git server for authentic collaboration using **Azure DevOps**.
|
||||
|
||||
**Azure DevOps Setup**
|
||||
|
||||
Use Azure DevOps as the cloud-based Git platform for this module:
|
||||
|
||||
**Benefits:**
|
||||
- 💰 Free tier supports up to 5 users with full access
|
||||
- 🌐 Cloud-hosted - no server maintenance required
|
||||
- 🔒 Enterprise-grade security and reliability
|
||||
- 🔑 Built-in SSH key support (industry best practice)
|
||||
- 👥 Perfect for workshops with any number of students (use Stakeholder licenses for >5 users)
|
||||
- 📊 Built-in pull request workflows and code review tools
|
||||
|
||||
**Setup Steps:**
|
||||
|
||||
1. **Create Azure DevOps Organization** (if you don't have one):
|
||||
- Sign up at [dev.azure.com](https://dev.azure.com) with a Microsoft account
|
||||
- Create a new organization for your workshop
|
||||
|
||||
2. **Set up SSH authentication** (recommended for all users):
|
||||
- See [AZURE-DEVOPS-SSH-SETUP.md](AZURE-DEVOPS-SSH-SETUP.md) for complete SSH key setup instructions
|
||||
- SSH provides secure, passwordless authentication (industry standard)
|
||||
|
||||
3. **Configure workshop repository and users**:
|
||||
- See `01-essentials/08-multiplayer/FACILITATOR-SETUP.md` for detailed workshop preparation:
|
||||
- Adding student accounts to Azure DevOps
|
||||
- Creating The Great Print Project repository
|
||||
- Configuring branch policies
|
||||
- Pairing students
|
||||
- Monitoring progress
|
||||
- Troubleshooting SSH and authentication issues
|
||||
|
||||
**Alternative: GitHub / GitLab / Bitbucket**
|
||||
|
||||
While this workshop uses Azure DevOps, the skills learned apply to any Git platform:
|
||||
- The workflow is identical across all platforms
|
||||
- SSH authentication works the same way everywhere
|
||||
- Pull request concepts transfer directly
|
||||
- Students can apply these skills to any Git hosting service
|
||||
|
||||
---
|
||||
|
||||
## Repository Structure
|
||||
|
||||
```
|
||||
git-workshop/
|
||||
├── README.md # This file
|
||||
├── INSTALLATION.md # Windows 11 installation guide (PowerShell 7, Git, VS Code)
|
||||
├── install-prerequisites.ps1 # Automated installation script (one-shot setup)
|
||||
├── GIT-CHEATSHEET.md # Quick reference for all Git commands
|
||||
├── WORKSHOP-AGENDA.md # Facilitator guide for running workshops
|
||||
├── PRESENTATION-OUTLINE.md # Slide deck outline
|
||||
├── AZURE-DEVOPS-SSH-SETUP.md # SSH authentication best practices for Azure DevOps
|
||||
├── install-glow.ps1 # Install glow markdown renderer
|
||||
│
|
||||
├── 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-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
|
||||
│
|
||||
└── 02-advanced/ # Professional techniques (6 modules)
|
||||
├── 01-rebasing/ # Linear history with rebase
|
||||
├── 02-interactive-rebase/ # Clean up commits
|
||||
├── 03-worktrees/ # Multiple branches simultaneously
|
||||
├── 04-bisect/ # Find bugs with binary search
|
||||
├── 05-blame/ # Code archaeology
|
||||
└── 06-merge-strategies/ # Master merge techniques
|
||||
```
|
||||
|
||||
## What's Unique About This Workshop
|
||||
|
||||
### The Great Print Project (Module 08)
|
||||
|
||||
Unlike any other Git tutorial, Module 08 provides **real collaborative experience**:
|
||||
|
||||
- **Real Git server**: Not simulated - actual Azure DevOps cloud repository
|
||||
- **Real teammates**: Work in pairs on shared branches
|
||||
- **Real conflicts**: Both partners edit the same code and must resolve conflicts together
|
||||
- **Real pull requests**: Create PRs, review code, merge to main
|
||||
- **Real success**: When all pairs merge, run `python main.py` and see everyone's contributions!
|
||||
- **Real security**: Use SSH keys for authentication (industry best practice)
|
||||
|
||||
**The challenge**: Each pair implements 3 Python functions (e.g., `print_b()`, `print_c()`, `print_d()`) in a shared repository. When complete, the program prints the alphabet A-Z and numbers 0-9.
|
||||
|
||||
**What students learn**:
|
||||
- Push/pull workflow with real teammates
|
||||
- Handling rejected pushes (partner pushed first!)
|
||||
- Resolving actual merge conflicts (not simulated)
|
||||
- Creating meaningful pull requests
|
||||
- Reviewing others' code
|
||||
- Staying synchronized with a team
|
||||
|
||||
This is how professional developers actually work - no simulation, no shortcuts.
|
||||
|
||||
---
|
||||
|
||||
## Frequently Asked Questions
|
||||
|
||||
**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 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?**
|
||||
A:
|
||||
- Essentials 01-05: 3-4 hours
|
||||
- Full Essentials (01-09): 6-7 hours
|
||||
- All modules: 12-15 hours
|
||||
- Self-paced over several days works great!
|
||||
|
||||
**Q: I'm stuck on a module. What should I do?**
|
||||
A:
|
||||
1. Re-read the module README.md
|
||||
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 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-07 work completely offline. Module 08 requires internet to access the Git server.
|
||||
|
||||
**Q: What if I prefer GitHub/GitLab instead of Azure DevOps?**
|
||||
A: The skills are identical across all Git platforms. Module 08 uses Azure DevOps but everything you learn applies directly to GitHub, GitLab, Bitbucket, and any other Git hosting service. The SSH authentication, pull request workflow, and collaboration patterns are the same everywhere.
|
||||
|
||||
---
|
||||
|
||||
## Contributing
|
||||
|
||||
Found a bug or have a suggestion? Please open an issue or submit a pull request!
|
||||
|
||||
---
|
||||
|
||||
## License
|
||||
|
||||
This workshop is provided as-is for educational purposes.
|
||||
|
||||
---
|
||||
|
||||
**Ready to master Git?**
|
||||
|
||||
First-time setup (Windows 11): See [INSTALLATION.md](INSTALLATION.md)
|
||||
|
||||
Then start with Essentials Module 01 and begin your journey!
|
||||
|
||||
```powershell
|
||||
cd 01-essentials\01-basics
|
||||
.\setup.ps1
|
||||
```
|
||||
|
||||
Happy Learning! 🚀
|
||||
|
||||
124
WHAT-IS-GIT.md
Normal file
124
WHAT-IS-GIT.md
Normal file
@@ -0,0 +1,124 @@
|
||||
# What is Git?
|
||||
|
||||
Git is a tool that tracks changes to your files over time. Think of it as an "undo history" for your entire project that you can browse, search, and share with others.
|
||||
|
||||
## Commits: Snapshots of Your Project
|
||||
|
||||
A **commit** is like taking a photo of your entire project at a specific moment in time.
|
||||
|
||||
Every time you make a commit, Git:
|
||||
1. Records what **all** your files look like right now
|
||||
2. Adds a message describing what changed
|
||||
3. Notes who made the change and when
|
||||
4. Links back to the previous commit
|
||||
|
||||
### Every Commit Contains Everything
|
||||
|
||||
This is important: each commit is a complete snapshot of **all** files in your project - not just the files you changed. You can check out any commit and see the entire project exactly as it was.
|
||||
|
||||
But wait - doesn't that waste a lot of space? No! Git is clever about this.
|
||||
|
||||
### Unchanged Files Are Reused
|
||||
|
||||
If a file hasn't changed since the last commit, Git doesn't store a new copy. Instead, it simply points to the version it already has:
|
||||
|
||||
```
|
||||
Commit 1 Commit 2 Commit 3
|
||||
───────── ───────── ─────────
|
||||
README.md ─────────────────────────────> (same)
|
||||
app.py ────────> app.py (v2) ───────> (same)
|
||||
config.json ──────> (same) ────────────> config.json (v3)
|
||||
```
|
||||
|
||||
In this example:
|
||||
- `README.md` never changed - all three commits refer to the same stored version
|
||||
- `app.py` changed in Commit 2, so a new version was stored
|
||||
- `config.json` changed in Commit 3, so a new version was stored
|
||||
|
||||
This means:
|
||||
- Every commit gives you the **complete picture** of your project
|
||||
- Git only stores **new content** when files actually change
|
||||
- Going back to any point in history is instant - no need to "replay" changes
|
||||
|
||||
```
|
||||
Commit 3 Commit 2 Commit 1
|
||||
| | |
|
||||
v v v
|
||||
[Add login] <-- [Fix bug] <-- [First version]
|
||||
```
|
||||
|
||||
Each commit points back to its parent, creating a chain of history. You can always go back and see exactly what your project looked like at any point.
|
||||
|
||||
## The Magic of Checksums
|
||||
|
||||
Here's where Git gets clever. Every commit gets a unique ID called a **checksum** (or "hash"). It looks like this:
|
||||
|
||||
```
|
||||
a1b2c3d4e5f6g7h8i9j0...
|
||||
```
|
||||
|
||||
This ID is calculated from the **contents** of the commit - the files, the message, the author, and the parent commit's ID.
|
||||
|
||||
Why does this matter?
|
||||
|
||||
### Verification
|
||||
|
||||
If even one character changes in a file, the checksum becomes completely different. This means:
|
||||
- Git instantly knows if something has been corrupted or tampered with
|
||||
- You can trust that what you downloaded is exactly what was uploaded
|
||||
|
||||
### Finding Differences
|
||||
|
||||
When you connect to another copy of the repository, Git compares checksums:
|
||||
|
||||
```
|
||||
Your computer: Server:
|
||||
Commit A Commit A (same checksum = identical)
|
||||
Commit B Commit B (same checksum = identical)
|
||||
Commit C (missing) (you have something new!)
|
||||
```
|
||||
|
||||
Git doesn't need to compare every file. It just compares the short checksums to instantly know what's different.
|
||||
|
||||
## Distributed: Everyone Has a Full Copy
|
||||
|
||||
Unlike older systems where one central server held all the history, Git is **distributed**. This means:
|
||||
|
||||
- Every person has a complete copy of the entire project history
|
||||
- You can work offline - commit, browse history, create branches
|
||||
- If the server disappears, anyone's copy can restore everything
|
||||
- You sync with others by exchanging commits
|
||||
|
||||
```
|
||||
[Alice's Computer] [Bob's Computer]
|
||||
| |
|
||||
Full history Full history
|
||||
All branches All branches
|
||||
| |
|
||||
+---- [Shared Server] ------+
|
||||
|
|
||||
Full history
|
||||
All branches
|
||||
```
|
||||
|
||||
When Alice pushes her new commits to the server, Bob can pull them down. The checksums ensure nothing gets lost or corrupted in transit.
|
||||
|
||||
## Putting It All Together
|
||||
|
||||
1. **You work** - Edit files, create new ones, delete old ones
|
||||
2. **You commit** - Take a snapshot with a descriptive message
|
||||
3. **Git calculates** - Creates a unique checksum for this commit
|
||||
4. **You push** - Send your commits to a shared server
|
||||
5. **Others pull** - Download your commits using checksums to verify
|
||||
6. **History grows** - The chain of commits gets longer
|
||||
|
||||
That's it! Git is essentially a distributed database of snapshots, connected together and verified by checksums. Everything else - branches, merges, rebasing - builds on these simple ideas.
|
||||
|
||||
## Key Takeaways
|
||||
|
||||
- **Commit** = A snapshot of your project at one moment
|
||||
- **Checksum** = A unique fingerprint calculated from the content
|
||||
- **Distributed** = Everyone has a full copy, not just the server
|
||||
- **History** = A chain of commits, each pointing to its parent
|
||||
|
||||
You don't need to understand every detail to use Git effectively. Just remember: commit often, write clear messages, and sync with your team regularly.
|
||||
960
install.ps1
Normal file
960
install.ps1
Normal file
@@ -0,0 +1,960 @@
|
||||
#!/usr/bin/env pwsh
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Installs all prerequisites for Git Workshop using winget and clones the repository.
|
||||
|
||||
.DESCRIPTION
|
||||
This script automates the installation of required tools for the Git Workshop:
|
||||
- PowerShell 7 (cross-platform PowerShell)
|
||||
- Git 2.23+ (version control system)
|
||||
- Visual Studio Code (code editor with Git integration)
|
||||
|
||||
Optional tools (with user prompts):
|
||||
- Windows Terminal (modern terminal experience)
|
||||
|
||||
The script checks for existing installations, shows clear progress, and verifies
|
||||
each installation succeeded. At the end, it clones the repository and can
|
||||
open it in VSCode for immediate workshop access.
|
||||
|
||||
One-shot installation:
|
||||
Invoke-RestMethod -Uri https://git.frod.dk/floppydiscen/git-workshop/raw/branch/main/install.ps1 | Invoke-Expression
|
||||
|
||||
.EXAMPLE
|
||||
PS> Invoke-RestMethod -Uri https://git.frod.dk/floppydiscen/git-workshop/raw/branch/main/install.ps1 | Invoke-Expression
|
||||
Runs the complete installation and setup in one command.
|
||||
|
||||
.EXAMPLE
|
||||
PS> .\install.ps1
|
||||
Runs the installation script with interactive prompts.
|
||||
|
||||
.NOTES
|
||||
Requires Windows 11 with winget (App Installer) available.
|
||||
Some installations may require administrator privileges.
|
||||
#>
|
||||
|
||||
[CmdletBinding()]
|
||||
param()
|
||||
|
||||
Set-StrictMode -Version Latest
|
||||
$ErrorActionPreference = 'Continue' # Continue on errors to show all results
|
||||
|
||||
#region Helper Functions
|
||||
|
||||
function Write-ColorMessage {
|
||||
param(
|
||||
[string]$Message,
|
||||
[string]$Color = 'White'
|
||||
)
|
||||
Write-Host $Message -ForegroundColor $Color
|
||||
}
|
||||
|
||||
function Write-Step {
|
||||
param([string]$Message)
|
||||
Write-ColorMessage "`n=== $Message ===" -Color Cyan
|
||||
}
|
||||
|
||||
function Write-Success {
|
||||
param([string]$Message)
|
||||
Write-ColorMessage " ✓ $Message" -Color Green
|
||||
}
|
||||
|
||||
function Write-Warning {
|
||||
param([string]$Message)
|
||||
Write-ColorMessage " ⚠ $Message" -Color Yellow
|
||||
}
|
||||
|
||||
function Write-Error {
|
||||
param([string]$Message)
|
||||
Write-ColorMessage " ✗ $Message" -Color Red
|
||||
}
|
||||
|
||||
function Test-CommandExists {
|
||||
param([string]$Command)
|
||||
|
||||
$oldPreference = $ErrorActionPreference
|
||||
$ErrorActionPreference = 'SilentlyContinue'
|
||||
|
||||
try {
|
||||
if (Get-Command $Command -ErrorAction SilentlyContinue) {
|
||||
return $true
|
||||
}
|
||||
return $false
|
||||
}
|
||||
finally {
|
||||
$ErrorActionPreference = $oldPreference
|
||||
}
|
||||
}
|
||||
|
||||
function Get-InstalledVersion {
|
||||
param(
|
||||
[string]$Command,
|
||||
[string]$VersionArg = '--version'
|
||||
)
|
||||
|
||||
try {
|
||||
$output = & $Command $VersionArg 2>&1 | Select-Object -First 1
|
||||
if ($null -ne $output) {
|
||||
return $output.ToString().Trim()
|
||||
}
|
||||
return $null
|
||||
}
|
||||
catch {
|
||||
return $null
|
||||
}
|
||||
}
|
||||
|
||||
function Test-WingetAvailable {
|
||||
if (-not (Test-CommandExists 'winget')) {
|
||||
Write-Error "winget is not available on this system."
|
||||
Write-Host "`nTo fix this:" -ForegroundColor Yellow
|
||||
Write-Host " 1. Update Windows 11 to the latest version (Settings → Windows Update)" -ForegroundColor White
|
||||
Write-Host " 2. Install 'App Installer' from the Microsoft Store" -ForegroundColor White
|
||||
Write-Host " 3. Restart your computer and run this script again" -ForegroundColor White
|
||||
return $false
|
||||
}
|
||||
return $true
|
||||
}
|
||||
|
||||
function Install-Package {
|
||||
param(
|
||||
[string]$Name,
|
||||
[string]$WingetId,
|
||||
[string]$CheckCommand,
|
||||
[string]$MinVersion = $null,
|
||||
[string]$AdditionalArgs = ''
|
||||
)
|
||||
|
||||
Write-Step "Installing $Name"
|
||||
|
||||
# Check if already installed
|
||||
if (Test-CommandExists $CheckCommand) {
|
||||
$version = Get-InstalledVersion $CheckCommand
|
||||
Write-Success "$Name is already installed: $version"
|
||||
|
||||
if ($MinVersion -and $version) {
|
||||
# Extract semantic version numbers only - stop before any non-digit/non-dot characters
|
||||
# This extracts "2.52.0" from "2.52.0.windows.1"
|
||||
if ($version -match '(\d+)(?:\.(\d+))?(?:\.(\d+))?') {
|
||||
$major = $matches[1]
|
||||
$minor = if ($matches[2]) { $matches[2] } else { "0" }
|
||||
$patch = if ($matches[3]) { $matches[3] } else { "0" }
|
||||
$installedVersion = "$major.$minor.$patch"
|
||||
try {
|
||||
if ([version]$installedVersion -lt [version]$MinVersion) {
|
||||
Write-Warning "Version $installedVersion is below minimum required version $MinVersion"
|
||||
Write-Host " Attempting to upgrade..." -ForegroundColor Cyan
|
||||
}
|
||||
else {
|
||||
return $true
|
||||
}
|
||||
}
|
||||
catch {
|
||||
Write-Warning "Version comparison failed - assuming sufficient version"
|
||||
return $true
|
||||
}
|
||||
}
|
||||
else {
|
||||
Write-Warning "Could not parse version from: $version"
|
||||
Write-Host " Assuming installed version is sufficient..." -ForegroundColor Cyan
|
||||
return $true
|
||||
}
|
||||
}
|
||||
else {
|
||||
return $true
|
||||
}
|
||||
}
|
||||
|
||||
# Install using winget
|
||||
Write-Host " Installing via winget: $WingetId" -ForegroundColor Cyan
|
||||
|
||||
$installCmd = "winget install --id $WingetId --source winget --silent $AdditionalArgs".Trim()
|
||||
Write-Host " Running: $installCmd" -ForegroundColor Gray
|
||||
|
||||
try {
|
||||
# Show progress during installation
|
||||
Write-Progress -Activity "Installing $Name" -Status "Downloading and installing..." -PercentComplete 25
|
||||
|
||||
$result = Invoke-Expression $installCmd 2>&1
|
||||
|
||||
Write-Progress -Activity "Installing $Name" -Status "Verifying installation..." -PercentComplete 75
|
||||
|
||||
# Check if installation succeeded
|
||||
Start-Sleep -Seconds 2 # Give the system time to register the new command
|
||||
|
||||
# Refresh environment variables in current session
|
||||
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
|
||||
|
||||
if (Test-CommandExists $CheckCommand) {
|
||||
$version = Get-InstalledVersion $CheckCommand
|
||||
Write-Success "$Name installed successfully: $version"
|
||||
Write-Progress -Activity "Installing $Name" -Completed
|
||||
return $true
|
||||
}
|
||||
else {
|
||||
Write-Warning "$Name installation completed, but command '$CheckCommand' not found."
|
||||
Write-Host " You may need to restart your terminal or computer." -ForegroundColor Yellow
|
||||
Write-Progress -Activity "Installing $Name" -Completed
|
||||
return $false
|
||||
}
|
||||
}
|
||||
catch {
|
||||
Write-Error "Failed to install $Name`: $_"
|
||||
Write-Progress -Activity "Installing $Name" -Completed
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
function Test-GitVersion {
|
||||
if (-not (Test-CommandExists 'git')) {
|
||||
return $false
|
||||
}
|
||||
|
||||
$version = Get-InstalledVersion 'git'
|
||||
|
||||
# Parse Git version from various formats:
|
||||
# "git version 2.52.0", "git version 2.52.0.windows.1", etc.
|
||||
if ($version -match 'git version (\d+)(?:\.(\d+))?(?:\.(\d+))?') {
|
||||
$majorVersion = [int]$matches[1]
|
||||
$minorVersion = [int]$matches[2]
|
||||
|
||||
# Check if version is 2.23 or higher
|
||||
if ($majorVersion -gt 2 -or ($majorVersion -eq 2 -and $minorVersion -ge 23)) {
|
||||
return $true
|
||||
}
|
||||
else {
|
||||
Write-Warning "Git version $majorVersion.$minorVersion is below required version 2.23"
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
Write-Warning "Could not parse Git version from: $version"
|
||||
return $false
|
||||
}
|
||||
|
||||
function Get-UserConfirmation {
|
||||
param([string]$Prompt)
|
||||
|
||||
while ($true) {
|
||||
$response = Read-Host "$Prompt (y/n)"
|
||||
$response = $response.Trim().ToLower()
|
||||
|
||||
if ($response -eq 'y' -or $response -eq 'yes') {
|
||||
return $true
|
||||
}
|
||||
elseif ($response -eq 'n' -or $response -eq 'no') {
|
||||
return $false
|
||||
}
|
||||
else {
|
||||
Write-Host "Please enter 'y' or 'n'" -ForegroundColor Yellow
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Prerequisites Check Function
|
||||
|
||||
function Get-SystemPrerequisites {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Checks which required tools are installed and their versions.
|
||||
|
||||
.DESCRIPTION
|
||||
Scans the system for PowerShell 7, Git 2.23+, and Visual Studio Code.
|
||||
Returns a hashtable with installation status and version information.
|
||||
#>
|
||||
|
||||
$prereqs = @{
|
||||
PowerShell = @{
|
||||
Installed = $false
|
||||
Version = $null
|
||||
Command = "pwsh"
|
||||
MinVersion = "7.0"
|
||||
Name = "PowerShell 7"
|
||||
Sufficient = $false
|
||||
}
|
||||
Git = @{
|
||||
Installed = $false
|
||||
Version = $null
|
||||
Command = "git"
|
||||
MinVersion = "2.23"
|
||||
Name = "Git"
|
||||
Sufficient = $false
|
||||
}
|
||||
VSCode = @{
|
||||
Installed = $false
|
||||
Version = $null
|
||||
Command = "code"
|
||||
MinVersion = $null
|
||||
Name = "Visual Studio Code"
|
||||
Sufficient = $false
|
||||
}
|
||||
WindowsTerminal = @{
|
||||
Installed = $false
|
||||
Version = $null
|
||||
Command = "wt"
|
||||
MinVersion = $null
|
||||
Name = "Windows Terminal"
|
||||
Sufficient = $false
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host "Checking system for installed tools..." -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
|
||||
foreach ($key in $prereqs.Keys) {
|
||||
$tool = $prereqs[$key]
|
||||
|
||||
if (Test-CommandExists $tool.Command) {
|
||||
$tool.Installed = $true
|
||||
$tool.Version = Get-InstalledVersion $tool.Command
|
||||
|
||||
if ($tool.MinVersion -and $tool.Version) {
|
||||
# Extract semantic version for comparison
|
||||
if ($tool.Version -match '(\d+)(?:\.(\d+))?(?:\.(\d+))?') {
|
||||
$major = [int]$matches[1]
|
||||
$minor = if ($matches[2]) { [int]$matches[2] } else { 0 }
|
||||
$patch = if ($matches[3]) { [int]$matches[3] } else { 0 }
|
||||
$installedVersion = "$major.$minor.$patch"
|
||||
|
||||
$minParts = $tool.MinVersion.Split('.')
|
||||
$minMajor = [int]$minParts[0]
|
||||
$minMinor = if ($minParts.Length -gt 1) { [int]$minParts[1] } else { 0 }
|
||||
$minPatch = if ($minParts.Length -gt 2) { [int]$minParts[2] } else { 0 }
|
||||
$minVersion = "$minMajor.$minMinor.$minPatch"
|
||||
|
||||
if ([version]$installedVersion -lt [version]$minVersion) {
|
||||
Write-Host " ✓ $($tool.Name): $($tool.Version) (⚠ below required $($tool.MinVersion))" -ForegroundColor Yellow
|
||||
$tool.Sufficient = $false
|
||||
} else {
|
||||
Write-Host " ✓ $($tool.Name): $($tool.Version)" -ForegroundColor Green
|
||||
$tool.Sufficient = $true
|
||||
}
|
||||
} else {
|
||||
Write-Host " ✓ $($tool.Name): $($tool.Version) (⚠ cannot parse version)" -ForegroundColor Yellow
|
||||
$tool.Sufficient = $true # Assume sufficient if we can't parse
|
||||
}
|
||||
} else {
|
||||
Write-Host " ✓ $($tool.Name): $($tool.Version)" -ForegroundColor Green
|
||||
$tool.Sufficient = $true
|
||||
}
|
||||
} else {
|
||||
Write-Host " ✗ $($tool.Name): Not installed" -ForegroundColor Red
|
||||
$tool.Sufficient = $false
|
||||
}
|
||||
}
|
||||
|
||||
return $prereqs
|
||||
}
|
||||
|
||||
function Show-PrerequisitesSummary {
|
||||
param([hashtable]$Prereqs)
|
||||
|
||||
Write-Host ""
|
||||
Write-Step "Prerequisites Summary"
|
||||
|
||||
$requiredTools = @("PowerShell", "Git", "VSCode")
|
||||
$allRequiredInstalled = $true
|
||||
$allRequiredSufficient = $true
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "Required Tools:" -ForegroundColor White
|
||||
|
||||
foreach ($toolName in $requiredTools) {
|
||||
$tool = $Prereqs[$toolName]
|
||||
if ($tool.Installed -and $tool.Sufficient) {
|
||||
Write-Success " $($tool.Name): $($tool.Version)"
|
||||
} elseif ($tool.Installed) {
|
||||
Write-Warning " $($tool.Name): $($tool.Version) (needs upgrade)"
|
||||
$allRequiredSufficient = $false
|
||||
} else {
|
||||
Write-Error " $($tool.Name): Not installed"
|
||||
$allRequiredInstalled = $false
|
||||
$allRequiredSufficient = $false
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "Optional Tools:" -ForegroundColor White
|
||||
|
||||
$wt = $Prereqs.WindowsTerminal
|
||||
if ($wt.Installed) {
|
||||
Write-Success " $($wt.Name): $($wt.Version)"
|
||||
} else {
|
||||
Write-Host " $($wt.Name): Not installed" -ForegroundColor Gray
|
||||
}
|
||||
|
||||
return @{
|
||||
AllRequiredInstalled = $allRequiredInstalled -and $allRequiredSufficient
|
||||
AnyMissing = -not $allRequiredInstalled
|
||||
AnyInsufficient = -not $allRequiredSufficient
|
||||
}
|
||||
}
|
||||
|
||||
function Get-UserChoice {
|
||||
param([hashtable]$PrereqStatus)
|
||||
|
||||
Write-Host ""
|
||||
Write-Step "Choose Your Action"
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "What would you like to do?" -ForegroundColor White
|
||||
Write-Host " 1) Install/update tools and clone workshop repository" -ForegroundColor Cyan
|
||||
Write-Host " 2) Just clone the workshop repository (skip tool installation)" -ForegroundColor Yellow
|
||||
Write-Host ""
|
||||
|
||||
if ($PrereqStatus.AllRequiredInstalled) {
|
||||
Write-Host "Note: All required tools are already installed" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "Note: Some required tools are missing or need updates" -ForegroundColor Yellow
|
||||
}
|
||||
Write-Host ""
|
||||
|
||||
while ($true) {
|
||||
$choice = Read-Host "Enter your choice (1-2)"
|
||||
switch ($choice.Trim()) {
|
||||
"1" { return "InstallTools" }
|
||||
"2" { return "CloneOnly" }
|
||||
default { Write-Host "Please enter 1 or 2" -ForegroundColor Yellow }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Main Script
|
||||
|
||||
Write-Host @"
|
||||
|
||||
╔═══════════════════════════════════════════════════════════╗
|
||||
║ ║
|
||||
║ Git Workshop - Prerequisites Installation Script ║
|
||||
║ ║
|
||||
╚═══════════════════════════════════════════════════════════╝
|
||||
|
||||
"@ -ForegroundColor Cyan
|
||||
|
||||
# Check for winget first
|
||||
Write-Step "Checking System Requirements"
|
||||
|
||||
if (-not (Test-WingetAvailable)) {
|
||||
Write-Host "`nInstallation cannot continue without winget." -ForegroundColor Red
|
||||
return
|
||||
}
|
||||
|
||||
Write-Success "winget is available"
|
||||
|
||||
# Check system prerequisites
|
||||
$prereqs = Get-SystemPrerequisites
|
||||
$prereqStatus = Show-PrerequisitesSummary $prereqs
|
||||
$userChoice = Get-UserChoice $prereqStatus
|
||||
|
||||
Write-Host ""
|
||||
Write-Step "User Choice: $userChoice"
|
||||
|
||||
# Track installation results
|
||||
$results = @{
|
||||
PowerShell = $prereqs.PowerShell.Installed -and $prereqs.PowerShell.Sufficient
|
||||
Git = $prereqs.Git.Installed -and $prereqs.Git.Sufficient
|
||||
VSCode = $prereqs.VSCode.Installed -and $prereqs.VSCode.Sufficient
|
||||
VSCodeExtensions = $false
|
||||
VSCodePowerShellIntegration = $null # null = not asked, true = configured, false = skipped/failed
|
||||
WindowsTerminal = $null
|
||||
}
|
||||
|
||||
# Progress tracking
|
||||
$totalSteps = 4 # Required installations + extensions
|
||||
$currentStep = 0
|
||||
|
||||
function Write-ProgressIndicator {
|
||||
param(
|
||||
[string]$Activity,
|
||||
[string]$Status,
|
||||
[int]$PercentComplete
|
||||
)
|
||||
|
||||
Write-Progress -Activity $Activity -Status $Status -PercentComplete $PercentComplete
|
||||
}
|
||||
|
||||
function Should-Install {
|
||||
param(
|
||||
[string]$ToolName,
|
||||
[string]$UserChoice,
|
||||
[hashtable]$Prereqs
|
||||
)
|
||||
|
||||
$tool = $Prereqs[$ToolName]
|
||||
|
||||
switch ($UserChoice) {
|
||||
"CloneOnly" {
|
||||
return $false
|
||||
}
|
||||
"InstallTools" {
|
||||
# Install only if missing or insufficient (smart install)
|
||||
return -not ($tool.Installed -and $tool.Sufficient)
|
||||
}
|
||||
default {
|
||||
return $false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function Install-VSCodeExtension {
|
||||
param(
|
||||
[string]$ExtensionId,
|
||||
[string]$ExtensionName
|
||||
)
|
||||
|
||||
Write-Host " Installing VSCode extension: $ExtensionName" -ForegroundColor Cyan
|
||||
|
||||
try {
|
||||
# Refresh environment to ensure code command is available
|
||||
$env:Path = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
|
||||
|
||||
$result = & code --install-extension $ExtensionId 2>&1
|
||||
|
||||
if ($LASTEXITCODE -eq 0) {
|
||||
Write-Success "VSCode extension '$ExtensionName' installed successfully"
|
||||
return $true
|
||||
}
|
||||
else {
|
||||
Write-Warning "Failed to install VSCode extension '$ExtensionName'"
|
||||
Write-Host " You can install it manually later: Ctrl+Shift+X → Search '$ExtensionName'" -ForegroundColor Gray
|
||||
return $false
|
||||
}
|
||||
}
|
||||
catch {
|
||||
Write-Warning "Could not install VSCode extension '$ExtensionName`: $_"
|
||||
Write-Host " You can install it manually later: Ctrl+Shift+X → Search '$ExtensionName'" -ForegroundColor Gray
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
function Set-VSCodePowerShellIntegration {
|
||||
# Ask user if they want to set PowerShell 7 as default terminal
|
||||
$setAsDefault = Get-UserConfirmation "Set PowerShell 7 as the default terminal in VSCode? (Recommended for this workshop)"
|
||||
|
||||
if (-not $setAsDefault) {
|
||||
Write-Host " Skipping PowerShell 7 terminal configuration." -ForegroundColor Gray
|
||||
return $false
|
||||
}
|
||||
|
||||
Write-Host " Configuring PowerShell 7 integration with VSCode..." -ForegroundColor Cyan
|
||||
|
||||
try {
|
||||
# Set PowerShell 7 as the default terminal in VSCode
|
||||
$vscodeSettingsPath = Join-Path $env:APPDATA "Code\User\settings.json"
|
||||
$vscodeSettingsDir = Split-Path $vscodeSettingsPath -Parent
|
||||
|
||||
# Create directory if it doesn't exist
|
||||
if (-not (Test-Path $vscodeSettingsDir)) {
|
||||
New-Item -Path $vscodeSettingsDir -ItemType Directory -Force | Out-Null
|
||||
}
|
||||
|
||||
# Read existing settings or create new
|
||||
if (Test-Path $vscodeSettingsPath) {
|
||||
$settings = Get-Content $vscodeSettingsPath -Raw | ConvertFrom-Json
|
||||
}
|
||||
else {
|
||||
$settings = @{}
|
||||
}
|
||||
|
||||
# Set PowerShell 7 as default terminal
|
||||
if (-not $settings.PSObject.Properties.Name -contains "terminal.integrated.defaultProfile.windows") {
|
||||
$settings | Add-Member -NotePropertyName "terminal.integrated.defaultProfile.windows" -NotePropertyValue "PowerShell"
|
||||
}
|
||||
else {
|
||||
$settings."terminal.integrated.defaultProfile.windows" = "PowerShell"
|
||||
}
|
||||
|
||||
# Add terminal profiles if not present
|
||||
if (-not $settings.PSObject.Properties.Name -contains "terminal.integrated.profiles.windows") {
|
||||
$profiles = @{
|
||||
"PowerShell" = @{
|
||||
"source" = "PowerShell"
|
||||
"icon" = "terminal-powershell"
|
||||
"path" = "pwsh.exe"
|
||||
}
|
||||
}
|
||||
$settings | Add-Member -NotePropertyName "terminal.integrated.profiles.windows" -NotePropertyValue $profiles
|
||||
}
|
||||
|
||||
# Save settings
|
||||
$settings | ConvertTo-Json -Depth 10 | Set-Content $vscodeSettingsPath
|
||||
|
||||
Write-Success "VSCode configured to use PowerShell 7 as default terminal"
|
||||
return $true
|
||||
}
|
||||
catch {
|
||||
Write-Warning "Could not configure VSCode PowerShell integration automatically"
|
||||
Write-Host " You can configure it manually in VSCode: Ctrl+Shift+P → Terminal: Select Default Profile → PowerShell" -ForegroundColor Gray
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
#region Installation Based on User Choice
|
||||
|
||||
if ($userChoice -ne "CloneOnly") {
|
||||
Write-Host "`nStarting installation based on your choice..." -ForegroundColor Cyan
|
||||
Write-Host "Note: Some installations may take a few minutes." -ForegroundColor Gray
|
||||
Write-Host ""
|
||||
|
||||
# Calculate steps needed
|
||||
$neededSteps = 0
|
||||
if (Should-Install -ToolName "PowerShell" -UserChoice $userChoice -Prereqs $prereqs) { $neededSteps++ }
|
||||
if (Should-Install -ToolName "Git" -UserChoice $userChoice -Prereqs $prereqs) { $neededSteps++ }
|
||||
if (Should-Install -ToolName "VSCode" -UserChoice $userChoice -Prereqs $prereqs) { $neededSteps++ }
|
||||
if (Should-Install -ToolName "WindowsTerminal" -UserChoice $userChoice -Prereqs $prereqs) { $neededSteps++ }
|
||||
if ($neededSteps -eq 0) { $neededSteps = 1 } # At least for progress bar
|
||||
if ($prereqs.VSCode.Installed -or (Should-Install -ToolName "VSCode" -UserChoice $userChoice -Prereqs $prereqs)) { $neededSteps++ } # For VSCode extensions
|
||||
|
||||
$currentStep = 0
|
||||
$totalSteps = $neededSteps
|
||||
|
||||
#region Required Installations
|
||||
|
||||
# Install PowerShell 7
|
||||
if (Should-Install -ToolName "PowerShell" -UserChoice $userChoice -Prereqs $prereqs) {
|
||||
$currentStep++
|
||||
Write-ProgressIndicator -Activity "Installing Tools" -Status "Installing PowerShell 7 ($currentStep/$totalSteps)" -PercentComplete (($currentStep / $totalSteps) * 100)
|
||||
$results.PowerShell = Install-Package `
|
||||
-Name "PowerShell 7" `
|
||||
-WingetId "Microsoft.PowerShell" `
|
||||
-CheckCommand "pwsh"
|
||||
} else {
|
||||
Write-Success "PowerShell 7 already installed and sufficient"
|
||||
}
|
||||
|
||||
# Install Git
|
||||
if (Should-Install -ToolName "Git" -UserChoice $userChoice -Prereqs $prereqs) {
|
||||
$currentStep++
|
||||
Write-ProgressIndicator -Activity "Installing Tools" -Status "Installing Git ($currentStep/$totalSteps)" -PercentComplete (($currentStep / $totalSteps) * 100)
|
||||
$results.Git = Install-Package `
|
||||
-Name "Git" `
|
||||
-WingetId "Git.Git" `
|
||||
-CheckCommand "git" `
|
||||
-MinVersion "2.23" `
|
||||
-AdditionalArgs "-e"
|
||||
|
||||
Write-Host " Setting the init.defaultBranch to be 'main'"
|
||||
git config --global init.defaultBranch main
|
||||
|
||||
Write-Host " Setting the default editor to code, to handle merge messages"
|
||||
git config --global core.editor "code --wait"
|
||||
|
||||
Write-Host " Setting vscode at the default code editor for merge conflicts"
|
||||
git config --global merge.tool vscode
|
||||
git config --global mergetool.vscode.cmd 'code --wait --merge $REMOTE $LOCAL $BASE $MERGED'
|
||||
|
||||
Write-Host " Setting vscode as the default code editor for diffs"
|
||||
git config --global diff.tool vscode
|
||||
git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'
|
||||
|
||||
# Verify Git version specifically
|
||||
if ($results.Git) {
|
||||
if (-not (Test-GitVersion)) {
|
||||
Write-Warning "Git is installed but version may be below 2.23"
|
||||
$results.Git = $false
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Write-Success "Git already installed and sufficient"
|
||||
}
|
||||
|
||||
# Install Visual Studio Code
|
||||
if (Should-Install -ToolName "VSCode" -UserChoice $userChoice -Prereqs $prereqs) {
|
||||
$currentStep++
|
||||
Write-ProgressIndicator -Activity "Installing Tools" -Status "Installing Visual Studio Code ($currentStep/$totalSteps)" -PercentComplete (($currentStep / $totalSteps) * 100)
|
||||
$results.VSCode = Install-Package `
|
||||
-Name "Visual Studio Code" `
|
||||
-WingetId "Microsoft.VisualStudioCode" `
|
||||
-CheckCommand "code"
|
||||
} else {
|
||||
Write-Success "Visual Studio Code already installed"
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region VSCode Extensions and Configuration
|
||||
|
||||
if (($prereqs.VSCode.Installed -or $results.VSCode) -and $userChoice -ne "CloneOnly") {
|
||||
$currentStep++
|
||||
Write-ProgressIndicator -Activity "Installing Tools" -Status "Installing VSCode Extensions ($currentStep/$totalSteps)" -PercentComplete (($currentStep / $totalSteps) * 100)
|
||||
|
||||
Write-Host ""
|
||||
Write-Step "Configuring VSCode"
|
||||
|
||||
# Install PowerShell extension
|
||||
$powershellExtensionResult = Install-VSCodeExtension -ExtensionId "ms-vscode.PowerShell" -ExtensionName "PowerShell"
|
||||
|
||||
# Configure PowerShell 7 integration (optional but recommended)
|
||||
if ($userChoice -eq "InstallTools") {
|
||||
$powershellIntegrationResult = Set-VSCodePowerShellIntegration
|
||||
$results.VSCodePowerShellIntegration = $powershellIntegrationResult
|
||||
} else {
|
||||
$results.VSCodePowerShellIntegration = $null
|
||||
}
|
||||
|
||||
$results.VSCodeExtensions = $powershellExtensionResult
|
||||
}
|
||||
|
||||
# Clear progress bar
|
||||
Write-Progress -Activity "Installing Tools" -Completed
|
||||
|
||||
#endregion
|
||||
|
||||
#region Optional Installations
|
||||
|
||||
# Windows Terminal (optional)
|
||||
if (Should-Install -ToolName "WindowsTerminal" -UserChoice $userChoice -Prereqs $prereqs) {
|
||||
Write-Host ""
|
||||
if (Get-UserConfirmation "Do you want to install Windows Terminal? (Highly recommended for better terminal experience)") {
|
||||
Write-ProgressIndicator -Activity "Installing Optional Tools" -Status "Installing Windows Terminal" -PercentComplete 50
|
||||
$results.WindowsTerminal = Install-Package `
|
||||
-Name "Windows Terminal" `
|
||||
-WingetId "Microsoft.WindowsTerminal" `
|
||||
-CheckCommand "wt"
|
||||
Write-Progress -Activity "Installing Optional Tools" -Completed
|
||||
} else {
|
||||
Write-Host " Skipping Windows Terminal installation." -ForegroundColor Gray
|
||||
$results.WindowsTerminal = $null
|
||||
}
|
||||
} elseif ($prereqs.WindowsTerminal.Installed) {
|
||||
Write-Success "Windows Terminal already installed"
|
||||
}
|
||||
|
||||
#endregion
|
||||
} else {
|
||||
Write-Host "`nSkipping tool installation as requested." -ForegroundColor Gray
|
||||
Write-Host "Proceeding directly to workshop repository setup..." -ForegroundColor Cyan
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Installation Summary
|
||||
|
||||
Write-Step "Installation Summary"
|
||||
|
||||
# For CloneOnly, check if tools were already installed
|
||||
if ($userChoice -eq "CloneOnly") {
|
||||
$allRequired = $prereqs.PowerShell.Installed -and $prereqs.PowerShell.Sufficient -and
|
||||
$prereqs.Git.Installed -and $prereqs.Git.Sufficient -and
|
||||
$prereqs.VSCode.Installed -and $prereqs.VSCode.Sufficient
|
||||
} else {
|
||||
$allRequired = $results.PowerShell -and $results.Git -and $results.VSCode
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "Required Tools:" -ForegroundColor White
|
||||
|
||||
if ($results.PowerShell) {
|
||||
Write-Success "PowerShell 7"
|
||||
}
|
||||
else {
|
||||
Write-Error "PowerShell 7 - Installation failed or needs restart"
|
||||
}
|
||||
|
||||
if ($results.Git) {
|
||||
Write-Success "Git 2.23+"
|
||||
}
|
||||
else {
|
||||
Write-Error "Git 2.23+ - Installation failed or needs restart"
|
||||
}
|
||||
|
||||
if ($results.VSCode) {
|
||||
Write-Success "Visual Studio Code"
|
||||
|
||||
if ($results.VSCodeExtensions) {
|
||||
Write-Success " • PowerShell extension"
|
||||
}
|
||||
else {
|
||||
Write-Warning " • VSCode PowerShell extension may need manual installation"
|
||||
}
|
||||
|
||||
if ($results.VSCodePowerShellIntegration -eq $true) {
|
||||
Write-Success " • PowerShell 7 terminal integration"
|
||||
}
|
||||
elseif ($results.VSCodePowerShellIntegration -eq $false) {
|
||||
Write-Host " • PowerShell 7 terminal integration: Skipped" -ForegroundColor Gray
|
||||
}
|
||||
}
|
||||
else {
|
||||
Write-Error "Visual Studio Code - Installation failed or needs restart"
|
||||
}
|
||||
|
||||
if ($results.WindowsTerminal -ne $null) {
|
||||
Write-Host ""
|
||||
Write-Host "Optional Tools:" -ForegroundColor White
|
||||
|
||||
if ($results.WindowsTerminal) {
|
||||
Write-Success "Windows Terminal"
|
||||
}
|
||||
else {
|
||||
Write-Error "Windows Terminal - Installation failed or needs restart"
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Next Steps
|
||||
|
||||
Write-Step "Next Steps"
|
||||
|
||||
if ($allRequired) {
|
||||
Write-Host ""
|
||||
Write-Success "All required tools installed successfully!"
|
||||
Write-Host ""
|
||||
|
||||
Write-Host "IMPORTANT: Configure Git before your first commit:" -ForegroundColor Yellow
|
||||
Write-Host ""
|
||||
Write-Host " git config --global user.name `"Your Name`"" -ForegroundColor White
|
||||
Write-Host " git config --global user.email `"your.email@example.com`"" -ForegroundColor White
|
||||
Write-Host ""
|
||||
|
||||
Write-Host "Optional: Set VS Code as Git's default editor:" -ForegroundColor Cyan
|
||||
Write-Host " git config --global core.editor `"code --wait`"" -ForegroundColor White
|
||||
Write-Host ""
|
||||
|
||||
Write-Host "Verify your installation:" -ForegroundColor Cyan
|
||||
Write-Host " pwsh --version" -ForegroundColor White
|
||||
Write-Host " git --version" -ForegroundColor White
|
||||
Write-Host " code --version" -ForegroundColor White
|
||||
Write-Host ""
|
||||
|
||||
Write-Host "Set PowerShell execution policy (if needed):" -ForegroundColor Cyan
|
||||
Write-Host " Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser" -ForegroundColor White
|
||||
Write-Host ""
|
||||
|
||||
Write-Host "Recommended VS Code Extensions:" -ForegroundColor Cyan
|
||||
Write-Host " • PowerShell - Better PowerShell support (from Microsoft)" -ForegroundColor White
|
||||
Write-Host ""
|
||||
Write-Host " Install via: Ctrl+Shift+X in VS Code" -ForegroundColor Gray
|
||||
Write-Host ""
|
||||
|
||||
Write-Host "You're ready to start the workshop!" -ForegroundColor Green
|
||||
Write-Host ""
|
||||
|
||||
# Ask user if they want to clone the workshop repository
|
||||
Write-Step "Workshop Setup"
|
||||
|
||||
if (Get-UserConfirmation "Clone the Git Workshop repository to Documents\git-workshop and open in VSCode?") {
|
||||
try {
|
||||
$documentsPath = [System.Environment]::GetFolderPath("MyDocuments")
|
||||
$workshopPath = Join-Path $documentsPath "git-workshop"
|
||||
|
||||
Write-Host " Cloning to: $workshopPath" -ForegroundColor Gray
|
||||
|
||||
# Create directory if it doesn't exist
|
||||
$documentsDir = Split-Path $workshopPath -Parent
|
||||
if (-not (Test-Path $documentsDir)) {
|
||||
New-Item -Path $documentsDir -ItemType Directory -Force | Out-Null
|
||||
}
|
||||
|
||||
# Clone or update the repository
|
||||
if (Test-Path $workshopPath) {
|
||||
Write-Host " Directory already exists. Checking if it's a git repository..." -ForegroundColor Yellow
|
||||
Push-Location $workshopPath
|
||||
if (Get-Command git -ErrorAction SilentlyContinue) {
|
||||
$remoteResult = git remote get-url origin 2>$null
|
||||
if ($LASTEXITCODE -eq 0 -and $remoteResult -like "*git-workshop*") {
|
||||
Write-Host " Repository already exists. Updating..." -ForegroundColor Cyan
|
||||
git pull origin main
|
||||
} else {
|
||||
Write-Warning " Directory exists but is not the git-workshop repository"
|
||||
Write-Host " Please remove the directory manually and run again" -ForegroundColor Yellow
|
||||
Pop-Location
|
||||
return
|
||||
}
|
||||
}
|
||||
Pop-Location
|
||||
} else {
|
||||
git clone "https://git.frod.dk/floppydiscen/git-workshop.git" $workshopPath
|
||||
Write-Success "Repository cloned successfully!"
|
||||
}
|
||||
|
||||
Write-Host " Opening in VSCode..." -ForegroundColor Cyan
|
||||
if (Get-Command code -ErrorAction SilentlyContinue) {
|
||||
& code $workshopPath
|
||||
Write-Success "VSCode opened with the workshop repository"
|
||||
Write-Host ""
|
||||
Write-Host "=== IMPORTANT: Terminal Instructions ===" -ForegroundColor Yellow
|
||||
Write-Host "Once VSCode opens, you MUST open the integrated terminal:" -ForegroundColor White
|
||||
Write-Host ""
|
||||
Write-Host "Option 1 (Recommended): Press Ctrl+` (backtick key)" -ForegroundColor Cyan
|
||||
Write-Host "Option 2: Use menu: View → Terminal" -ForegroundColor Cyan
|
||||
Write-Host "Option 3: Use Command Palette: Ctrl+Shift+P → 'Terminal: Create New Terminal'" -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
Write-Host "=== Quick Start (run in VSCode terminal) ===" -ForegroundColor Yellow
|
||||
Write-Host " cd 01-essentials\01-basics" -ForegroundColor White
|
||||
Write-Host " .\setup.ps1" -ForegroundColor White
|
||||
Write-Host ""
|
||||
Write-Host "The terminal should show 'PowerShell' and open to the correct directory." -ForegroundColor Green
|
||||
Write-Host ""
|
||||
} else {
|
||||
Write-Warning "VSCode command not found. Please open manually:"
|
||||
Write-Host " code '$workshopPath'" -ForegroundColor White
|
||||
Write-Host ""
|
||||
Write-Host "=== Manual Instructions ===" -ForegroundColor Yellow
|
||||
Write-Host "1. Open VSCode manually" -ForegroundColor White
|
||||
Write-Host "2. Open integrated terminal (Ctrl+` or View → Terminal)" -ForegroundColor White
|
||||
Write-Host "3. Navigate: cd 01-essentials\01-basics" -ForegroundColor White
|
||||
Write-Host "4. Run: .\setup.ps1" -ForegroundColor White
|
||||
Write-Host ""
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "Quick start commands (run in VSCode terminal):" -ForegroundColor Cyan
|
||||
Write-Host " cd 01-essentials\01-basics" -ForegroundColor White
|
||||
Write-Host " .\setup.ps1" -ForegroundColor White
|
||||
Write-Host ""
|
||||
|
||||
} catch {
|
||||
Write-Error "Failed to clone repository: $_"
|
||||
Write-Host ""
|
||||
Write-Host "You can clone manually:" -ForegroundColor Yellow
|
||||
Write-Host " git clone https://git.frod.dk/floppydiscen/git-workshop.git ~/Documents/git-workshop" -ForegroundColor White
|
||||
Write-Host " code ~/Documents/git-workshop" -ForegroundColor White
|
||||
}
|
||||
} else {
|
||||
Write-Host " Skipping repository clone." -ForegroundColor Gray
|
||||
Write-Host ""
|
||||
Write-Host "Manual setup:" -ForegroundColor Cyan
|
||||
Write-Host " cd path\to\git-workshop" -ForegroundColor White
|
||||
Write-Host " cd 01-essentials\01-basics" -ForegroundColor White
|
||||
Write-Host " .\setup.ps1" -ForegroundColor White
|
||||
Write-Host ""
|
||||
}
|
||||
}
|
||||
else {
|
||||
Write-Host ""
|
||||
Write-Warning "Some required installations failed or need verification."
|
||||
Write-Host ""
|
||||
Write-Host "Troubleshooting steps:" -ForegroundColor Yellow
|
||||
Write-Host " 1. Close and reopen your terminal (or restart your computer)" -ForegroundColor White
|
||||
Write-Host " 2. Run this script again: .\install.ps1" -ForegroundColor White
|
||||
Write-Host " 3. If issues persist, try manual installation:" -ForegroundColor White
|
||||
Write-Host " See INSTALLATION.md for detailed instructions" -ForegroundColor White
|
||||
Write-Host ""
|
||||
|
||||
if (-not $results.Git) {
|
||||
Write-Host "For Git issues:" -ForegroundColor Yellow
|
||||
Write-Host " • Restart terminal after installation (PATH needs to refresh)" -ForegroundColor White
|
||||
Write-Host " • Manual download: https://git-scm.com/downloads" -ForegroundColor White
|
||||
Write-Host ""
|
||||
}
|
||||
|
||||
if (-not $results.VSCode) {
|
||||
Write-Host "For VS Code issues:" -ForegroundColor Yellow
|
||||
Write-Host " • Ensure 'Add to PATH' option is enabled during installation" -ForegroundColor White
|
||||
Write-Host " • Manual download: https://code.visualstudio.com/" -ForegroundColor White
|
||||
Write-Host ""
|
||||
}
|
||||
|
||||
if (-not $results.PowerShell) {
|
||||
Write-Host "For PowerShell 7 issues:" -ForegroundColor Yellow
|
||||
Write-Host " • Manual download: https://github.com/PowerShell/PowerShell/releases/latest" -ForegroundColor White
|
||||
Write-Host " • Download the file ending in '-win-x64.msi'" -ForegroundColor White
|
||||
Write-Host ""
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,78 +0,0 @@
|
||||
# Module 01: Git Basics
|
||||
|
||||
## Learning Objectives
|
||||
|
||||
- Understand what a git repository is
|
||||
- Learn the basic git workflow: modify → stage → commit
|
||||
- Use `git status` to check repository state
|
||||
- Use `git add` to stage changes
|
||||
- Use `git commit` to save changes
|
||||
|
||||
## Challenge
|
||||
|
||||
In this challenge, you'll learn the fundamental git workflow.
|
||||
|
||||
### Setup
|
||||
|
||||
Run the setup script to prepare the challenge:
|
||||
|
||||
```powershell
|
||||
.\setup.ps1
|
||||
```
|
||||
|
||||
This will create a directory called `challenge` with some files that need to be committed.
|
||||
|
||||
### Your Task
|
||||
|
||||
Your goal is to commit both `welcome.txt` and `instructions.txt` to a git repository. Here's a suggested approach:
|
||||
|
||||
1. Navigate into the `challenge` directory: `cd challenge`
|
||||
2. **Initialize a new git repository**: `git init` (this is your first step!)
|
||||
3. Check the status of your repository: `git status`
|
||||
4. Stage the files you want to commit: `git add welcome.txt` (or `git add .` to stage all files)
|
||||
5. Create a commit: `git commit -m "Your commit message"`
|
||||
6. Verify both files are committed: `git ls-tree -r HEAD --name-only`
|
||||
|
||||
**Important Notes**:
|
||||
- The challenge directory is NOT a git repository until you run `git init`. This is intentional - you're learning to start from scratch!
|
||||
- You can commit both files together in one commit, or separately in multiple commits - it's up to you!
|
||||
- The verification script checks that both files are committed, not the specific commit messages or order
|
||||
|
||||
### Key Concepts
|
||||
|
||||
- **Repository**: A directory tracked by git, containing your project files and their history
|
||||
- **Working Directory**: The files you see and edit
|
||||
- **Staging Area (Index)**: A preparation area for your next commit
|
||||
- **Commit**: A snapshot of your staged changes
|
||||
|
||||
### Useful Commands
|
||||
|
||||
```bash
|
||||
git init # Initialize a new git repository
|
||||
git status # Show the working tree status
|
||||
git add <file> # Stage a specific file for commit
|
||||
git add . # Stage all files in current directory
|
||||
git commit -m "<message>" # Create a commit with a message
|
||||
git ls-tree -r HEAD --name-only # List all files in the latest commit
|
||||
git log # View commit history
|
||||
```
|
||||
|
||||
### Verification
|
||||
|
||||
Once you think you've completed the challenge, run:
|
||||
|
||||
```powershell
|
||||
.\verify.ps1
|
||||
```
|
||||
|
||||
This will check if you've successfully completed all the steps.
|
||||
|
||||
### Need to Start Over?
|
||||
|
||||
If you want to reset the challenge and start fresh, run:
|
||||
|
||||
```powershell
|
||||
.\reset.ps1
|
||||
```
|
||||
|
||||
This will remove your challenge directory and set up a new one.
|
||||
@@ -1,84 +0,0 @@
|
||||
# Module 02: Viewing History
|
||||
|
||||
## Learning Objectives
|
||||
|
||||
In this module, you will:
|
||||
- Understand commit history and how to navigate it
|
||||
- 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
|
||||
- Understand commit hashes and references
|
||||
|
||||
## Challenge
|
||||
|
||||
### Setup
|
||||
|
||||
Run the setup script to create your challenge environment:
|
||||
|
||||
```powershell
|
||||
.\setup.ps1
|
||||
```
|
||||
|
||||
This will create a `challenge/` directory with a Git repository that already has some commit history.
|
||||
|
||||
### 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.
|
||||
|
||||
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.
|
||||
|
||||
**Suggested Approach:**
|
||||
|
||||
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`
|
||||
|
||||
> **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
|
||||
|
||||
## Key Concepts
|
||||
|
||||
- **Commit Hash**: A unique identifier (SHA-1 hash) for each commit. You can use the full hash or just the first few characters.
|
||||
- **Commit Message**: A description of what changed in that commit, written by the author.
|
||||
- **Commit History**: The chronological record of all changes made to a repository.
|
||||
- **HEAD**: A pointer to the current commit you're working from.
|
||||
|
||||
## Useful Commands
|
||||
|
||||
```bash
|
||||
git log # View commit history
|
||||
git log --oneline # Compact one-line format
|
||||
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
|
||||
```
|
||||
|
||||
## Verification
|
||||
|
||||
Once you've filled in your answers in `answers.md`, verify your solution:
|
||||
|
||||
```powershell
|
||||
.\verify.ps1
|
||||
```
|
||||
|
||||
The verification script will check that your answers contain the expected information.
|
||||
|
||||
## Need to Start Over?
|
||||
|
||||
If you want to reset the challenge and start fresh:
|
||||
|
||||
```powershell
|
||||
.\reset.ps1
|
||||
```
|
||||
|
||||
This will remove the challenge directory and run the setup script again, giving you a clean slate.
|
||||
@@ -1,94 +0,0 @@
|
||||
# Module 03: Branching Basics
|
||||
|
||||
## Learning Objectives
|
||||
|
||||
In this module, you will:
|
||||
- Understand what a branch is in Git
|
||||
- Create new branches using `git branch` or `git checkout -b`
|
||||
- Switch between branches using `git checkout` or `git switch`
|
||||
- View all branches with `git branch`
|
||||
- Understand that branches allow parallel development
|
||||
|
||||
## Challenge
|
||||
|
||||
### Setup
|
||||
|
||||
Run the setup script to create your challenge environment:
|
||||
|
||||
```powershell
|
||||
.\setup.ps1
|
||||
```
|
||||
|
||||
This will create a `challenge/` directory with a Git repository that has some initial commits on the main branch.
|
||||
|
||||
### Your Task
|
||||
|
||||
Your goal is to create a feature branch, make commits on it, and understand how branches work independently.
|
||||
|
||||
**Steps:**
|
||||
|
||||
1. Create a new branch called `feature-login`
|
||||
2. Switch to the new branch
|
||||
3. Create a new file `login.py` with some login functionality
|
||||
4. Commit the new file to your feature branch
|
||||
5. Make another change to `login.py` and commit it
|
||||
6. Switch back to the main branch and observe that `login.py` doesn't exist there
|
||||
|
||||
**Suggested Approach:**
|
||||
|
||||
1. Navigate to the challenge directory: `cd challenge`
|
||||
2. View existing branches: `git branch`
|
||||
3. Create and switch to new branch: `git checkout -b feature-login`
|
||||
4. Create `login.py` with any content you like
|
||||
5. Stage and commit: `git add login.py` and `git commit -m "Add login functionality"`
|
||||
6. Modify `login.py`, then commit again
|
||||
7. Switch back to main: `git checkout main`
|
||||
8. Run `ls` and notice that `login.py` doesn't exist on main!
|
||||
9. Switch back to feature-login: `git checkout feature-login`
|
||||
10. Run `ls` again and see that `login.py` is back!
|
||||
|
||||
> **Important Notes:**
|
||||
> - You can use either `git checkout` or `git switch` to change branches
|
||||
> - `git checkout -b <name>` creates and switches in one command
|
||||
> - `git switch -c <name>` is the newer equivalent
|
||||
> - Branches are independent - files in one branch don't affect another until you merge
|
||||
> - You can switch between branches as many times as you want
|
||||
|
||||
## Key Concepts
|
||||
|
||||
- **Branch**: A lightweight movable pointer to a commit. Branches allow you to work on different features independently.
|
||||
- **HEAD**: A pointer to the current branch you're working on. When you switch branches, HEAD moves.
|
||||
- **main/master**: The default branch name in Git (main is the modern convention, master is older).
|
||||
- **Feature Branch**: A branch created to develop a specific feature, separate from the main codebase.
|
||||
|
||||
## Useful Commands
|
||||
|
||||
```bash
|
||||
git branch # List all branches (* shows current)
|
||||
git branch <name> # Create a new branch
|
||||
git checkout <branch> # Switch to an existing branch
|
||||
git checkout -b <name> # Create and switch to new branch
|
||||
git switch <branch> # Switch to a branch (newer syntax)
|
||||
git switch -c <name> # Create and switch (newer syntax)
|
||||
git branch -d <name> # Delete a branch (we won't use this yet)
|
||||
```
|
||||
|
||||
## Verification
|
||||
|
||||
Once you've completed the challenge, verify your solution:
|
||||
|
||||
```powershell
|
||||
.\verify.ps1
|
||||
```
|
||||
|
||||
The verification script will check that you've created the branch, made commits, and that the branches are independent.
|
||||
|
||||
## Need to Start Over?
|
||||
|
||||
If you want to reset the challenge and start fresh:
|
||||
|
||||
```powershell
|
||||
.\reset.ps1
|
||||
```
|
||||
|
||||
This will remove the challenge directory and run the setup script again, giving you a clean slate.
|
||||
@@ -1,26 +0,0 @@
|
||||
#!/usr/bin/env pwsh
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Resets the Module 03 challenge environment.
|
||||
|
||||
.DESCRIPTION
|
||||
This script removes the existing challenge directory and runs
|
||||
the setup script again to create a fresh challenge environment.
|
||||
#>
|
||||
|
||||
Write-Host "`n=== Resetting Module 03 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"
|
||||
} else {
|
||||
Write-Host "No existing challenge directory found." -ForegroundColor Cyan
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "----------------------------------------" -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
|
||||
# Run setup script
|
||||
& "$PSScriptRoot\setup.ps1"
|
||||
@@ -1,85 +0,0 @@
|
||||
#!/usr/bin/env pwsh
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Sets up the Module 03 challenge environment for learning about branches.
|
||||
|
||||
.DESCRIPTION
|
||||
This script creates a challenge directory with a Git repository that
|
||||
contains a couple of commits on the main branch. Students will create
|
||||
a feature branch and make commits on it.
|
||||
#>
|
||||
|
||||
Write-Host "`n=== Setting up Module 03 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"
|
||||
|
||||
# Commit 1: Initial commit
|
||||
Write-Host "Creating initial project..." -ForegroundColor Green
|
||||
$mainContent = @"
|
||||
# main.py - Main application file
|
||||
|
||||
def main():
|
||||
print("Welcome to the Application!")
|
||||
print("This is the main branch")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
"@
|
||||
Set-Content -Path "main.py" -Value $mainContent
|
||||
|
||||
git add .
|
||||
git commit -m "Initial commit" | Out-Null
|
||||
|
||||
# Commit 2: Add main functionality
|
||||
Write-Host "Adding main functionality..." -ForegroundColor Green
|
||||
$mainContent = @"
|
||||
# main.py - Main application file
|
||||
|
||||
def main():
|
||||
print("Welcome to the Application!")
|
||||
print("This is the main branch")
|
||||
run_application()
|
||||
|
||||
def run_application():
|
||||
print("Application is running...")
|
||||
print("Ready for new features!")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
"@
|
||||
Set-Content -Path "main.py" -Value $mainContent
|
||||
|
||||
git add .
|
||||
git commit -m "Add main functionality" | Out-Null
|
||||
|
||||
# Return to module directory
|
||||
Set-Location ..
|
||||
|
||||
Write-Host "`n=== Setup Complete! ===" -ForegroundColor Green
|
||||
Write-Host "`nYour challenge environment is ready in the 'challenge/' directory." -ForegroundColor Cyan
|
||||
Write-Host "`nNext steps:" -ForegroundColor Cyan
|
||||
Write-Host " 1. cd challenge" -ForegroundColor White
|
||||
Write-Host " 2. Create a new branch: git checkout -b feature-login" -ForegroundColor White
|
||||
Write-Host " 3. Create login.py and commit it" -ForegroundColor White
|
||||
Write-Host " 4. Make another commit on the feature branch" -ForegroundColor White
|
||||
Write-Host " 5. Switch back to main: git checkout main" -ForegroundColor White
|
||||
Write-Host " 6. Observe that login.py doesn't exist on main!" -ForegroundColor White
|
||||
Write-Host " 7. Run '..\verify.ps1' to check your solution" -ForegroundColor White
|
||||
Write-Host ""
|
||||
@@ -1,105 +0,0 @@
|
||||
#!/usr/bin/env pwsh
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Verifies the Module 03 challenge solution.
|
||||
|
||||
.DESCRIPTION
|
||||
This script checks that:
|
||||
- The challenge directory exists
|
||||
- A Git repository exists
|
||||
- The feature-login branch exists
|
||||
- The branch has at least 2 new commits
|
||||
- login.py exists in the feature branch but not in main
|
||||
#>
|
||||
|
||||
Write-Host "`n=== Verifying Module 03 Solution ===" -ForegroundColor Cyan
|
||||
|
||||
$allChecksPassed = $true
|
||||
|
||||
# Check if challenge directory exists
|
||||
if (-not (Test-Path "challenge")) {
|
||||
Write-Host "[FAIL] Challenge directory not found. Did you run setup.ps1?" -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
Set-Location "challenge"
|
||||
|
||||
# Check if git repository exists
|
||||
if (-not (Test-Path ".git")) {
|
||||
Write-Host "[FAIL] Not a git repository. Did you run setup.ps1?" -ForegroundColor Red
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Save current branch
|
||||
$originalBranch = git branch --show-current 2>$null
|
||||
|
||||
# Check if feature-login branch exists
|
||||
$branchExists = git branch --list "feature-login" 2>$null
|
||||
if ($branchExists) {
|
||||
Write-Host "[PASS] Branch 'feature-login' exists" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "[FAIL] Branch 'feature-login' not found" -ForegroundColor Red
|
||||
Write-Host "[HINT] Create the branch with: git checkout -b feature-login" -ForegroundColor Yellow
|
||||
$allChecksPassed = $false
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check if feature-login has commits beyond main
|
||||
$commitCount = git rev-list main..feature-login --count 2>$null
|
||||
if ($commitCount -ge 2) {
|
||||
Write-Host "[PASS] Branch 'feature-login' has $commitCount new commits" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "[FAIL] Branch 'feature-login' needs at least 2 new commits (found: $commitCount)" -ForegroundColor Red
|
||||
Write-Host "[HINT] Make sure you've committed login.py and made at least one more commit" -ForegroundColor Yellow
|
||||
$allChecksPassed = $false
|
||||
}
|
||||
|
||||
# Switch to feature-login and check for login.py
|
||||
git checkout feature-login 2>$null | Out-Null
|
||||
if (Test-Path "login.py") {
|
||||
Write-Host "[PASS] File 'login.py' exists in feature-login branch" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "[FAIL] File 'login.py' not found in feature-login branch" -ForegroundColor Red
|
||||
Write-Host "[HINT] Create login.py and commit it to the feature-login branch" -ForegroundColor Yellow
|
||||
$allChecksPassed = $false
|
||||
}
|
||||
|
||||
# Switch to main and verify login.py doesn't exist
|
||||
git checkout main 2>$null | Out-Null
|
||||
if (-not (Test-Path "login.py")) {
|
||||
Write-Host "[PASS] File 'login.py' does NOT exist in main branch (branches are independent!)" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "[FAIL] File 'login.py' should not exist in main branch" -ForegroundColor Red
|
||||
Write-Host "[HINT] Make sure you created login.py only on the feature-login branch" -ForegroundColor Yellow
|
||||
$allChecksPassed = $false
|
||||
}
|
||||
|
||||
# Switch back to original branch
|
||||
if ($originalBranch) {
|
||||
git checkout $originalBranch 2>$null | Out-Null
|
||||
}
|
||||
|
||||
Set-Location ..
|
||||
|
||||
# Final summary
|
||||
if ($allChecksPassed) {
|
||||
Write-Host "`n" -NoNewline
|
||||
Write-Host "=====================================" -ForegroundColor Green
|
||||
Write-Host " CONGRATULATIONS! CHALLENGE PASSED!" -ForegroundColor Green
|
||||
Write-Host "=====================================" -ForegroundColor Green
|
||||
Write-Host "`nYou've successfully learned about Git branches!" -ForegroundColor Cyan
|
||||
Write-Host "You now understand:" -ForegroundColor Cyan
|
||||
Write-Host " - How to create branches with git checkout -b" -ForegroundColor White
|
||||
Write-Host " - How to switch between branches" -ForegroundColor White
|
||||
Write-Host " - That branches are independent lines of development" -ForegroundColor White
|
||||
Write-Host " - That files in one branch don't affect another" -ForegroundColor White
|
||||
Write-Host "`nReady for the next module!" -ForegroundColor Green
|
||||
Write-Host ""
|
||||
} else {
|
||||
Write-Host "`n[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
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
# Module 04: Merging
|
||||
|
||||
## Learning Objectives
|
||||
|
||||
In this module, you will:
|
||||
- Understand what merging means in Git
|
||||
- Perform a fast-forward merge
|
||||
- Perform a three-way merge
|
||||
- Understand when merge commits are created
|
||||
- Use `git merge` to combine branches
|
||||
|
||||
## Challenge
|
||||
|
||||
### Setup
|
||||
|
||||
Run the setup script to create your challenge environment:
|
||||
|
||||
```powershell
|
||||
.\setup.ps1
|
||||
```
|
||||
|
||||
This will create a `challenge/` directory with a Git repository that has a main branch and a feature branch ready to merge.
|
||||
|
||||
### Your Task
|
||||
|
||||
This challenge has two parts that teach you about the two types of merges in Git:
|
||||
|
||||
**Part 1: Fast-Forward Merge**
|
||||
1. Merge the existing `feature-api` branch into main
|
||||
2. Observe that this is a "fast-forward" merge (no merge commit created)
|
||||
|
||||
**Part 2: Three-Way Merge**
|
||||
3. Create a new branch called `feature-ui`
|
||||
4. Make commits on the feature-ui branch
|
||||
5. Switch back to main and make a commit there too (creates divergence)
|
||||
6. Merge feature-ui into main
|
||||
7. Observe that this creates a merge commit (three-way merge)
|
||||
|
||||
**Suggested Approach:**
|
||||
|
||||
1. Navigate to the challenge directory: `cd challenge`
|
||||
2. Check current branch: `git branch` (should be on main)
|
||||
3. View existing branches: `git branch -a`
|
||||
4. Merge feature-api: `git merge feature-api`
|
||||
5. View the log: `git log --oneline --graph`
|
||||
6. Create feature-ui branch: `git checkout -b feature-ui`
|
||||
7. Create a new file `ui.py` and commit it
|
||||
8. Make another commit on feature-ui (modify ui.py)
|
||||
9. Switch back to main: `git checkout main`
|
||||
10. Make a change on main (modify api.py) and commit it
|
||||
11. Merge feature-ui: `git merge feature-ui`
|
||||
12. View the merge history: `git log --oneline --graph --all`
|
||||
|
||||
> **Important Notes:**
|
||||
> - A **fast-forward merge** happens when main hasn't changed since the feature branch was created
|
||||
> - A **three-way merge** creates a merge commit when both branches have diverged
|
||||
> - You can see merge commits with `git log --merges`
|
||||
> - The `--graph` option helps visualize the branch history
|
||||
> - After merging, the feature branch still exists but you can delete it with `git branch -d`
|
||||
|
||||
## Key Concepts
|
||||
|
||||
- **Merge**: Combining changes from different branches into one branch.
|
||||
- **Fast-Forward Merge**: When the target branch hasn't changed, Git simply moves the branch pointer forward. No merge commit is created.
|
||||
- **Three-Way Merge**: When both branches have new commits, Git creates a merge commit that has two parent commits.
|
||||
- **Merge Commit**: A special commit with two (or more) parent commits, representing the point where branches were merged.
|
||||
- **Divergent Branches**: Branches that have different commits since they split from a common ancestor.
|
||||
|
||||
## Useful Commands
|
||||
|
||||
```bash
|
||||
git merge <branch> # Merge branch into current branch
|
||||
git log --oneline --graph # View merge history visually
|
||||
git log --graph --all # View all branches and merges
|
||||
git log --merges # Show only merge commits
|
||||
git branch -d <branch> # Delete a merged branch (optional)
|
||||
```
|
||||
|
||||
## Verification
|
||||
|
||||
Once you've completed both merges, verify your solution:
|
||||
|
||||
```powershell
|
||||
.\verify.ps1
|
||||
```
|
||||
|
||||
The verification script will check that you've successfully merged both feature branches and understand the different merge types.
|
||||
|
||||
## Need to Start Over?
|
||||
|
||||
If you want to reset the challenge and start fresh:
|
||||
|
||||
```powershell
|
||||
.\reset.ps1
|
||||
```
|
||||
|
||||
This will remove the challenge directory and run the setup script again, giving you a clean slate.
|
||||
@@ -1,26 +0,0 @@
|
||||
#!/usr/bin/env pwsh
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Resets the Module 04 challenge environment.
|
||||
|
||||
.DESCRIPTION
|
||||
This script removes the existing challenge directory and runs
|
||||
the setup script again to create a fresh challenge environment.
|
||||
#>
|
||||
|
||||
Write-Host "`n=== Resetting Module 04 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"
|
||||
} else {
|
||||
Write-Host "No existing challenge directory found." -ForegroundColor Cyan
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "----------------------------------------" -ForegroundColor Cyan
|
||||
Write-Host ""
|
||||
|
||||
# Run setup script
|
||||
& "$PSScriptRoot\setup.ps1"
|
||||
@@ -1,128 +0,0 @@
|
||||
#!/usr/bin/env pwsh
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Sets up the Module 04 challenge environment for learning about merging.
|
||||
|
||||
.DESCRIPTION
|
||||
This script creates a challenge directory with a Git repository that
|
||||
contains a main branch and a feature branch ready to merge. Students
|
||||
will practice both fast-forward and three-way merges.
|
||||
#>
|
||||
|
||||
Write-Host "`n=== Setting up Module 04 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"
|
||||
|
||||
# Commit 1: Initial API file on main
|
||||
Write-Host "Creating initial API structure on main..." -ForegroundColor Green
|
||||
$apiContent = @"
|
||||
# api.py - API module
|
||||
|
||||
def api_handler():
|
||||
print("API Handler initialized")
|
||||
return True
|
||||
"@
|
||||
Set-Content -Path "api.py" -Value $apiContent
|
||||
|
||||
git add .
|
||||
git commit -m "Initial API setup" | Out-Null
|
||||
|
||||
# Commit 2: Add more API functionality on main
|
||||
$apiContent = @"
|
||||
# api.py - API module
|
||||
|
||||
def api_handler():
|
||||
print("API Handler initialized")
|
||||
return True
|
||||
|
||||
def get_data():
|
||||
print("Fetching data from API...")
|
||||
return {"status": "ok"}
|
||||
"@
|
||||
Set-Content -Path "api.py" -Value $apiContent
|
||||
|
||||
git add .
|
||||
git commit -m "Add get_data function" | Out-Null
|
||||
|
||||
# Create feature-api branch and add commits
|
||||
Write-Host "Creating feature-api branch..." -ForegroundColor Green
|
||||
git checkout -b feature-api 2>$null | Out-Null
|
||||
|
||||
# Commit on feature-api: Add API routes
|
||||
$routesContent = @"
|
||||
# api-routes.py - API Routes module
|
||||
|
||||
def setup_routes():
|
||||
print("Setting up API routes...")
|
||||
routes = {
|
||||
"/api/data": "get_data",
|
||||
"/api/status": "get_status"
|
||||
}
|
||||
return routes
|
||||
"@
|
||||
Set-Content -Path "api-routes.py" -Value $routesContent
|
||||
|
||||
git add .
|
||||
git commit -m "Add API routes" | Out-Null
|
||||
|
||||
# Second commit on feature-api: Enhance routes
|
||||
$routesContent = @"
|
||||
# api-routes.py - API Routes module
|
||||
|
||||
def setup_routes():
|
||||
print("Setting up API routes...")
|
||||
routes = {
|
||||
"/api/data": "get_data",
|
||||
"/api/status": "get_status",
|
||||
"/api/health": "health_check"
|
||||
}
|
||||
return routes
|
||||
|
||||
def health_check():
|
||||
return {"healthy": True}
|
||||
"@
|
||||
Set-Content -Path "api-routes.py" -Value $routesContent
|
||||
|
||||
git add .
|
||||
git commit -m "Add health check route" | Out-Null
|
||||
|
||||
# Switch back to main branch
|
||||
Write-Host "Switching back to main branch..." -ForegroundColor Green
|
||||
git checkout main 2>$null | Out-Null
|
||||
|
||||
# Return to module directory
|
||||
Set-Location ..
|
||||
|
||||
Write-Host "`n=== Setup Complete! ===" -ForegroundColor Green
|
||||
Write-Host "`nYour challenge environment is ready in the 'challenge/' directory." -ForegroundColor Cyan
|
||||
Write-Host "`nYou have:" -ForegroundColor Cyan
|
||||
Write-Host " - A main branch with API code" -ForegroundColor White
|
||||
Write-Host " - A feature-api branch with API routes ready to merge" -ForegroundColor White
|
||||
Write-Host "`nNext steps:" -ForegroundColor Cyan
|
||||
Write-Host " 1. cd challenge" -ForegroundColor White
|
||||
Write-Host " 2. View branches: git branch -a" -ForegroundColor White
|
||||
Write-Host " 3. Merge feature-api: git merge feature-api (fast-forward merge)" -ForegroundColor White
|
||||
Write-Host " 4. Create feature-ui branch: git checkout -b feature-ui" -ForegroundColor White
|
||||
Write-Host " 5. Make commits on feature-ui" -ForegroundColor White
|
||||
Write-Host " 6. Switch back to main and make a commit there" -ForegroundColor White
|
||||
Write-Host " 7. Merge feature-ui: git merge feature-ui (three-way merge)" -ForegroundColor White
|
||||
Write-Host " 8. View merge history: git log --oneline --graph --all" -ForegroundColor White
|
||||
Write-Host " 9. Run '..\verify.ps1' to check your solution" -ForegroundColor White
|
||||
Write-Host ""
|
||||
@@ -1,140 +0,0 @@
|
||||
#!/usr/bin/env pwsh
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Verifies the Module 04 challenge solution.
|
||||
|
||||
.DESCRIPTION
|
||||
This script checks that:
|
||||
- The challenge directory exists
|
||||
- A Git repository exists
|
||||
- Currently on main branch
|
||||
- feature-api has been merged into main
|
||||
- feature-ui branch exists and has been merged
|
||||
- A merge commit exists (from three-way merge)
|
||||
#>
|
||||
|
||||
Write-Host "`n=== Verifying Module 04 Solution ===" -ForegroundColor Cyan
|
||||
|
||||
$allChecksPassed = $true
|
||||
|
||||
# Check if challenge directory exists
|
||||
if (-not (Test-Path "challenge")) {
|
||||
Write-Host "[FAIL] Challenge directory not found. Did you run setup.ps1?" -ForegroundColor Red
|
||||
exit 1
|
||||
}
|
||||
|
||||
Set-Location "challenge"
|
||||
|
||||
# Check if git repository exists
|
||||
if (-not (Test-Path ".git")) {
|
||||
Write-Host "[FAIL] Not a git repository. Did you run setup.ps1?" -ForegroundColor Red
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check current branch is main
|
||||
$currentBranch = git branch --show-current 2>$null
|
||||
if ($currentBranch -eq "main") {
|
||||
Write-Host "[PASS] Currently on main branch" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "[FAIL] Not on main branch (currently on: $currentBranch)" -ForegroundColor Red
|
||||
Write-Host "[HINT] Switch to main with: git checkout main" -ForegroundColor Yellow
|
||||
$allChecksPassed = $false
|
||||
}
|
||||
|
||||
# Check if feature-api branch exists
|
||||
$featureApiBranch = git branch --list "feature-api" 2>$null
|
||||
if ($featureApiBranch) {
|
||||
Write-Host "[PASS] Branch 'feature-api' exists" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "[INFO] Branch 'feature-api' not found (may have been deleted after merge)" -ForegroundColor Cyan
|
||||
}
|
||||
|
||||
# Check if commits from feature-api are in main
|
||||
$apiRoutesCommit = git log --all --grep="Add API routes" --oneline 2>$null
|
||||
$healthCheckCommit = git log --all --grep="Add health check route" --oneline 2>$null
|
||||
|
||||
if ($apiRoutesCommit -and $healthCheckCommit) {
|
||||
# Check if these commits are in main's history
|
||||
$apiRoutesInMain = git log --grep="Add API routes" --oneline 2>$null
|
||||
$healthCheckInMain = git log --grep="Add health check route" --oneline 2>$null
|
||||
|
||||
if ($apiRoutesInMain -and $healthCheckInMain) {
|
||||
Write-Host "[PASS] Commits from feature-api are merged into main" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "[FAIL] Commits from feature-api not found in main" -ForegroundColor Red
|
||||
Write-Host "[HINT] Merge feature-api with: git merge feature-api" -ForegroundColor Yellow
|
||||
$allChecksPassed = $false
|
||||
}
|
||||
} else {
|
||||
Write-Host "[FAIL] Expected commits from feature-api not found" -ForegroundColor Red
|
||||
Write-Host "[HINT] Did you run setup.ps1?" -ForegroundColor Yellow
|
||||
$allChecksPassed = $false
|
||||
}
|
||||
|
||||
# Check if api-routes.py exists (should be on main after merge)
|
||||
if (Test-Path "api-routes.py") {
|
||||
Write-Host "[PASS] File 'api-routes.py' exists on main (from feature-api merge)" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "[FAIL] File 'api-routes.py' not found on main" -ForegroundColor Red
|
||||
Write-Host "[HINT] This file should appear after merging feature-api" -ForegroundColor Yellow
|
||||
$allChecksPassed = $false
|
||||
}
|
||||
|
||||
# Check if feature-ui branch exists
|
||||
$featureUiBranch = git branch --list "feature-ui" 2>$null
|
||||
if ($featureUiBranch) {
|
||||
Write-Host "[PASS] Branch 'feature-ui' exists" -ForegroundColor Green
|
||||
|
||||
# Check if feature-ui has commits
|
||||
$uiCommitCount = git rev-list main..feature-ui --count 2>$null
|
||||
if ($uiCommitCount -gt 0) {
|
||||
Write-Host "[INFO] Branch 'feature-ui' has $uiCommitCount commit(s)" -ForegroundColor Cyan
|
||||
}
|
||||
} else {
|
||||
Write-Host "[FAIL] Branch 'feature-ui' not found" -ForegroundColor Red
|
||||
Write-Host "[HINT] Create feature-ui with: git checkout -b feature-ui" -ForegroundColor Yellow
|
||||
$allChecksPassed = $false
|
||||
}
|
||||
|
||||
# Check for merge commits (indicates three-way merge happened)
|
||||
$mergeCommits = git log --merges --oneline 2>$null
|
||||
if ($mergeCommits) {
|
||||
Write-Host "[PASS] Merge commit(s) found (three-way merge completed)" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "[FAIL] No merge commits found" -ForegroundColor Red
|
||||
Write-Host "[HINT] Create divergence: make commits on both main and feature-ui, then merge" -ForegroundColor Yellow
|
||||
$allChecksPassed = $false
|
||||
}
|
||||
|
||||
# Check if ui.py exists (should be on main after merge)
|
||||
if (Test-Path "ui.py") {
|
||||
Write-Host "[PASS] File 'ui.py' exists on main (from feature-ui merge)" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "[FAIL] File 'ui.py' not found on main" -ForegroundColor Red
|
||||
Write-Host "[HINT] Create ui.py on feature-ui branch and merge it into main" -ForegroundColor Yellow
|
||||
$allChecksPassed = $false
|
||||
}
|
||||
|
||||
Set-Location ..
|
||||
|
||||
# Final summary
|
||||
if ($allChecksPassed) {
|
||||
Write-Host "`n" -NoNewline
|
||||
Write-Host "=====================================" -ForegroundColor Green
|
||||
Write-Host " CONGRATULATIONS! CHALLENGE PASSED!" -ForegroundColor Green
|
||||
Write-Host "=====================================" -ForegroundColor Green
|
||||
Write-Host "`nYou've successfully learned about Git merging!" -ForegroundColor Cyan
|
||||
Write-Host "You now understand:" -ForegroundColor Cyan
|
||||
Write-Host " - Fast-forward merges (when main hasn't changed)" -ForegroundColor White
|
||||
Write-Host " - Three-way merges (when branches have diverged)" -ForegroundColor White
|
||||
Write-Host " - How to use git merge to combine branches" -ForegroundColor White
|
||||
Write-Host " - Merge commits and how to view them" -ForegroundColor White
|
||||
Write-Host "`nReady for the next module!" -ForegroundColor Green
|
||||
Write-Host ""
|
||||
} else {
|
||||
Write-Host "`n[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
|
||||
}
|
||||
@@ -1,106 +0,0 @@
|
||||
# Module 06: Merge Conflicts
|
||||
|
||||
## Learning Objectives
|
||||
|
||||
By the end of this module, you will:
|
||||
- Understand what merge conflicts are and why they occur
|
||||
- Identify merge conflicts in your repository
|
||||
- Read and interpret conflict markers
|
||||
- Resolve merge conflicts manually
|
||||
- Complete a merge after resolving conflicts
|
||||
|
||||
## Challenge Description
|
||||
|
||||
You have a repository with a main branch and a feature branch called `update-config`. Both branches have modified the same configuration file in different ways, creating a merge conflict.
|
||||
|
||||
Your task is to:
|
||||
1. Attempt to merge the `update-config` branch into `main`
|
||||
2. Identify and understand the merge conflict
|
||||
3. Resolve the conflict by keeping both the timeout setting from `main` AND the debug setting from `update-config`
|
||||
4. Complete the merge with an appropriate commit message
|
||||
|
||||
## Key Concepts
|
||||
|
||||
### What Are Merge Conflicts?
|
||||
|
||||
A merge conflict occurs when Git cannot automatically combine changes from two branches because they modify the same part of the same file in different ways. When this happens, Git pauses the merge and asks you to manually resolve the conflict.
|
||||
|
||||
### Conflict Markers
|
||||
|
||||
When a conflict occurs, Git adds special markers to the affected files:
|
||||
|
||||
```
|
||||
<<<<<<< HEAD
|
||||
Your current branch's changes
|
||||
=======
|
||||
The incoming branch's changes
|
||||
>>>>>>> branch-name
|
||||
```
|
||||
|
||||
- `<<<<<<< HEAD`: Marks the beginning of your current branch's version
|
||||
- `=======`: Separates the two versions
|
||||
- `>>>>>>> branch-name`: Marks the end of the incoming branch's version
|
||||
|
||||
### Resolving Conflicts
|
||||
|
||||
To resolve a conflict:
|
||||
1. Open the conflicted file in your editor
|
||||
2. Look for the conflict markers
|
||||
3. Decide which changes to keep (you can keep one side, the other, or combine both)
|
||||
4. Remove the conflict markers
|
||||
5. Stage the resolved file with `git add`
|
||||
6. Complete the merge with `git commit`
|
||||
|
||||
## Useful Commands
|
||||
|
||||
```bash
|
||||
# Merge a branch (may result in conflicts)
|
||||
git merge <branch-name>
|
||||
|
||||
# Check the status during a conflict
|
||||
git status
|
||||
|
||||
# See which files have conflicts
|
||||
git diff --name-only --diff-filter=U
|
||||
|
||||
# View the conflict in detail
|
||||
git diff
|
||||
|
||||
# After resolving, stage the file
|
||||
git add <file>
|
||||
|
||||
# Complete the merge
|
||||
git commit
|
||||
|
||||
# If you want to abort the merge
|
||||
git merge --abort
|
||||
|
||||
# Visualize the branch history
|
||||
git log --oneline --graph --all
|
||||
```
|
||||
|
||||
## Verification
|
||||
|
||||
Run the verification script to check your solution:
|
||||
|
||||
```bash
|
||||
.\verify.ps1
|
||||
```
|
||||
|
||||
The verification will check that:
|
||||
- The merge conflict was resolved
|
||||
- The merge was completed successfully
|
||||
- Both configuration settings were preserved correctly
|
||||
- The commit history shows the merge
|
||||
|
||||
## Tips
|
||||
|
||||
- Don't panic when you see a conflict - it's a normal part of working with Git
|
||||
- Read the conflict markers carefully to understand both versions
|
||||
- You can use `git status` to see which files have conflicts
|
||||
- Always test your code after resolving conflicts to ensure it still works
|
||||
- The conflict markers (`<<<<<<<`, `=======`, `>>>>>>>`) must be completely removed from the final file
|
||||
|
||||
## What You'll Learn
|
||||
|
||||
Merge conflicts are inevitable when working on a team or even when working alone across multiple branches. Learning to resolve them confidently is an essential Git skill. This module teaches you the fundamentals of conflict resolution that you'll use throughout your development career.
|
||||
@@ -1,22 +0,0 @@
|
||||
#!/usr/bin/env pwsh
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Resets the merge conflicts 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"
|
||||
@@ -1,97 +0,0 @@
|
||||
#!/usr/bin/env pwsh
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Sets up the merge conflicts challenge environment.
|
||||
|
||||
.DESCRIPTION
|
||||
Creates a Git repository with a merge conflict scenario involving
|
||||
a configuration file that has been modified differently on two branches.
|
||||
#>
|
||||
|
||||
# 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 config.json file
|
||||
$initialConfig = @"
|
||||
{
|
||||
"app": {
|
||||
"name": "MyApp",
|
||||
"version": "1.0.0",
|
||||
"port": 3000
|
||||
}
|
||||
}
|
||||
"@
|
||||
|
||||
Set-Content -Path "config.json" -Value $initialConfig
|
||||
git add config.json
|
||||
git commit -m "Initial configuration" | Out-Null
|
||||
|
||||
# Create feature branch
|
||||
git branch update-config | Out-Null
|
||||
|
||||
# On main branch: Add timeout setting
|
||||
$mainConfig = @"
|
||||
{
|
||||
"app": {
|
||||
"name": "MyApp",
|
||||
"version": "1.0.0",
|
||||
"port": 3000,
|
||||
"timeout": 5000
|
||||
}
|
||||
}
|
||||
"@
|
||||
|
||||
Set-Content -Path "config.json" -Value $mainConfig
|
||||
git add config.json
|
||||
git commit -m "Add timeout configuration" | Out-Null
|
||||
|
||||
# Switch to feature branch: Add debug setting (conflicting change)
|
||||
git checkout update-config | Out-Null
|
||||
|
||||
$featureConfig = @"
|
||||
{
|
||||
"app": {
|
||||
"name": "MyApp",
|
||||
"version": "1.0.0",
|
||||
"port": 3000,
|
||||
"debug": true
|
||||
}
|
||||
}
|
||||
"@
|
||||
|
||||
Set-Content -Path "config.json" -Value $featureConfig
|
||||
git add config.json
|
||||
git commit -m "Add debug mode configuration" | Out-Null
|
||||
|
||||
# Switch back to main branch
|
||||
git checkout main | 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 are now on the 'main' branch." -ForegroundColor Cyan
|
||||
Write-Host "There is a branch called 'update-config' with conflicting changes." -ForegroundColor Cyan
|
||||
Write-Host "`nYour task:" -ForegroundColor Yellow
|
||||
Write-Host "1. Navigate to the challenge directory: cd challenge" -ForegroundColor White
|
||||
Write-Host "2. Try to merge the 'update-config' branch into 'main'" -ForegroundColor White
|
||||
Write-Host "3. Resolve the merge conflict in config.json" -ForegroundColor White
|
||||
Write-Host "4. Keep BOTH the timeout setting AND the debug setting" -ForegroundColor White
|
||||
Write-Host "5. Complete the merge" -ForegroundColor White
|
||||
Write-Host "`nRun '../verify.ps1' from the challenge directory to check your solution.`n" -ForegroundColor Cyan
|
||||
@@ -1,150 +0,0 @@
|
||||
#!/usr/bin/env pwsh
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Verifies the merge conflicts challenge solution.
|
||||
|
||||
.DESCRIPTION
|
||||
Checks that the user successfully resolved the merge conflict,
|
||||
kept both configuration settings, and completed the merge.
|
||||
#>
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
# Check current branch
|
||||
$currentBranch = git branch --show-current 2>$null
|
||||
if ($currentBranch -ne "main") {
|
||||
Write-Host "[FAIL] You should be on the 'main' branch." -ForegroundColor Red
|
||||
Write-Host "Current branch: $currentBranch" -ForegroundColor Yellow
|
||||
Write-Host "Hint: Use 'git checkout main' to switch to main branch" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check if there's an ongoing merge
|
||||
if (Test-Path ".git/MERGE_HEAD") {
|
||||
Write-Host "[FAIL] Merge is not complete. There are still unresolved conflicts." -ForegroundColor Red
|
||||
Write-Host "Hint: After resolving conflicts in config.json, use:" -ForegroundColor Yellow
|
||||
Write-Host " git add config.json" -ForegroundColor White
|
||||
Write-Host " git commit" -ForegroundColor White
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check if config.json exists
|
||||
if (-not (Test-Path "config.json")) {
|
||||
Write-Host "[FAIL] config.json file not found." -ForegroundColor Red
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Read and parse config.json
|
||||
try {
|
||||
$configContent = Get-Content "config.json" -Raw
|
||||
$config = $configContent | ConvertFrom-Json
|
||||
} catch {
|
||||
Write-Host "[FAIL] config.json is not valid JSON." -ForegroundColor Red
|
||||
Write-Host "Make sure you removed all conflict markers (<<<<<<, ======, >>>>>>)" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check for conflict markers (in case user forgot to remove them)
|
||||
if ($configContent -match "<<<<<<|======|>>>>>>") {
|
||||
Write-Host "[FAIL] Conflict markers still present in config.json" -ForegroundColor Red
|
||||
Write-Host "Remove all lines containing: <<<<<<, ======, >>>>>>" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check if both timeout and debug settings exist
|
||||
$hasTimeout = $null -ne $config.app.timeout
|
||||
$hasDebug = $null -ne $config.app.debug
|
||||
|
||||
if (-not $hasTimeout) {
|
||||
Write-Host "[FAIL] The 'timeout' setting is missing from config.json" -ForegroundColor Red
|
||||
Write-Host "Hint: The resolved file should include both timeout AND debug settings" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
if (-not $hasDebug) {
|
||||
Write-Host "[FAIL] The 'debug' setting is missing from config.json" -ForegroundColor Red
|
||||
Write-Host "Hint: The resolved file should include both timeout AND debug settings" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Validate the values
|
||||
if ($config.app.timeout -ne 5000) {
|
||||
Write-Host "[FAIL] The 'timeout' value should be 5000" -ForegroundColor Red
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
if ($config.app.debug -ne $true) {
|
||||
Write-Host "[FAIL] The 'debug' value should be true" -ForegroundColor Red
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check that merge commit exists
|
||||
$commitCount = (git rev-list --count HEAD 2>$null)
|
||||
if ($commitCount -lt 4) {
|
||||
Write-Host "[FAIL] Not enough commits. Expected at least 4 commits (including merge commit)." -ForegroundColor Red
|
||||
Write-Host "Current commits: $commitCount" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check that the latest commit is a merge commit (has 2 parents)
|
||||
$parentCount = (git rev-list --parents -n 1 HEAD 2>$null).Split(' ').Count - 1
|
||||
if ($parentCount -ne 2) {
|
||||
Write-Host "[FAIL] The latest commit should be a merge commit." -ForegroundColor Red
|
||||
Write-Host "Hint: Complete the merge with 'git commit' after resolving conflicts" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check that both branches are merged
|
||||
$branches = git branch --merged 2>$null
|
||||
if ($branches -notmatch "update-config") {
|
||||
Write-Host "[FAIL] The 'update-config' branch was not merged." -ForegroundColor Red
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# 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 "- Identified the merge conflict" -ForegroundColor White
|
||||
Write-Host "- Resolved the conflict by keeping both settings" -ForegroundColor White
|
||||
Write-Host "- Completed the merge with a proper merge commit" -ForegroundColor White
|
||||
Write-Host "`nYou now understand how to handle merge conflicts!" -ForegroundColor Green
|
||||
Write-Host "This is a critical skill for collaborative development.`n" -ForegroundColor Green
|
||||
|
||||
Set-Location ..
|
||||
exit 0
|
||||
@@ -1,130 +0,0 @@
|
||||
# Module 07: Rebasing
|
||||
|
||||
## Learning Objectives
|
||||
|
||||
By the end of this module, you will:
|
||||
- Understand what rebasing is and how it works
|
||||
- Know the difference between merge and rebase
|
||||
- Perform a rebase to integrate changes from one branch to another
|
||||
- Understand when to use rebase vs merge
|
||||
- Know the golden rule of rebasing
|
||||
|
||||
## Challenge Description
|
||||
|
||||
You have a repository with a `main` branch and a `feature` branch. While you were working on the feature branch, new commits were added to `main`. You want to incorporate those changes into your feature branch while maintaining a clean, linear history.
|
||||
|
||||
Your task is to:
|
||||
1. Review the current commit history
|
||||
2. Rebase the `feature` branch onto `main`
|
||||
3. Verify that the history is now linear
|
||||
|
||||
## Key Concepts
|
||||
|
||||
### What is Rebasing?
|
||||
|
||||
Rebasing is the process of moving or combining a sequence of commits to a new base commit. Instead of creating a merge commit like `git merge` does, rebasing rewrites the commit history by replaying your commits on top of another branch.
|
||||
|
||||
### Rebase vs Merge
|
||||
|
||||
**Merge:**
|
||||
```
|
||||
A---B---C feature
|
||||
/ \
|
||||
D---E---F---G main
|
||||
```
|
||||
Creates a merge commit (G) that ties the histories together.
|
||||
|
||||
**Rebase:**
|
||||
```
|
||||
A'--B'--C' feature
|
||||
/
|
||||
D---E---F main
|
||||
```
|
||||
Replays commits A, B, C on top of F, creating new commits A', B', C' with the same changes but different commit hashes.
|
||||
|
||||
### Benefits of Rebasing
|
||||
|
||||
- **Cleaner history**: Linear history is easier to read and understand
|
||||
- **Simpler log**: No merge commits cluttering the history
|
||||
- **Easier bisecting**: Finding bugs with `git bisect` is simpler with linear history
|
||||
|
||||
### The Golden Rule of Rebasing
|
||||
|
||||
**Never rebase commits that have been pushed to a public/shared repository.**
|
||||
|
||||
Why? Because rebasing rewrites history by creating new commits. If others have based work on the original commits, rebasing will cause serious problems for collaborators.
|
||||
|
||||
**Safe to rebase:**
|
||||
- Local commits not yet pushed
|
||||
- Feature branches you're working on alone
|
||||
- Cleaning up your work before creating a pull request
|
||||
|
||||
**Never rebase:**
|
||||
- Commits already pushed to a shared branch (like `main` or `develop`)
|
||||
- Commits that others might have based work on
|
||||
|
||||
## Useful Commands
|
||||
|
||||
```bash
|
||||
# Rebase current branch onto another branch
|
||||
git rebase <branch-name>
|
||||
|
||||
# View commit history as a graph
|
||||
git log --oneline --graph --all
|
||||
|
||||
# If conflicts occur during rebase:
|
||||
# 1. Resolve conflicts in files
|
||||
# 2. Stage the resolved files
|
||||
git add <file>
|
||||
# 3. Continue the rebase
|
||||
git rebase --continue
|
||||
|
||||
# Abort a rebase if something goes wrong
|
||||
git rebase --abort
|
||||
|
||||
# Check which branch you're on
|
||||
git branch
|
||||
|
||||
# Switch to a branch
|
||||
git checkout <branch-name>
|
||||
```
|
||||
|
||||
## Verification
|
||||
|
||||
Run the verification script to check your solution:
|
||||
|
||||
```bash
|
||||
.\verify.ps1
|
||||
```
|
||||
|
||||
The verification will check that:
|
||||
- You're on the feature branch
|
||||
- The rebase was completed successfully
|
||||
- The history is linear (no merge commits)
|
||||
- All commits from both branches are present
|
||||
|
||||
## Tips
|
||||
|
||||
- Always check your commit graph with `git log --oneline --graph --all` before and after rebasing
|
||||
- If you encounter conflicts during rebase, resolve them just like merge conflicts
|
||||
- Use `git rebase --abort` if you want to cancel the rebase and start over
|
||||
- Rebasing rewrites history, so the commit hashes will change
|
||||
- Only rebase local commits that haven't been shared with others
|
||||
|
||||
## When to Use Rebase vs Merge
|
||||
|
||||
**Use Rebase when:**
|
||||
- You want a clean, linear history
|
||||
- Working on a local feature branch that hasn't been shared
|
||||
- Updating your feature branch with the latest changes from main
|
||||
- You want to clean up commits before submitting a pull request
|
||||
|
||||
**Use Merge when:**
|
||||
- Working on a shared/public branch
|
||||
- You want to preserve the complete history including when branches diverged
|
||||
- You're merging a completed feature into main
|
||||
- You want to be safe and avoid rewriting history
|
||||
|
||||
## What You'll Learn
|
||||
|
||||
Rebasing is a powerful tool for maintaining a clean project history. While merging is safer and preserves exact history, rebasing creates a more readable linear timeline. Understanding both techniques and knowing when to use each is essential for effective Git workflow management.
|
||||
@@ -1,22 +0,0 @@
|
||||
#!/usr/bin/env pwsh
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Resets the rebasing 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"
|
||||
@@ -1,123 +0,0 @@
|
||||
#!/usr/bin/env pwsh
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Sets up the rebasing challenge environment.
|
||||
|
||||
.DESCRIPTION
|
||||
Creates a Git repository with diverged branches to practice rebasing.
|
||||
The feature branch needs to be rebased onto main.
|
||||
#>
|
||||
|
||||
# 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 file and commit
|
||||
$readme = @"
|
||||
# My Project
|
||||
|
||||
A sample project for learning Git rebasing.
|
||||
"@
|
||||
|
||||
Set-Content -Path "README.md" -Value $readme
|
||||
git add README.md
|
||||
git commit -m "Initial commit" | Out-Null
|
||||
|
||||
# Create and switch to feature branch
|
||||
git checkout -b feature | Out-Null
|
||||
|
||||
# Add commits on feature branch
|
||||
$feature1 = @"
|
||||
# My Project
|
||||
|
||||
A sample project for learning Git rebasing.
|
||||
|
||||
## Features
|
||||
|
||||
- Feature A: User authentication
|
||||
"@
|
||||
|
||||
Set-Content -Path "README.md" -Value $feature1
|
||||
git add README.md
|
||||
git commit -m "Add feature A" | Out-Null
|
||||
|
||||
$feature2 = @"
|
||||
# My Project
|
||||
|
||||
A sample project for learning Git rebasing.
|
||||
|
||||
## Features
|
||||
|
||||
- Feature A: User authentication
|
||||
- Feature B: Data validation
|
||||
"@
|
||||
|
||||
Set-Content -Path "README.md" -Value $feature2
|
||||
git add README.md
|
||||
git commit -m "Add feature B" | Out-Null
|
||||
|
||||
# Switch back to main and add commits (simulating other work happening on main)
|
||||
git checkout main | Out-Null
|
||||
|
||||
$main1 = @"
|
||||
# My Project
|
||||
|
||||
A sample project for learning Git rebasing.
|
||||
|
||||
## Installation
|
||||
|
||||
Run \`npm install\` to install dependencies.
|
||||
"@
|
||||
|
||||
Set-Content -Path "README.md" -Value $main1
|
||||
git add README.md
|
||||
git commit -m "Add installation instructions" | Out-Null
|
||||
|
||||
$main2 = @"
|
||||
# My Project
|
||||
|
||||
A sample project for learning Git rebasing.
|
||||
|
||||
## Installation
|
||||
|
||||
Run \`npm install\` to install dependencies.
|
||||
|
||||
## Configuration
|
||||
|
||||
Copy \`config.example.json\` to \`config.json\` and update settings.
|
||||
"@
|
||||
|
||||
Set-Content -Path "README.md" -Value $main2
|
||||
git add README.md
|
||||
git commit -m "Add configuration instructions" | Out-Null
|
||||
|
||||
# Switch back to feature branch
|
||||
git checkout 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 are now on the 'feature' branch." -ForegroundColor Cyan
|
||||
Write-Host "The 'main' branch has new commits that aren't in your feature branch." -ForegroundColor Cyan
|
||||
Write-Host "`nYour task:" -ForegroundColor Yellow
|
||||
Write-Host "1. Navigate to the challenge directory: cd challenge" -ForegroundColor White
|
||||
Write-Host "2. View the commit history: git log --oneline --graph --all" -ForegroundColor White
|
||||
Write-Host "3. Rebase the 'feature' branch onto 'main'" -ForegroundColor White
|
||||
Write-Host "4. View the history again to see the linear result" -ForegroundColor White
|
||||
Write-Host "`nRun '../verify.ps1' from the challenge directory to check your solution.`n" -ForegroundColor Cyan
|
||||
@@ -1,154 +0,0 @@
|
||||
#!/usr/bin/env pwsh
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Verifies the rebasing challenge solution.
|
||||
|
||||
.DESCRIPTION
|
||||
Checks that the user successfully rebased the feature branch onto main,
|
||||
resulting in a clean, linear history.
|
||||
#>
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
# Check current branch
|
||||
$currentBranch = git branch --show-current 2>$null
|
||||
if ($currentBranch -ne "feature") {
|
||||
Write-Host "[FAIL] You should be on the 'feature' branch." -ForegroundColor Red
|
||||
Write-Host "Current branch: $currentBranch" -ForegroundColor Yellow
|
||||
Write-Host "Hint: Use 'git checkout feature' to switch to feature branch" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check if there's an ongoing rebase
|
||||
if (Test-Path ".git/rebase-merge") {
|
||||
Write-Host "[FAIL] Rebase is not complete. There may be unresolved conflicts." -ForegroundColor Red
|
||||
Write-Host "Hint: Resolve any conflicts, then use:" -ForegroundColor Yellow
|
||||
Write-Host " git add <file>" -ForegroundColor White
|
||||
Write-Host " git rebase --continue" -ForegroundColor White
|
||||
Write-Host "Or abort with: git rebase --abort" -ForegroundColor White
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Get all commits on feature branch
|
||||
$featureCommits = git log --oneline feature 2>$null
|
||||
if (-not $featureCommits) {
|
||||
Write-Host "[FAIL] No commits found on feature branch." -ForegroundColor Red
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check commit count (should be 5: 1 initial + 2 main + 2 feature)
|
||||
$commitCount = (git rev-list --count feature 2>$null)
|
||||
if ($commitCount -ne 5) {
|
||||
Write-Host "[FAIL] Expected 5 commits on feature branch, found $commitCount" -ForegroundColor Red
|
||||
Write-Host "Hint: The rebased feature branch should contain:" -ForegroundColor Yellow
|
||||
Write-Host " - 1 initial commit" -ForegroundColor White
|
||||
Write-Host " - 2 commits from main" -ForegroundColor White
|
||||
Write-Host " - 2 commits from feature" -ForegroundColor White
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Get commit messages in reverse chronological order (newest first)
|
||||
$commits = git log --pretty=format:"%s" feature 2>$null
|
||||
|
||||
# Convert to array and reverse to get chronological order (oldest first)
|
||||
$commitArray = $commits -split "`n"
|
||||
[array]::Reverse($commitArray)
|
||||
|
||||
# Check that commits are in the expected order after rebase
|
||||
$expectedOrder = @(
|
||||
"Initial commit",
|
||||
"Add installation instructions",
|
||||
"Add configuration instructions",
|
||||
"Add feature A",
|
||||
"Add feature B"
|
||||
)
|
||||
|
||||
$orderCorrect = $true
|
||||
for ($i = 0; $i -lt $expectedOrder.Length; $i++) {
|
||||
if ($commitArray[$i] -ne $expectedOrder[$i]) {
|
||||
$orderCorrect = $false
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if (-not $orderCorrect) {
|
||||
Write-Host "[FAIL] Commit order is incorrect after rebase." -ForegroundColor Red
|
||||
Write-Host "Expected order (oldest to newest):" -ForegroundColor Yellow
|
||||
$expectedOrder | ForEach-Object { Write-Host " $_" -ForegroundColor White }
|
||||
Write-Host "`nActual order:" -ForegroundColor Yellow
|
||||
$commitArray | ForEach-Object { Write-Host " $_" -ForegroundColor White }
|
||||
Write-Host "`nHint: The feature commits should come AFTER the main commits" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check that main branch still has only 3 commits (initial + 2 main commits)
|
||||
$mainCommitCount = (git rev-list --count main 2>$null)
|
||||
if ($mainCommitCount -ne 3) {
|
||||
Write-Host "[FAIL] Main branch should have 3 commits, found $mainCommitCount" -ForegroundColor Red
|
||||
Write-Host "Hint: You should rebase feature onto main, not the other way around" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check that there are no merge commits (parent count should be 1 for all commits)
|
||||
$mergeCommits = git log --merges --oneline feature 2>$null
|
||||
if ($mergeCommits) {
|
||||
Write-Host "[FAIL] Found merge commits in history. Rebasing should create a linear history." -ForegroundColor Red
|
||||
Write-Host "Hint: Use 'git rebase main' instead of 'git merge main'" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Verify that feature branch contains all commits from main
|
||||
$mainCommits = git log --pretty=format:"%s" main 2>$null
|
||||
$featureCommitMessages = git log --pretty=format:"%s" feature 2>$null
|
||||
|
||||
foreach ($mainCommit in ($mainCommits -split "`n")) {
|
||||
if ($featureCommitMessages -notcontains $mainCommit) {
|
||||
Write-Host "[FAIL] Feature branch is missing commits from main." -ForegroundColor Red
|
||||
Write-Host "Missing: $mainCommit" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
|
||||
# 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 "- Rebased the feature branch onto main" -ForegroundColor White
|
||||
Write-Host "- Created a clean, linear commit history" -ForegroundColor White
|
||||
Write-Host "- Preserved all commits from both branches" -ForegroundColor White
|
||||
Write-Host "`nYour feature branch now has a linear history!" -ForegroundColor Green
|
||||
Write-Host "Run 'git log --oneline --graph --all' to see the result.`n" -ForegroundColor Cyan
|
||||
|
||||
Set-Location ..
|
||||
exit 0
|
||||
@@ -1,159 +0,0 @@
|
||||
# Module 08: Interactive Rebase
|
||||
|
||||
## Learning Objectives
|
||||
|
||||
By the end of this module, you will:
|
||||
- Understand what interactive rebase is and when to use it
|
||||
- Learn the different interactive rebase commands (pick, reword, squash, fixup, drop)
|
||||
- Clean up commit history by squashing related commits
|
||||
- Reword commit messages to be more descriptive
|
||||
- Use reset and commit techniques to achieve similar results
|
||||
|
||||
## Challenge Description
|
||||
|
||||
You have a feature branch with messy commit history - multiple "WIP" commits, typos in commit messages, and commits that should be combined. Before submitting a pull request, you want to clean up this history.
|
||||
|
||||
Your task is to:
|
||||
1. Review the current commit history with its messy commits
|
||||
2. Combine the four feature commits into a single, well-described commit
|
||||
3. Keep the initial commit unchanged
|
||||
4. End up with a clean, professional commit history
|
||||
|
||||
## Key Concepts
|
||||
|
||||
### What is Interactive Rebase?
|
||||
|
||||
Interactive rebase (`git rebase -i`) is a powerful tool that lets you modify commit history. Unlike regular rebase which simply moves commits, interactive rebase lets you:
|
||||
- Reorder commits
|
||||
- Edit commit messages (reword)
|
||||
- Combine multiple commits (squash/fixup)
|
||||
- Split commits into smaller ones
|
||||
- Delete commits entirely
|
||||
- Edit the contents of commits
|
||||
|
||||
### Interactive Rebase Commands
|
||||
|
||||
When you run `git rebase -i HEAD~3`, Git opens an editor with a list of commits and commands:
|
||||
|
||||
```
|
||||
pick abc1234 First commit
|
||||
pick def5678 Second commit
|
||||
pick ghi9012 Third commit
|
||||
```
|
||||
|
||||
You can change `pick` to other commands:
|
||||
|
||||
- **pick**: Keep the commit as-is
|
||||
- **reword**: Keep the commit but edit its message
|
||||
- **edit**: Pause the rebase to amend the commit
|
||||
- **squash**: Combine this commit with the previous one, keeping both messages
|
||||
- **fixup**: Like squash, but discard this commit's message
|
||||
- **drop**: Remove the commit entirely
|
||||
|
||||
### When to Use Interactive Rebase
|
||||
|
||||
Interactive rebase is perfect for:
|
||||
- Cleaning up work-in-progress commits before creating a pull request
|
||||
- Fixing typos in commit messages
|
||||
- Combining related commits into logical units
|
||||
- Removing debug commits or experimental code
|
||||
- Creating a clean, professional history
|
||||
|
||||
**Remember**: Only rebase commits that haven't been pushed to a shared branch!
|
||||
|
||||
## Alternative Approach: Reset and Recommit
|
||||
|
||||
Since interactive rebase requires an interactive editor, this challenge uses an alternative approach that achieves the same result:
|
||||
|
||||
1. **Reset soft**: Move HEAD back while keeping changes staged
|
||||
```bash
|
||||
git reset --soft HEAD~4
|
||||
```
|
||||
This keeps all changes from the last 4 commits but "uncommits" them.
|
||||
|
||||
2. **Create a new commit**: Commit all changes with a clean message
|
||||
```bash
|
||||
git commit -m "Your new clean commit message"
|
||||
```
|
||||
|
||||
This technique is useful when you want to combine multiple commits into one without using interactive rebase.
|
||||
|
||||
## Useful Commands
|
||||
|
||||
```bash
|
||||
# View commit history
|
||||
git log --oneline
|
||||
|
||||
# See the last N commits in detail
|
||||
git log -n 5
|
||||
|
||||
# Reset to N commits back, keeping changes staged
|
||||
git reset --soft HEAD~N
|
||||
|
||||
# Reset to N commits back, keeping changes unstaged
|
||||
git reset --mixed HEAD~N
|
||||
|
||||
# Reset to N commits back, discarding all changes (DANGEROUS!)
|
||||
git reset --hard HEAD~N
|
||||
|
||||
# Create a new commit
|
||||
git commit -m "message"
|
||||
|
||||
# Amend the last commit
|
||||
git commit --amend -m "new message"
|
||||
|
||||
# Interactive rebase (requires interactive editor)
|
||||
git rebase -i HEAD~N
|
||||
|
||||
# Check current status
|
||||
git status
|
||||
```
|
||||
|
||||
## Verification
|
||||
|
||||
Run the verification script to check your solution:
|
||||
|
||||
```bash
|
||||
.\verify.ps1
|
||||
```
|
||||
|
||||
The verification will check that:
|
||||
- You have exactly 2 commits total (initial + your combined feature commit)
|
||||
- The feature commit contains all the changes from the original 4 feature commits
|
||||
- The commit message is clean and descriptive
|
||||
- All expected files are present
|
||||
|
||||
## Challenge Steps
|
||||
|
||||
1. Navigate to the challenge directory
|
||||
2. View the current messy commit history: `git log --oneline`
|
||||
3. Use `git reset --soft HEAD~4` to uncommit the last 4 commits while keeping changes
|
||||
4. Check status with `git status` - you should see all changes staged
|
||||
5. Create a new commit with a clean message: `git commit -m "Add user profile feature with validation"`
|
||||
6. Verify with `git log --oneline` - you should now have clean history
|
||||
7. Run the verification script
|
||||
|
||||
## Tips
|
||||
|
||||
- Always check your history with `git log --oneline` before and after
|
||||
- `git reset --soft` is safer than `--hard` because it keeps your changes
|
||||
- You can use `git reset --soft HEAD~N` where N is the number of commits to undo
|
||||
- If you make a mistake, run `.\reset.ps1` to start over
|
||||
- In real projects with interactive rebase, you would use `git rebase -i HEAD~N` and modify the commit list in the editor
|
||||
|
||||
## What is Git Reset?
|
||||
|
||||
`git reset` moves the current branch pointer to a different commit:
|
||||
|
||||
- **--soft**: Moves HEAD but keeps changes staged
|
||||
- **--mixed** (default): Moves HEAD and unstages changes, but keeps them in working directory
|
||||
- **--hard**: Moves HEAD and discards all changes (dangerous!)
|
||||
|
||||
Reset is useful for:
|
||||
- Undoing commits while keeping changes (`--soft`)
|
||||
- Unstaging files (`--mixed`)
|
||||
- Discarding commits entirely (`--hard`)
|
||||
|
||||
## What You'll Learn
|
||||
|
||||
Cleaning up commit history is an important skill for professional development. Whether you use interactive rebase or reset-and-recommit, the goal is the same: create a clear, logical history that makes your code review easier and your project history more understandable. Messy WIP commits are fine during development, but cleaning them up before merging shows attention to detail and makes your codebase more maintainable.
|
||||
@@ -1,22 +0,0 @@
|
||||
#!/usr/bin/env pwsh
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Resets the interactive rebase 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"
|
||||
@@ -1,133 +0,0 @@
|
||||
#!/usr/bin/env pwsh
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Sets up the interactive rebase challenge environment.
|
||||
|
||||
.DESCRIPTION
|
||||
Creates a Git repository with messy commit history that needs to be
|
||||
cleaned up using reset and recommit techniques.
|
||||
#>
|
||||
|
||||
# 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 commit
|
||||
$readme = @"
|
||||
# User Management System
|
||||
|
||||
A simple user management application.
|
||||
"@
|
||||
|
||||
Set-Content -Path "README.md" -Value $readme
|
||||
git add README.md
|
||||
git commit -m "Initial commit" | Out-Null
|
||||
|
||||
# Create messy commits that should be squashed
|
||||
|
||||
# Commit 1: WIP user profile
|
||||
$userProfile = @"
|
||||
class UserProfile:
|
||||
def __init__(self, name, email):
|
||||
self.name = name
|
||||
self.email = email
|
||||
"@
|
||||
|
||||
Set-Content -Path "user_profile.py" -Value $userProfile
|
||||
git add user_profile.py
|
||||
git commit -m "WIP: user profile" | Out-Null
|
||||
|
||||
# Commit 2: Add validation (typo in message)
|
||||
$userProfileWithValidation = @"
|
||||
class UserProfile:
|
||||
def __init__(self, name, email):
|
||||
self.name = name
|
||||
self.email = email
|
||||
|
||||
def validate(self):
|
||||
if not self.name or not self.email:
|
||||
raise ValueError('Name and email are required')
|
||||
return True
|
||||
"@
|
||||
|
||||
Set-Content -Path "user_profile.py" -Value $userProfileWithValidation
|
||||
git add user_profile.py
|
||||
git commit -m "add validaton" | Out-Null # Intentional typo
|
||||
|
||||
# Commit 3: Fix validation
|
||||
$userProfileFixed = @"
|
||||
class UserProfile:
|
||||
def __init__(self, name, email):
|
||||
self.name = name
|
||||
self.email = email
|
||||
|
||||
def validate(self):
|
||||
if not self.name or not self.email:
|
||||
raise ValueError('Name and email are required')
|
||||
if '@' not in self.email:
|
||||
raise ValueError('Invalid email format')
|
||||
return True
|
||||
"@
|
||||
|
||||
Set-Content -Path "user_profile.py" -Value $userProfileFixed
|
||||
git add user_profile.py
|
||||
git commit -m "fix validation bug" | Out-Null
|
||||
|
||||
# Commit 4: Add tests (another WIP commit)
|
||||
$tests = @"
|
||||
import unittest
|
||||
from user_profile import UserProfile
|
||||
|
||||
class TestUserProfile(unittest.TestCase):
|
||||
def test_validate_correct_user_data(self):
|
||||
user = UserProfile('John', 'john@example.com')
|
||||
self.assertTrue(user.validate())
|
||||
|
||||
def test_reject_missing_name(self):
|
||||
user = UserProfile('', 'john@example.com')
|
||||
with self.assertRaises(ValueError):
|
||||
user.validate()
|
||||
|
||||
def test_reject_invalid_email(self):
|
||||
user = UserProfile('John', 'invalid-email')
|
||||
with self.assertRaises(ValueError):
|
||||
user.validate()
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
"@
|
||||
|
||||
Set-Content -Path "test_user_profile.py" -Value $tests
|
||||
git add test_user_profile.py
|
||||
git commit -m "WIP tests" | 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 a repository with messy commit history:" -ForegroundColor Cyan
|
||||
Write-Host "- WIP commits" -ForegroundColor Yellow
|
||||
Write-Host "- Typos in commit messages" -ForegroundColor Yellow
|
||||
Write-Host "- Multiple commits that should be combined" -ForegroundColor Yellow
|
||||
Write-Host "`nYour task:" -ForegroundColor Yellow
|
||||
Write-Host "1. Navigate to the challenge directory: cd challenge" -ForegroundColor White
|
||||
Write-Host "2. View the messy history: git log --oneline" -ForegroundColor White
|
||||
Write-Host "3. Use 'git reset --soft HEAD~4' to uncommit the last 4 commits" -ForegroundColor White
|
||||
Write-Host "4. Create a single clean commit with a descriptive message" -ForegroundColor White
|
||||
Write-Host " Example: git commit -m 'Add user profile feature with validation'" -ForegroundColor White
|
||||
Write-Host "`nRun '../verify.ps1' from the challenge directory to check your solution.`n" -ForegroundColor Cyan
|
||||
@@ -1,172 +0,0 @@
|
||||
#!/usr/bin/env pwsh
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Verifies the interactive rebase challenge solution.
|
||||
|
||||
.DESCRIPTION
|
||||
Checks that the user successfully cleaned up the commit history
|
||||
by combining multiple messy commits into a single clean commit.
|
||||
#>
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
# Check commit count (should be exactly 2: initial + combined feature commit)
|
||||
$commitCount = (git rev-list --count HEAD 2>$null)
|
||||
if ($commitCount -ne 2) {
|
||||
Write-Host "[FAIL] Expected exactly 2 commits, found $commitCount" -ForegroundColor Red
|
||||
if ($commitCount -gt 2) {
|
||||
Write-Host "Hint: You have too many commits. Use 'git reset --soft HEAD~N' to combine them." -ForegroundColor Yellow
|
||||
Write-Host " Where N is the number of commits to undo (should be 4 in this case)." -ForegroundColor Yellow
|
||||
} else {
|
||||
Write-Host "Hint: You may have reset too far back. Run ../reset.ps1 to start over." -ForegroundColor Yellow
|
||||
}
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Get commit messages
|
||||
$commits = git log --pretty=format:"%s" 2>$null
|
||||
$commitArray = $commits -split "`n"
|
||||
|
||||
# Check that first commit is still the initial commit
|
||||
if ($commitArray[1] -ne "Initial commit") {
|
||||
Write-Host "[FAIL] The initial commit should remain unchanged." -ForegroundColor Red
|
||||
Write-Host "Expected first commit: 'Initial commit'" -ForegroundColor Yellow
|
||||
Write-Host "Found: '$($commitArray[1])'" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check that the second commit message is clean (not WIP, not with typos)
|
||||
$featureCommit = $commitArray[0]
|
||||
if ($featureCommit -match "WIP|wip") {
|
||||
Write-Host "[FAIL] Commit message still contains 'WIP'." -ForegroundColor Red
|
||||
Write-Host "Current message: '$featureCommit'" -ForegroundColor Yellow
|
||||
Write-Host "Hint: Create a clean, descriptive commit message." -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
if ($featureCommit -match "validaton") {
|
||||
Write-Host "[FAIL] Commit message contains a typo ('validaton')." -ForegroundColor Red
|
||||
Write-Host "Current message: '$featureCommit'" -ForegroundColor Yellow
|
||||
Write-Host "Hint: Use a properly spelled commit message." -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check that commit message is not empty and has reasonable length
|
||||
if ($featureCommit.Length -lt 10) {
|
||||
Write-Host "[FAIL] Commit message is too short. Be more descriptive." -ForegroundColor Red
|
||||
Write-Host "Current message: '$featureCommit'" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check that required files exist
|
||||
if (-not (Test-Path "user_profile.py")) {
|
||||
Write-Host "[FAIL] user_profile.py not found." -ForegroundColor Red
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
if (-not (Test-Path "test_user_profile.py")) {
|
||||
Write-Host "[FAIL] test_user_profile.py not found." -ForegroundColor Red
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check that user_profile.py contains all expected features
|
||||
$userProfileContent = Get-Content "user_profile.py" -Raw
|
||||
|
||||
# Should have the class
|
||||
if ($userProfileContent -notmatch "class UserProfile") {
|
||||
Write-Host "[FAIL] user_profile.py should contain UserProfile class." -ForegroundColor Red
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Should have validation method
|
||||
if ($userProfileContent -notmatch "def validate\(") {
|
||||
Write-Host "[FAIL] user_profile.py should contain validate() method." -ForegroundColor Red
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Should have email format validation (the final fix from commit 3)
|
||||
if ($userProfileContent -notmatch "'@'.*in.*email|email.*in.*'@'") {
|
||||
Write-Host "[FAIL] user_profile.py should contain email format validation." -ForegroundColor Red
|
||||
Write-Host "Hint: Make sure all changes from all 4 commits are included." -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check that test file has content
|
||||
$testContent = Get-Content "test_user_profile.py" -Raw
|
||||
|
||||
if ($testContent -notmatch "class.*TestUserProfile") {
|
||||
Write-Host "[FAIL] test_user_profile.py should contain TestUserProfile tests." -ForegroundColor Red
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check that we have at least 3 test cases
|
||||
$testMatches = ([regex]::Matches($testContent, "def test_")).Count
|
||||
if ($testMatches -lt 3) {
|
||||
Write-Host "[FAIL] test_user_profile.py should contain at least 3 test cases." -ForegroundColor Red
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Verify that the latest commit contains changes to both files
|
||||
$filesInLastCommit = git diff-tree --no-commit-id --name-only -r HEAD 2>$null
|
||||
if ($filesInLastCommit -notcontains "user_profile.py") {
|
||||
Write-Host "[FAIL] The feature commit should include user_profile.py" -ForegroundColor Red
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
if ($filesInLastCommit -notcontains "test_user_profile.py") {
|
||||
Write-Host "[FAIL] The feature commit should include test_user_profile.py" -ForegroundColor Red
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# 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 "- Combined 4 messy commits into 1 clean commit" -ForegroundColor White
|
||||
Write-Host "- Created a descriptive commit message" -ForegroundColor White
|
||||
Write-Host "- Preserved all code changes" -ForegroundColor White
|
||||
Write-Host "- Cleaned up the commit history" -ForegroundColor White
|
||||
Write-Host "`nYour commit history is now clean and professional!" -ForegroundColor Green
|
||||
Write-Host "Run 'git log --oneline' to see the result.`n" -ForegroundColor Cyan
|
||||
Write-Host "Key takeaway: Clean commit history makes code review easier" -ForegroundColor Yellow
|
||||
Write-Host "and your project more maintainable.`n" -ForegroundColor Yellow
|
||||
|
||||
Set-Location ..
|
||||
exit 0
|
||||
@@ -1,160 +0,0 @@
|
||||
# Module 09: Cherry-Pick
|
||||
|
||||
## Learning Objectives
|
||||
|
||||
By the end of this module, you will:
|
||||
- Understand what cherry-picking is and how it works
|
||||
- Know when to use cherry-pick vs merge or rebase
|
||||
- Apply specific commits from one branch to another
|
||||
- Handle cherry-pick conflicts if they occur
|
||||
- Understand common use cases for cherry-picking
|
||||
|
||||
## Challenge Description
|
||||
|
||||
You have a `development` branch with several commits. Some of these commits are bug fixes that need to be applied to the `main` branch immediately, but other commits are experimental features that shouldn't be merged yet.
|
||||
|
||||
Your task is to:
|
||||
1. Review the commits on the development branch
|
||||
2. Identify which commits are bug fixes
|
||||
3. Cherry-pick only the bug fix commits to the main branch
|
||||
4. Verify that main has the bug fixes but not the experimental features
|
||||
|
||||
## Key Concepts
|
||||
|
||||
### What is Cherry-Pick?
|
||||
|
||||
Cherry-pick allows you to apply a specific commit from one branch to another. Instead of merging an entire branch, you can selectively choose individual commits.
|
||||
|
||||
```
|
||||
A---B---C---D development
|
||||
/
|
||||
E---F main
|
||||
```
|
||||
|
||||
After cherry-picking commit C:
|
||||
```
|
||||
A---B---C---D development
|
||||
/
|
||||
E---F---C' main
|
||||
```
|
||||
|
||||
Note that C' is a new commit with the same changes as C but a different commit hash.
|
||||
|
||||
### Cherry-Pick vs Merge vs Rebase
|
||||
|
||||
- **Merge**: Brings all commits from another branch and creates a merge commit
|
||||
- **Rebase**: Replays all commits from your branch on top of another branch
|
||||
- **Cherry-Pick**: Applies one or more specific commits to your current branch
|
||||
|
||||
### When to Use Cherry-Pick
|
||||
|
||||
Cherry-pick is useful when you:
|
||||
- Need a bug fix from a feature branch but can't merge the whole branch yet
|
||||
- Want to apply a specific commit to a release branch
|
||||
- Need to backport a fix to an older version
|
||||
- Made a commit on the wrong branch and need to move it
|
||||
- Want to duplicate a commit across multiple branches
|
||||
|
||||
### Cherry-Pick Creates New Commits
|
||||
|
||||
Important: Cherry-picked commits are new commits with different hashes. The original commit remains on the source branch, and a copy is created on the target branch with the same changes but a different commit ID.
|
||||
|
||||
## Useful Commands
|
||||
|
||||
```bash
|
||||
# View commits on another branch
|
||||
git log <branch-name> --oneline
|
||||
|
||||
# View a specific commit's details
|
||||
git show <commit-hash>
|
||||
|
||||
# Cherry-pick a single commit
|
||||
git cherry-pick <commit-hash>
|
||||
|
||||
# Cherry-pick multiple commits
|
||||
git cherry-pick <commit-hash1> <commit-hash2>
|
||||
|
||||
# Cherry-pick a range of commits
|
||||
git cherry-pick <start-hash>..<end-hash>
|
||||
|
||||
# If conflicts occur during cherry-pick:
|
||||
# 1. Resolve conflicts in files
|
||||
# 2. Stage the resolved files
|
||||
git add <file>
|
||||
# 3. Continue the cherry-pick
|
||||
git cherry-pick --continue
|
||||
|
||||
# Abort a cherry-pick if something goes wrong
|
||||
git cherry-pick --abort
|
||||
|
||||
# View commit history graph
|
||||
git log --oneline --graph --all
|
||||
```
|
||||
|
||||
## Verification
|
||||
|
||||
Run the verification script to check your solution:
|
||||
|
||||
```bash
|
||||
.\verify.ps1
|
||||
```
|
||||
|
||||
The verification will check that:
|
||||
- You're on the main branch
|
||||
- The security bug fix commit has been applied to main
|
||||
- The performance bug fix commit has been applied to main
|
||||
- The experimental features are NOT on main
|
||||
- The commits were cherry-picked (not merged)
|
||||
|
||||
## Challenge Steps
|
||||
|
||||
1. Navigate to the challenge directory
|
||||
2. You're currently on the development branch
|
||||
3. View the commits: `git log --oneline`
|
||||
4. You'll see several commits - identify the bug fixes
|
||||
5. Switch to main branch: `git checkout main`
|
||||
6. Cherry-pick the bug fix commits (you'll need their commit hashes)
|
||||
7. Verify the result with `git log --oneline`
|
||||
8. Run the verification script
|
||||
|
||||
## Tips
|
||||
|
||||
- Use `git log development --oneline` to see commits on the development branch
|
||||
- Use `git show <hash>` to view details of a specific commit
|
||||
- You can cherry-pick by commit hash - you only need the first 7 characters
|
||||
- Cherry-pick commits in chronological order (oldest first) to avoid conflicts
|
||||
- If you make a mistake, use `.\reset.ps1` to start over
|
||||
- The commit message will be preserved when cherry-picking
|
||||
|
||||
## Common Cherry-Pick Scenarios
|
||||
|
||||
### Hotfix to Production
|
||||
You have a critical bug fix on a development branch that needs to go to production immediately:
|
||||
```bash
|
||||
git checkout production
|
||||
git cherry-pick <bugfix-commit-hash>
|
||||
```
|
||||
|
||||
### Wrong Branch
|
||||
You accidentally committed on the wrong branch:
|
||||
```bash
|
||||
# On wrong branch, note the commit hash
|
||||
git log --oneline
|
||||
# Switch to correct branch
|
||||
git checkout correct-branch
|
||||
git cherry-pick <commit-hash>
|
||||
# Go back and remove from wrong branch
|
||||
git checkout wrong-branch
|
||||
git reset --hard HEAD~1
|
||||
```
|
||||
|
||||
### Backporting
|
||||
You need to apply a fix to an older release branch:
|
||||
```bash
|
||||
git checkout release-2.0
|
||||
git cherry-pick <fix-from-main>
|
||||
```
|
||||
|
||||
## What You'll Learn
|
||||
|
||||
Cherry-pick is a surgical tool in your Git toolbox. While merge and rebase work with entire branches, cherry-pick lets you be selective about which changes to apply. This is invaluable for managing hotfixes, maintaining multiple release branches, and handling situations where you need specific changes without bringing along everything else. Understanding when to use cherry-pick versus other Git operations is a mark of Git expertise.
|
||||
@@ -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 checkout 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.
|
||||
@@ -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"
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -1,199 +0,0 @@
|
||||
# Module 11: Stash
|
||||
|
||||
## Learning Objectives
|
||||
|
||||
By the end of this module, you will:
|
||||
- Understand what git stash is and when to use it
|
||||
- Temporarily save work without committing
|
||||
- Switch between branches without losing uncommitted changes
|
||||
- Manage multiple stashes
|
||||
- Apply and remove stashed changes
|
||||
- Understand the difference between stash pop and stash apply
|
||||
|
||||
## Challenge Description
|
||||
|
||||
You're working on a feature when your teammate reports a critical bug in production. You need to switch to the main branch to fix it immediately, but your current work is incomplete and not ready to commit.
|
||||
|
||||
Your task is to:
|
||||
1. Stash your incomplete work
|
||||
2. Switch to the main branch
|
||||
3. Fix the urgent bug and commit it
|
||||
4. Return to your feature branch
|
||||
5. Restore your stashed changes
|
||||
6. Complete your feature and commit it
|
||||
|
||||
## Key Concepts
|
||||
|
||||
### What is Git Stash?
|
||||
|
||||
Git stash temporarily saves your uncommitted changes (both staged and unstaged) and reverts your working directory to match the HEAD commit. Think of it as a clipboard for your changes.
|
||||
|
||||
### When to Use Stash
|
||||
|
||||
Use stash when you need to:
|
||||
- Switch branches but have uncommitted changes
|
||||
- Pull updates from remote but have local modifications
|
||||
- Quickly test something on a clean working directory
|
||||
- Save work temporarily without creating a commit
|
||||
- Context-switch between tasks
|
||||
|
||||
### Stash vs Commit
|
||||
|
||||
**Stash:**
|
||||
- Temporary storage
|
||||
- Not part of project history
|
||||
- Can be applied to different branches
|
||||
- Easy to discard if not needed
|
||||
- Local only (not pushed to remote)
|
||||
|
||||
**Commit:**
|
||||
- Permanent part of history
|
||||
- Creates a snapshot in the project timeline
|
||||
- Associated with a specific branch
|
||||
- Should be meaningful and complete
|
||||
- Can be pushed to remote
|
||||
|
||||
### The Stash Stack
|
||||
|
||||
Git stash works like a stack (LIFO - Last In, First Out):
|
||||
```
|
||||
stash@{0} <- Most recent stash (top of stack)
|
||||
stash@{1}
|
||||
stash@{2} <- Oldest stash
|
||||
```
|
||||
|
||||
You can have multiple stashes and apply any of them.
|
||||
|
||||
## Useful Commands
|
||||
|
||||
```bash
|
||||
# Stash current changes
|
||||
git stash
|
||||
git stash save "description" # With a descriptive message
|
||||
|
||||
# Stash including untracked files
|
||||
git stash -u
|
||||
|
||||
# List all stashes
|
||||
git stash list
|
||||
|
||||
# Apply most recent stash and remove it from stack
|
||||
git stash pop
|
||||
|
||||
# Apply most recent stash but keep it in stack
|
||||
git stash apply
|
||||
|
||||
# Apply a specific stash
|
||||
git stash apply stash@{1}
|
||||
|
||||
# Show what's in a stash
|
||||
git stash show
|
||||
git stash show -p # Show full diff
|
||||
|
||||
# Drop (delete) a stash
|
||||
git stash drop stash@{0}
|
||||
|
||||
# Clear all stashes
|
||||
git stash clear
|
||||
|
||||
# Create a branch from a stash
|
||||
git stash branch new-branch-name
|
||||
```
|
||||
|
||||
## Verification
|
||||
|
||||
Run the verification script to check your solution:
|
||||
|
||||
```bash
|
||||
.\verify.ps1
|
||||
```
|
||||
|
||||
The verification will check that:
|
||||
- The bug fix commit exists on main
|
||||
- Your feature is completed on the feature branch
|
||||
- Changes were properly stashed and restored
|
||||
- No uncommitted changes remain
|
||||
|
||||
## Challenge Steps
|
||||
|
||||
1. Navigate to the challenge directory
|
||||
2. You're on feature-login with uncommitted changes
|
||||
3. Check status: `git status` (you'll see modified files)
|
||||
4. Stash your changes: `git stash save "WIP: login feature"`
|
||||
5. Verify working directory is clean: `git status`
|
||||
6. Switch to main: `git checkout main`
|
||||
7. View the bug in app.js and fix it (remove the incorrect line)
|
||||
8. Commit the fix: `git add app.js && git commit -m "Fix critical security bug"`
|
||||
9. Switch back to feature: `git checkout feature-login`
|
||||
10. Restore your work: `git stash pop`
|
||||
11. Complete the feature (the TODOs in login.js)
|
||||
12. Commit your completed feature
|
||||
13. Run verification
|
||||
|
||||
## Tips
|
||||
|
||||
- Always use `git stash save "message"` to describe what you're stashing
|
||||
- Use `git stash list` to see all your stashes
|
||||
- `git stash pop` applies and removes the stash (use this most often)
|
||||
- `git stash apply` keeps the stash (useful if you want to apply it to multiple branches)
|
||||
- Stashes are local - they don't get pushed to remote repositories
|
||||
- You can stash even if you have changes to different files
|
||||
- Stash before pulling to avoid merge conflicts
|
||||
- Use `git stash show -p` to preview what's in a stash before applying
|
||||
|
||||
## Common Stash Scenarios
|
||||
|
||||
### Scenario 1: Quick Branch Switch
|
||||
```bash
|
||||
# Working on feature, need to switch to main
|
||||
git stash
|
||||
git checkout main
|
||||
# Do work on main
|
||||
git checkout feature
|
||||
git stash pop
|
||||
```
|
||||
|
||||
### Scenario 2: Pull with Local Changes
|
||||
```bash
|
||||
# You have local changes but need to pull
|
||||
git stash
|
||||
git pull
|
||||
git stash pop
|
||||
# Resolve any conflicts
|
||||
```
|
||||
|
||||
### Scenario 3: Experimental Changes
|
||||
```bash
|
||||
# Try something experimental
|
||||
git stash # Save current work
|
||||
# Make experimental changes
|
||||
# Decide you don't like it
|
||||
git restore . # Discard experiment
|
||||
git stash pop # Restore original work
|
||||
```
|
||||
|
||||
### Scenario 4: Apply to Multiple Branches
|
||||
```bash
|
||||
# Same fix needed on multiple branches
|
||||
git stash
|
||||
git checkout branch1
|
||||
git stash apply
|
||||
git commit -am "Apply fix"
|
||||
git checkout branch2
|
||||
git stash apply
|
||||
git commit -am "Apply fix"
|
||||
git stash drop # Clean up when done
|
||||
```
|
||||
|
||||
## Stash Conflicts
|
||||
|
||||
If applying a stash causes conflicts:
|
||||
1. Git will mark the conflicts in your files
|
||||
2. Resolve conflicts manually (like merge conflicts)
|
||||
3. Stage the resolved files: `git add <file>`
|
||||
4. The stash is automatically dropped after successful pop
|
||||
5. If you used `apply`, manually drop it: `git stash drop`
|
||||
|
||||
## What You'll Learn
|
||||
|
||||
Git stash is an essential tool for managing context switches in your daily workflow. It lets you maintain a clean working directory while preserving incomplete work, making it easy to handle interruptions, urgent fixes, and quick branch switches. Mastering stash makes you more efficient and helps avoid the temptation to make "WIP" commits just to switch branches. Think of stash as your temporary workspace that follows you around.
|
||||
@@ -1,252 +0,0 @@
|
||||
# Module 12: Working with Remotes
|
||||
|
||||
## Learning Objectives
|
||||
|
||||
By the end of this module, you will:
|
||||
- Understand what remote repositories are
|
||||
- Clone a repository from a remote source
|
||||
- Push local commits to a remote repository
|
||||
- Pull changes from a remote repository
|
||||
- Understand the difference between fetch and pull
|
||||
- Manage remote branches
|
||||
- Work with remote tracking branches
|
||||
|
||||
## Challenge Description
|
||||
|
||||
You're joining a team project that's already hosted on a remote server. You need to clone the repository, make changes, and synchronize your work with the remote.
|
||||
|
||||
Your task is to:
|
||||
1. Clone the remote repository
|
||||
2. Create a new branch for your feature
|
||||
3. Make changes and commit them locally
|
||||
4. Push your branch to the remote
|
||||
5. Fetch updates that were made by teammates
|
||||
6. Merge remote changes into your branch
|
||||
|
||||
## Key Concepts
|
||||
|
||||
### What is a Remote Repository?
|
||||
|
||||
A remote repository is a version of your project hosted on a server (like GitHub, GitLab, or Bitbucket) or another location. It allows teams to collaborate by sharing code.
|
||||
|
||||
### Common Remote Operations
|
||||
|
||||
**Clone**: Create a local copy of a remote repository
|
||||
```
|
||||
Remote Server Your Computer
|
||||
[repo] -----------------> [local copy of repo]
|
||||
```
|
||||
|
||||
**Push**: Send your local commits to the remote
|
||||
```
|
||||
Your Computer Remote Server
|
||||
[commits] -----------------> [repo updated]
|
||||
```
|
||||
|
||||
**Pull**: Get changes from remote and merge into your branch
|
||||
```
|
||||
Remote Server Your Computer
|
||||
[commits] ---------------> [branch updated]
|
||||
```
|
||||
|
||||
**Fetch**: Get changes from remote but don't merge yet
|
||||
```
|
||||
Remote Server Your Computer
|
||||
[commits] ---------------> [stored locally, not merged]
|
||||
```
|
||||
|
||||
### Origin vs Upstream
|
||||
|
||||
- **origin**: The default name for the remote you cloned from
|
||||
- **upstream**: Often used for the original repository when you've forked it
|
||||
- You can have multiple remotes with different names
|
||||
|
||||
### Remote Tracking Branches
|
||||
|
||||
When you clone a repository, Git creates remote tracking branches:
|
||||
- `origin/main` - tracks the main branch on origin
|
||||
- `origin/feature` - tracks the feature branch on origin
|
||||
|
||||
These are read-only local copies that show the state of remote branches.
|
||||
|
||||
## Useful Commands
|
||||
|
||||
```bash
|
||||
# Clone a repository
|
||||
git clone <url>
|
||||
git clone <url> <directory-name>
|
||||
|
||||
# View remotes
|
||||
git remote
|
||||
git remote -v # Show URLs
|
||||
|
||||
# Add a remote
|
||||
git remote add <name> <url>
|
||||
|
||||
# Remove a remote
|
||||
git remote remove <name>
|
||||
|
||||
# Rename a remote
|
||||
git remote rename <old-name> <new-name>
|
||||
|
||||
# Push to remote
|
||||
git push origin <branch-name>
|
||||
git push -u origin <branch-name> # Set upstream tracking
|
||||
|
||||
# Pull from remote (fetch + merge)
|
||||
git pull
|
||||
git pull origin <branch-name>
|
||||
|
||||
# Fetch from remote (no merge)
|
||||
git fetch
|
||||
git fetch origin
|
||||
|
||||
# See remote branches
|
||||
git branch -r
|
||||
git branch -a # All branches (local and remote)
|
||||
|
||||
# Delete remote branch
|
||||
git push origin --delete <branch-name>
|
||||
|
||||
# Update remote tracking information
|
||||
git remote update
|
||||
git remote prune origin # Remove stale remote tracking branches
|
||||
```
|
||||
|
||||
## Verification
|
||||
|
||||
Run the verification script to check your solution:
|
||||
|
||||
```bash
|
||||
.\verify.ps1
|
||||
```
|
||||
|
||||
The verification will check that:
|
||||
- You cloned the repository correctly
|
||||
- Your feature branch exists and has commits
|
||||
- Changes were pushed to the remote
|
||||
- You fetched and merged remote updates
|
||||
- Your branch is up to date
|
||||
|
||||
## Challenge Steps
|
||||
|
||||
1. Navigate to the challenge directory
|
||||
2. You'll find a simulated "remote" repository
|
||||
3. Clone it: `git clone remote-repo local-repo`
|
||||
4. Navigate into your clone: `cd local-repo`
|
||||
5. Create and switch to a feature branch: `git checkout -b add-feature`
|
||||
6. Make changes to the project (add a new feature to app.js)
|
||||
7. Commit your changes
|
||||
8. Push to remote: `git push -u origin add-feature`
|
||||
9. Simulate teammate changes (run the provided update script)
|
||||
10. Fetch updates: `git fetch origin`
|
||||
11. View remote changes: `git log origin/main`
|
||||
12. Merge remote main into your branch: `git merge origin/main`
|
||||
13. Run verification
|
||||
|
||||
## Tips
|
||||
|
||||
- `git clone` automatically sets up the remote as "origin"
|
||||
- `git push -u` sets up tracking so future pushes can just use `git push`
|
||||
- Use `git fetch` to see what's changed before merging
|
||||
- `git pull` = `git fetch` + `git merge`
|
||||
- Always pull before pushing to avoid conflicts
|
||||
- Use `git branch -a` to see all local and remote branches
|
||||
- Remote branches are read-only; you work on local branches
|
||||
- `origin/main` is a remote tracking branch, `main` is your local branch
|
||||
|
||||
## Push vs Pull vs Fetch
|
||||
|
||||
### Git Push
|
||||
Uploads your local commits to the remote:
|
||||
```bash
|
||||
git push origin main
|
||||
```
|
||||
Use when: You have local commits ready to share with the team
|
||||
|
||||
### Git Pull
|
||||
Downloads and merges remote changes:
|
||||
```bash
|
||||
git pull origin main
|
||||
```
|
||||
Use when: You want to update your branch with remote changes
|
||||
Equivalent to: `git fetch origin` + `git merge origin/main`
|
||||
|
||||
### Git Fetch
|
||||
Downloads remote changes without merging:
|
||||
```bash
|
||||
git fetch origin
|
||||
```
|
||||
Use when: You want to see what's changed before merging
|
||||
Safer than pull because it doesn't automatically merge
|
||||
|
||||
## Common Remote Workflows
|
||||
|
||||
### Daily Work Flow
|
||||
```bash
|
||||
# Start of day: get latest changes
|
||||
git checkout main
|
||||
git pull origin main
|
||||
|
||||
# Create feature branch
|
||||
git checkout -b my-feature
|
||||
|
||||
# Do work, make commits
|
||||
git add .
|
||||
git commit -m "Add feature"
|
||||
|
||||
# Before pushing, update with latest main
|
||||
git checkout main
|
||||
git pull origin main
|
||||
git checkout my-feature
|
||||
git merge main
|
||||
|
||||
# Push your feature
|
||||
git push -u origin my-feature
|
||||
```
|
||||
|
||||
### Collaboration Workflow
|
||||
```bash
|
||||
# Teammate pushed changes to main
|
||||
git fetch origin
|
||||
git log origin/main # Review changes
|
||||
git merge origin/main # Merge into current branch
|
||||
|
||||
# Or use pull (fetch + merge in one step)
|
||||
git pull origin main
|
||||
```
|
||||
|
||||
### Syncing Fork (with upstream)
|
||||
```bash
|
||||
# Add original repo as upstream
|
||||
git remote add upstream <original-repo-url>
|
||||
|
||||
# Get latest from upstream
|
||||
git fetch upstream
|
||||
git checkout main
|
||||
git merge upstream/main
|
||||
|
||||
# Push to your fork
|
||||
git push origin main
|
||||
```
|
||||
|
||||
## Handling Push Rejection
|
||||
|
||||
If push is rejected because remote has changes:
|
||||
```bash
|
||||
# Remote has commits you don't have
|
||||
git push origin main
|
||||
# Error: Updates were rejected
|
||||
|
||||
# Solution 1: Pull first (creates merge commit)
|
||||
git pull origin main
|
||||
git push origin main
|
||||
|
||||
# Solution 2: Pull with rebase (cleaner history)
|
||||
git pull --rebase origin main
|
||||
git push origin main
|
||||
```
|
||||
|
||||
## What You'll Learn
|
||||
|
||||
Working with remotes is fundamental to team collaboration. Understanding the difference between local and remote branches, knowing when to push/pull/fetch, and managing synchronization are core skills for any developer. While this module uses a local "remote" for learning, the concepts apply directly to GitHub, GitLab, and other hosting services. Mastering remotes enables you to work effectively in distributed teams and contribute to open source projects.
|
||||
@@ -1,22 +0,0 @@
|
||||
#!/usr/bin/env pwsh
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Resets the remotes 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"
|
||||
@@ -1,147 +0,0 @@
|
||||
#!/usr/bin/env pwsh
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Sets up the remotes challenge environment.
|
||||
|
||||
.DESCRIPTION
|
||||
Creates a simulated remote repository and working environment
|
||||
for learning Git remote operations.
|
||||
#>
|
||||
|
||||
# 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 structure
|
||||
Write-Host "Creating challenge environment..." -ForegroundColor Cyan
|
||||
New-Item -ItemType Directory -Path "challenge" | Out-Null
|
||||
Set-Location "challenge"
|
||||
|
||||
# Create a temporary workspace to build the initial repository
|
||||
New-Item -ItemType Directory -Path "temp-workspace" | Out-Null
|
||||
Set-Location "temp-workspace"
|
||||
|
||||
# Initialize and create initial commits
|
||||
git init | Out-Null
|
||||
git config user.name "Workshop User" | Out-Null
|
||||
git config user.email "user@workshop.local" | Out-Null
|
||||
|
||||
# Create initial project files
|
||||
$app = @"
|
||||
class Application:
|
||||
def __init__(self):
|
||||
self.name = 'TeamProject'
|
||||
self.version = '1.0.0'
|
||||
|
||||
def start(self):
|
||||
print('Application started')
|
||||
"@
|
||||
|
||||
Set-Content -Path "app.py" -Value $app
|
||||
git add app.py
|
||||
git commit -m "Initial application" | Out-Null
|
||||
|
||||
$readme = @"
|
||||
# Team Project
|
||||
|
||||
A collaborative project for learning Git remotes.
|
||||
|
||||
## Features
|
||||
- Basic application structure
|
||||
"@
|
||||
|
||||
Set-Content -Path "README.md" -Value $readme
|
||||
git add README.md
|
||||
git commit -m "Add README" | Out-Null
|
||||
|
||||
$package = @"
|
||||
from setuptools import setup
|
||||
|
||||
setup(
|
||||
name='team-project',
|
||||
version='1.0.0',
|
||||
description='Learning Git remotes',
|
||||
py_modules=['app'],
|
||||
)
|
||||
"@
|
||||
|
||||
Set-Content -Path "setup.py" -Value $package
|
||||
git add setup.py
|
||||
git commit -m "Add setup.py" | Out-Null
|
||||
|
||||
# Create the "remote" repository (bare repository)
|
||||
Set-Location ..
|
||||
git clone --bare temp-workspace remote-repo 2>$null | Out-Null
|
||||
|
||||
# Clean up temp workspace
|
||||
Remove-Item -Path "temp-workspace" -Recurse -Force
|
||||
|
||||
# Create a helper script to simulate teammate changes
|
||||
$simulateTeammateScript = @"
|
||||
#!/usr/bin/env pwsh
|
||||
|
||||
# This script simulates a teammate making changes to the remote repository
|
||||
|
||||
Write-Host "Simulating teammate changes..." -ForegroundColor Cyan
|
||||
|
||||
# Create a temporary clone
|
||||
if (Test-Path "temp-teammate") {
|
||||
Remove-Item -Path "temp-teammate" -Recurse -Force
|
||||
}
|
||||
|
||||
git clone remote-repo temp-teammate 2>>`$null | Out-Null
|
||||
Set-Location temp-teammate
|
||||
|
||||
git config user.name "Teammate" 2>>`$null | Out-Null
|
||||
git config user.email "teammate@workshop.local" 2>>`$null | Out-Null
|
||||
|
||||
# Make changes to main branch
|
||||
`$appContent = Get-Content "app.py" -Raw
|
||||
`$updatedApp = `$appContent -replace "def start\(self\):", @"
|
||||
def start(self):
|
||||
print('Starting application...')
|
||||
self.initialize()
|
||||
|
||||
def initialize(self):
|
||||
"@
|
||||
|
||||
Set-Content -Path "app.py" -Value `$updatedApp
|
||||
git add app.py 2>>`$null
|
||||
git commit -m "Add initialization method" 2>>`$null | Out-Null
|
||||
git push origin main 2>>`$null | Out-Null
|
||||
|
||||
Set-Location ..
|
||||
Remove-Item -Path "temp-teammate" -Recurse -Force
|
||||
|
||||
Write-Host "Teammate pushed changes to remote repository!" -ForegroundColor Green
|
||||
Write-Host "Use 'git fetch origin' to see the changes" -ForegroundColor Cyan
|
||||
"@
|
||||
|
||||
Set-Content -Path "simulate-teammate.ps1" -Value $simulateTeammateScript
|
||||
|
||||
# Return to module directory
|
||||
Set-Location ..
|
||||
|
||||
Write-Host "`n========================================" -ForegroundColor Green
|
||||
Write-Host "Challenge environment created!" -ForegroundColor Green
|
||||
Write-Host "========================================" -ForegroundColor Green
|
||||
Write-Host "`nSetup complete! You have:" -ForegroundColor Cyan
|
||||
Write-Host "- remote-repo/ - A 'remote' repository (simulates GitHub/GitLab)" -ForegroundColor White
|
||||
Write-Host "- simulate-teammate.ps1 - Script to simulate teammate changes" -ForegroundColor White
|
||||
Write-Host "`nYour task:" -ForegroundColor Yellow
|
||||
Write-Host "1. Navigate to the challenge directory: cd challenge" -ForegroundColor White
|
||||
Write-Host "2. Clone the remote: git clone remote-repo local-repo" -ForegroundColor White
|
||||
Write-Host "3. Navigate to your clone: cd local-repo" -ForegroundColor White
|
||||
Write-Host "4. Create a feature branch: git checkout -b add-feature" -ForegroundColor White
|
||||
Write-Host "5. Add a new method to app.py (e.g., stop method)" -ForegroundColor White
|
||||
Write-Host "6. Commit your changes" -ForegroundColor White
|
||||
Write-Host "7. Push your branch: git push -u origin add-feature" -ForegroundColor White
|
||||
Write-Host "8. Go back to challenge directory: cd .." -ForegroundColor White
|
||||
Write-Host "9. Simulate teammate changes: pwsh simulate-teammate.ps1" -ForegroundColor White
|
||||
Write-Host "10. Go back to local-repo: cd local-repo" -ForegroundColor White
|
||||
Write-Host "11. Fetch updates: git fetch origin" -ForegroundColor White
|
||||
Write-Host "12. Merge remote main: git merge origin/main" -ForegroundColor White
|
||||
Write-Host "`nRun '../verify.ps1' from the challenge directory to check your solution.`n" -ForegroundColor Cyan
|
||||
@@ -1,174 +0,0 @@
|
||||
#!/usr/bin/env pwsh
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Verifies the remotes challenge solution.
|
||||
|
||||
.DESCRIPTION
|
||||
Checks that the user successfully cloned the repository, worked with
|
||||
remotes, pushed branches, and synchronized changes.
|
||||
#>
|
||||
|
||||
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 local-repo exists
|
||||
if (-not (Test-Path "local-repo")) {
|
||||
Write-Host "[FAIL] local-repo directory not found." -ForegroundColor Red
|
||||
Write-Host "Hint: Clone the remote repository with: git clone remote-repo local-repo" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
Set-Location "local-repo"
|
||||
|
||||
# Check if it's a git repository
|
||||
if (-not (Test-Path ".git")) {
|
||||
Write-Host "[FAIL] local-repo is not a git repository." -ForegroundColor Red
|
||||
Set-Location ../..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check if remote 'origin' is configured
|
||||
$remotes = git remote 2>$null
|
||||
if ($remotes -notcontains "origin") {
|
||||
Write-Host "[FAIL] No remote named 'origin' found." -ForegroundColor Red
|
||||
Write-Host "Hint: Cloning should automatically set up 'origin' as the remote" -ForegroundColor Yellow
|
||||
Set-Location ../..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check if origin points to the remote-repo
|
||||
$originUrl = git remote get-url origin 2>$null
|
||||
if ($originUrl -notmatch "remote-repo") {
|
||||
Write-Host "[FAIL] Origin remote does not point to remote-repo." -ForegroundColor Red
|
||||
Write-Host "Origin URL: $originUrl" -ForegroundColor Yellow
|
||||
Set-Location ../..
|
||||
exit 1
|
||||
}
|
||||
|
||||
Write-Host "[PASS] Repository cloned correctly with origin remote!" -ForegroundColor Green
|
||||
|
||||
# Check if add-feature branch exists locally
|
||||
$branches = git branch 2>$null
|
||||
if ($branches -notmatch "add-feature") {
|
||||
Write-Host "[FAIL] add-feature branch not found locally." -ForegroundColor Red
|
||||
Write-Host "Hint: Create the branch with: git checkout -b add-feature" -ForegroundColor Yellow
|
||||
Set-Location ../..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Switch to add-feature branch
|
||||
git checkout add-feature 2>$null | Out-Null
|
||||
|
||||
# Check if there are commits on add-feature beyond the initial commits
|
||||
$featureCommitCount = (git rev-list --count add-feature 2>$null)
|
||||
$mainCommitCount = (git rev-list --count main 2>$null)
|
||||
|
||||
if ($featureCommitCount -le $mainCommitCount) {
|
||||
Write-Host "[FAIL] add-feature branch has no new commits." -ForegroundColor Red
|
||||
Write-Host "Hint: Make changes to app.py and commit them on the add-feature branch" -ForegroundColor Yellow
|
||||
Set-Location ../..
|
||||
exit 1
|
||||
}
|
||||
|
||||
Write-Host "[PASS] Feature branch created with commits!" -ForegroundColor Green
|
||||
|
||||
# Check if add-feature branch was pushed to remote
|
||||
Set-Location ..
|
||||
Set-Location remote-repo
|
||||
|
||||
$remoteBranches = git branch 2>$null
|
||||
if ($remoteBranches -notmatch "add-feature") {
|
||||
Write-Host "[FAIL] add-feature branch not found on remote." -ForegroundColor Red
|
||||
Write-Host "Hint: Push your branch with: git push -u origin add-feature" -ForegroundColor Yellow
|
||||
Set-Location ../..
|
||||
exit 1
|
||||
}
|
||||
|
||||
Write-Host "[PASS] Feature branch pushed to remote!" -ForegroundColor Green
|
||||
|
||||
Set-Location ../local-repo
|
||||
|
||||
# Check if app.py has been modified
|
||||
if (-not (Test-Path "app.py")) {
|
||||
Write-Host "[FAIL] app.py not found." -ForegroundColor Red
|
||||
Set-Location ../..
|
||||
exit 1
|
||||
}
|
||||
|
||||
$appContent = Get-Content "app.py" -Raw
|
||||
|
||||
# Check for user's changes (should have added something)
|
||||
$featureCommits = git log --pretty=format:"%s" add-feature 2>$null
|
||||
if (-not ($featureCommits -match "stop|feature|add" -or $appContent -match "stop")) {
|
||||
Write-Host "[FAIL] No new feature detected in app.py." -ForegroundColor Red
|
||||
Write-Host "Hint: Add a new method (like 'stop') to app.py and commit it" -ForegroundColor Yellow
|
||||
Set-Location ../..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check if teammate changes were fetched and merged
|
||||
# The teammate's change adds an 'initialize' method
|
||||
if ($appContent -notmatch "initialize") {
|
||||
Write-Host "[FAIL] Teammate's changes not merged into your branch." -ForegroundColor Red
|
||||
Write-Host "Hint: Did you run the simulate-teammate.ps1 script?" -ForegroundColor Yellow
|
||||
Write-Host " Then: git fetch origin" -ForegroundColor Yellow
|
||||
Write-Host " git merge origin/main" -ForegroundColor Yellow
|
||||
Set-Location ../..
|
||||
exit 1
|
||||
}
|
||||
|
||||
Write-Host "[PASS] Remote changes fetched and merged!" -ForegroundColor Green
|
||||
|
||||
# Check that the branch has both sets of changes
|
||||
$allCommits = git log --all --pretty=format:"%s" 2>$null
|
||||
$hasUserCommit = $allCommits -match "stop|feature|add"
|
||||
$hasTeammateCommit = $allCommits -match "initialization|initialize"
|
||||
|
||||
if (-not $hasTeammateCommit) {
|
||||
Write-Host "[WARNING] Teammate commit not found in history." -ForegroundColor Yellow
|
||||
Write-Host "Make sure you ran simulate-teammate.ps1 and fetched the changes" -ForegroundColor Yellow
|
||||
}
|
||||
|
||||
# Check for remote tracking
|
||||
$tracking = git branch -vv 2>$null
|
||||
if ($tracking -notmatch "origin/add-feature") {
|
||||
Write-Host "[WARNING] add-feature branch may not be tracking origin/add-feature" -ForegroundColor Yellow
|
||||
Write-Host "Hint: Use 'git push -u origin add-feature' to set up tracking" -ForegroundColor Yellow
|
||||
}
|
||||
|
||||
# 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 "- Cloned the remote repository" -ForegroundColor White
|
||||
Write-Host "- Created a feature branch" -ForegroundColor White
|
||||
Write-Host "- Made commits to your branch" -ForegroundColor White
|
||||
Write-Host "- Pushed your branch to the remote" -ForegroundColor White
|
||||
Write-Host "- Fetched changes from the remote" -ForegroundColor White
|
||||
Write-Host "- Merged remote changes into your branch" -ForegroundColor White
|
||||
Write-Host "`nYou now understand how to work with Git remotes!" -ForegroundColor Green
|
||||
Write-Host "`nKey takeaways:" -ForegroundColor Yellow
|
||||
Write-Host "- Clone creates a local copy linked to the remote" -ForegroundColor White
|
||||
Write-Host "- Push uploads your commits to the remote" -ForegroundColor White
|
||||
Write-Host "- Fetch downloads remote changes without merging" -ForegroundColor White
|
||||
Write-Host "- Pull = Fetch + Merge" -ForegroundColor White
|
||||
Write-Host "`nThese skills are essential for team collaboration!`n" -ForegroundColor Green
|
||||
|
||||
Set-Location ../..
|
||||
exit 0
|
||||
@@ -1,208 +0,0 @@
|
||||
# Module 13: Worktrees
|
||||
|
||||
## Learning Objectives
|
||||
|
||||
By the end of this module, you will:
|
||||
- Understand what Git worktrees are and when to use them
|
||||
- Create and manage multiple working directories for the same repository
|
||||
- Work on multiple branches simultaneously
|
||||
- Understand the benefits of worktrees over stashing or cloning
|
||||
- Remove and clean up worktrees
|
||||
|
||||
## Challenge Description
|
||||
|
||||
You're working on a feature when an urgent bug report comes in. Instead of stashing your work or creating a separate clone, you'll use Git worktrees to work on both the feature and the bugfix simultaneously in different directories.
|
||||
|
||||
Your task is to:
|
||||
1. Create a worktree for the bugfix on a separate branch
|
||||
2. Fix the bug in the worktree
|
||||
3. Commit and verify the fix
|
||||
4. Continue working on your feature in the main working directory
|
||||
5. Clean up the worktree when done
|
||||
|
||||
## Key Concepts
|
||||
|
||||
### What are Git Worktrees?
|
||||
|
||||
A worktree is an additional working directory attached to the same repository. Each worktree can have a different branch checked out, allowing you to work on multiple branches simultaneously without switching.
|
||||
|
||||
### Traditional Workflow vs Worktrees
|
||||
|
||||
**Traditional (switching branches):**
|
||||
```
|
||||
main-repo/
|
||||
- Switch to bugfix branch
|
||||
- Fix bug
|
||||
- Switch back to feature branch
|
||||
- Continue feature work
|
||||
- (Requires stashing or committing incomplete work)
|
||||
```
|
||||
|
||||
**With Worktrees:**
|
||||
```
|
||||
main-repo/ <- feature branch
|
||||
worktrees/bugfix/ <- bugfix branch
|
||||
|
||||
Work in both simultaneously!
|
||||
```
|
||||
|
||||
### Why Use Worktrees?
|
||||
|
||||
**Advantages:**
|
||||
- Work on multiple branches at the same time
|
||||
- No need to stash or commit incomplete work
|
||||
- Each worktree has its own working directory and index
|
||||
- Share the same Git history (one `.git` directory)
|
||||
- Faster than cloning the entire repository
|
||||
- Perfect for code reviews, comparisons, or parallel development
|
||||
|
||||
**Use Cases:**
|
||||
- Urgent bug fixes while working on a feature
|
||||
- Code reviews (checkout PR in separate worktree)
|
||||
- Comparing implementations side by side
|
||||
- Running tests on one branch while coding on another
|
||||
- Building different versions simultaneously
|
||||
|
||||
## Useful Commands
|
||||
|
||||
```bash
|
||||
# List all worktrees
|
||||
git worktree list
|
||||
|
||||
# Add a new worktree
|
||||
git worktree add <path> <branch>
|
||||
git worktree add ../bugfix bugfix-branch
|
||||
|
||||
# Create new branch in worktree
|
||||
git worktree add <path> -b <new-branch>
|
||||
git worktree add ../feature-new -b feature-new
|
||||
|
||||
# Remove a worktree
|
||||
git worktree remove <path>
|
||||
git worktree remove ../bugfix
|
||||
|
||||
# Prune stale worktree information
|
||||
git worktree prune
|
||||
|
||||
# Move a worktree
|
||||
git worktree move <old-path> <new-path>
|
||||
|
||||
# Lock a worktree (prevent deletion)
|
||||
git worktree lock <path>
|
||||
git worktree unlock <path>
|
||||
```
|
||||
|
||||
## Verification
|
||||
|
||||
Run the verification script to check your solution:
|
||||
|
||||
```bash
|
||||
.\verify.ps1
|
||||
```
|
||||
|
||||
The verification will check that:
|
||||
- You created a worktree for the bugfix
|
||||
- The bug was fixed and committed in the worktree
|
||||
- Your feature work continued in the main directory
|
||||
- Both branches have the expected changes
|
||||
|
||||
## Challenge Steps
|
||||
|
||||
1. Navigate to the challenge directory
|
||||
2. You're in main-repo with a feature branch checked out
|
||||
3. View current worktrees: `git worktree list`
|
||||
4. Create a worktree for bugfix: `git worktree add ../bugfix-worktree -b bugfix`
|
||||
5. Navigate to the worktree: `cd ../bugfix-worktree`
|
||||
6. Fix the bug in calculator.js (fix the divide by zero check)
|
||||
7. Commit the fix: `git add . && git commit -m "Fix divide by zero bug"`
|
||||
8. Go back to main repo: `cd ../main-repo`
|
||||
9. Continue working on your feature
|
||||
10. Add a new method to calculator.js
|
||||
11. Commit your feature
|
||||
12. List worktrees: `git worktree list`
|
||||
13. Remove the worktree: `git worktree remove ../bugfix-worktree`
|
||||
14. Run verification
|
||||
|
||||
## Tips
|
||||
|
||||
- Worktree paths are typically siblings of your main repo (use `../worktree-name`)
|
||||
- Each worktree must have a different branch checked out
|
||||
- Can't checkout the same branch in multiple worktrees
|
||||
- The main `.git` directory is shared, so commits in any worktree are visible everywhere
|
||||
- Worktrees are listed in `.git/worktrees/`
|
||||
- Use `git worktree remove` to clean up, or just delete the directory and run `git worktree prune`
|
||||
- Worktrees persist across restarts until explicitly removed
|
||||
|
||||
## Common Worktree Workflows
|
||||
|
||||
### Urgent Bugfix
|
||||
```bash
|
||||
# Currently on feature branch with uncommitted changes
|
||||
git worktree add ../hotfix -b hotfix
|
||||
|
||||
cd ../hotfix
|
||||
# Fix the bug
|
||||
git add .
|
||||
git commit -m "Fix critical bug"
|
||||
git push origin hotfix
|
||||
|
||||
cd ../main-repo
|
||||
# Continue working on feature
|
||||
```
|
||||
|
||||
### Code Review
|
||||
```bash
|
||||
# Review a pull request without switching branches
|
||||
git fetch origin pull/123/head:pr-123
|
||||
git worktree add ../review-pr-123 pr-123
|
||||
|
||||
cd ../review-pr-123
|
||||
# Review code, test it
|
||||
# Run: npm test, npm start, etc.
|
||||
|
||||
cd ../main-repo
|
||||
git worktree remove ../review-pr-123
|
||||
```
|
||||
|
||||
### Parallel Development
|
||||
```bash
|
||||
# Work on two features simultaneously
|
||||
git worktree add ../feature-a -b feature-a
|
||||
git worktree add ../feature-b -b feature-b
|
||||
|
||||
# Terminal 1
|
||||
cd feature-a && code .
|
||||
|
||||
# Terminal 2
|
||||
cd feature-b && code .
|
||||
```
|
||||
|
||||
### Build Comparison
|
||||
```bash
|
||||
# Compare builds between branches
|
||||
git worktree add ../release-build release-v2.0
|
||||
|
||||
cd ../release-build
|
||||
npm run build
|
||||
# Test production build
|
||||
|
||||
# Meanwhile, continue development in main repo
|
||||
```
|
||||
|
||||
## Worktree vs Other Approaches
|
||||
|
||||
### vs Stashing
|
||||
- **Stash**: Temporary, one at a time, requires branch switching
|
||||
- **Worktree**: Persistent, multiple simultaneously, no switching
|
||||
|
||||
### vs Cloning
|
||||
- **Clone**: Full copy, separate `.git`, uses more disk space
|
||||
- **Worktree**: Shared `.git`, less disk space, instant sync
|
||||
|
||||
### vs Branch Switching
|
||||
- **Switching**: Requires clean working directory, one branch at a time
|
||||
- **Worktree**: Keep dirty working directory, multiple branches active
|
||||
|
||||
## What You'll Learn
|
||||
|
||||
Git worktrees are a powerful but underutilized feature that can significantly improve your workflow. They eliminate the need for constant branch switching, stashing, or maintaining multiple clones. Whether you're handling urgent fixes, reviewing code, or comparing implementations, worktrees provide a clean and efficient solution. Once you understand worktrees, you'll find many situations where they're the perfect tool for the job.
|
||||
@@ -1,22 +0,0 @@
|
||||
#!/usr/bin/env pwsh
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Resets the worktrees 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"
|
||||
@@ -1,123 +0,0 @@
|
||||
#!/usr/bin/env pwsh
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Sets up the worktrees challenge environment.
|
||||
|
||||
.DESCRIPTION
|
||||
Creates a Git repository with a feature in progress, ready for
|
||||
demonstrating the use of worktrees for parallel work.
|
||||
#>
|
||||
|
||||
# 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"
|
||||
|
||||
# Create main repository
|
||||
New-Item -ItemType Directory -Path "main-repo" | Out-Null
|
||||
Set-Location "main-repo"
|
||||
|
||||
# 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 calculator with a bug
|
||||
$calculator = @"
|
||||
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: No division by zero check!
|
||||
def divide(self, a, b):
|
||||
return a / b
|
||||
"@
|
||||
|
||||
Set-Content -Path "calculator.py" -Value $calculator
|
||||
git add calculator.py
|
||||
git commit -m "Initial calculator implementation" | Out-Null
|
||||
|
||||
$readme = @"
|
||||
# Calculator Project
|
||||
|
||||
A simple calculator with basic operations.
|
||||
|
||||
## Features
|
||||
- Addition
|
||||
- Subtraction
|
||||
- Multiplication
|
||||
- Division (has a bug!)
|
||||
"@
|
||||
|
||||
Set-Content -Path "README.md" -Value $readme
|
||||
git add README.md
|
||||
git commit -m "Add README" | Out-Null
|
||||
|
||||
# Create feature branch and start working on it
|
||||
git checkout -b feature-advanced-math | Out-Null
|
||||
|
||||
# Add work in progress on feature branch
|
||||
$calculatorWithFeature = @"
|
||||
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: No division by zero check!
|
||||
def divide(self, a, b):
|
||||
return a / b
|
||||
|
||||
# New feature: power function (work in progress)
|
||||
def power(self, a, b):
|
||||
return a ** b
|
||||
|
||||
# TODO: Add square root function
|
||||
# TODO: Add logarithm function
|
||||
"@
|
||||
|
||||
Set-Content -Path "calculator.py" -Value $calculatorWithFeature
|
||||
git add calculator.py
|
||||
git commit -m "Add power function (WIP: more math functions coming)" | Out-Null
|
||||
|
||||
# Return to challenge directory
|
||||
Set-Location ..
|
||||
|
||||
Write-Host "`n========================================" -ForegroundColor Green
|
||||
Write-Host "Challenge environment created!" -ForegroundColor Green
|
||||
Write-Host "========================================" -ForegroundColor Green
|
||||
Write-Host "`nSituation:" -ForegroundColor Cyan
|
||||
Write-Host "You're working on the 'feature-advanced-math' branch" -ForegroundColor White
|
||||
Write-Host "You have plans to add more math functions (see TODOs)" -ForegroundColor White
|
||||
Write-Host "`nUrgent: A critical bug was discovered in the divide function!" -ForegroundColor Red
|
||||
Write-Host "It doesn't check for division by zero." -ForegroundColor Red
|
||||
Write-Host "`nInstead of stashing your feature work, use a worktree:" -ForegroundColor Yellow
|
||||
Write-Host "`nYour task:" -ForegroundColor Yellow
|
||||
Write-Host "1. Navigate to main-repo: cd challenge/main-repo" -ForegroundColor White
|
||||
Write-Host "2. Create a worktree: git worktree add ../bugfix-worktree -b bugfix" -ForegroundColor White
|
||||
Write-Host "3. Go to worktree: cd ../bugfix-worktree" -ForegroundColor White
|
||||
Write-Host "4. Fix the bug in calculator.py:" -ForegroundColor White
|
||||
Write-Host " Add a check: if (b === 0) throw new Error('Division by zero');" -ForegroundColor White
|
||||
Write-Host "5. Commit the fix: git add . && git commit -m 'Fix divide by zero bug'" -ForegroundColor White
|
||||
Write-Host "6. Return to main-repo: cd ../main-repo" -ForegroundColor White
|
||||
Write-Host "7. Complete your feature: Add square root method to calculator.py" -ForegroundColor White
|
||||
Write-Host "8. Commit: git add . && git commit -m 'Add square root function'" -ForegroundColor White
|
||||
Write-Host "9. Clean up worktree: git worktree remove ../bugfix-worktree" -ForegroundColor White
|
||||
Write-Host "`nRun '../verify.ps1' from the challenge directory to check your solution.`n" -ForegroundColor Cyan
|
||||
@@ -1,165 +0,0 @@
|
||||
#!/usr/bin/env pwsh
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Verifies the worktrees challenge solution.
|
||||
|
||||
.DESCRIPTION
|
||||
Checks that the user successfully used worktrees to fix a bug
|
||||
while continuing work on a feature.
|
||||
#>
|
||||
|
||||
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 main-repo exists
|
||||
if (-not (Test-Path "main-repo")) {
|
||||
Write-Host "[FAIL] main-repo directory not found." -ForegroundColor Red
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
Set-Location "main-repo"
|
||||
|
||||
# Check if it's a git repository
|
||||
if (-not (Test-Path ".git")) {
|
||||
Write-Host "[FAIL] main-repo is not a git repository." -ForegroundColor Red
|
||||
Set-Location ../..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check if bugfix branch exists
|
||||
$branches = git branch --all 2>$null
|
||||
if ($branches -notmatch "bugfix") {
|
||||
Write-Host "[FAIL] bugfix branch not found." -ForegroundColor Red
|
||||
Write-Host "Hint: Create a worktree with: git worktree add ../bugfix-worktree -b bugfix" -ForegroundColor Yellow
|
||||
Set-Location ../..
|
||||
exit 1
|
||||
}
|
||||
|
||||
Write-Host "[PASS] Bugfix branch exists!" -ForegroundColor Green
|
||||
|
||||
# Check bugfix branch for the fix
|
||||
git checkout bugfix 2>$null | Out-Null
|
||||
|
||||
if (-not (Test-Path "calculator.py")) {
|
||||
Write-Host "[FAIL] calculator.py not found on bugfix branch." -ForegroundColor Red
|
||||
Set-Location ../..
|
||||
exit 1
|
||||
}
|
||||
|
||||
$bugfixCalc = Get-Content "calculator.py" -Raw
|
||||
|
||||
# Check if division by zero check was added
|
||||
if ($bugfixCalc -notmatch "b === 0|b == 0|division by zero|divide by zero") {
|
||||
Write-Host "[FAIL] Division by zero check not found in bugfix branch." -ForegroundColor Red
|
||||
Write-Host "Hint: Add a check in the divide method to prevent division by zero" -ForegroundColor Yellow
|
||||
Set-Location ../..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check for commit on bugfix branch
|
||||
$bugfixCommits = git log --pretty=format:"%s" bugfix 2>$null
|
||||
if ($bugfixCommits -notmatch "bug|fix|division|divide") {
|
||||
Write-Host "[FAIL] No bugfix commit found on bugfix branch." -ForegroundColor Red
|
||||
Write-Host "Hint: Commit your fix with a descriptive message" -ForegroundColor Yellow
|
||||
Set-Location ../..
|
||||
exit 1
|
||||
}
|
||||
|
||||
Write-Host "[PASS] Bug fixed on bugfix branch!" -ForegroundColor Green
|
||||
|
||||
# Check feature branch for continued work
|
||||
git checkout feature-advanced-math 2>$null | Out-Null
|
||||
|
||||
$featureCalc = Get-Content "calculator.py" -Raw
|
||||
|
||||
# Check if square root function was added
|
||||
if ($featureCalc -notmatch "sqrt|squareRoot") {
|
||||
Write-Host "[FAIL] Square root function not found on feature branch." -ForegroundColor Red
|
||||
Write-Host "Hint: Add the square root method to calculator.py on feature-advanced-math branch" -ForegroundColor Yellow
|
||||
Set-Location ../..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check that feature work was committed
|
||||
$featureCommits = git log --pretty=format:"%s" feature-advanced-math 2>$null
|
||||
$featureCommitArray = $featureCommits -split "`n"
|
||||
|
||||
# Should have at least 3 commits: initial + README + power + sqrt
|
||||
if ($featureCommitArray.Count -lt 4) {
|
||||
Write-Host "[FAIL] Not enough commits on feature branch." -ForegroundColor Red
|
||||
Write-Host "Expected: initial, README, power, and square root commits" -ForegroundColor Yellow
|
||||
Write-Host "Found $($featureCommitArray.Count) commits" -ForegroundColor Yellow
|
||||
Set-Location ../..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check if the latest feature commit is about square root
|
||||
if ($featureCommitArray[0] -notmatch "sqrt|square|root") {
|
||||
Write-Host "[FAIL] Latest commit on feature branch should be about square root." -ForegroundColor Red
|
||||
Write-Host "Latest commit: $($featureCommitArray[0])" -ForegroundColor Yellow
|
||||
Set-Location ../..
|
||||
exit 1
|
||||
}
|
||||
|
||||
Write-Host "[PASS] Feature work completed!" -ForegroundColor Green
|
||||
|
||||
# Check if worktree was cleaned up (bugfix-worktree should not exist or be removed)
|
||||
Set-Location ..
|
||||
$worktreeStillExists = Test-Path "bugfix-worktree"
|
||||
|
||||
if ($worktreeStillExists) {
|
||||
Write-Host "[WARNING] bugfix-worktree directory still exists." -ForegroundColor Yellow
|
||||
Write-Host "Hint: Clean up with: git worktree remove ../bugfix-worktree" -ForegroundColor Yellow
|
||||
# Don't fail on this, just warn
|
||||
}
|
||||
|
||||
# Check worktree list
|
||||
Set-Location "main-repo"
|
||||
$worktrees = git worktree list 2>$null
|
||||
|
||||
# Verify that the concept was understood (they should have created the worktree at some point)
|
||||
# We can check this by looking for the bugfix branch existence
|
||||
if ($branches -notmatch "bugfix") {
|
||||
Write-Host "[FAIL] No evidence of worktree usage." -ForegroundColor Red
|
||||
Set-Location ../..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# 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 "- Created a worktree for the bugfix" -ForegroundColor White
|
||||
Write-Host "- Fixed the division by zero bug" -ForegroundColor White
|
||||
Write-Host "- Committed the fix on the bugfix branch" -ForegroundColor White
|
||||
Write-Host "- Continued feature work in parallel" -ForegroundColor White
|
||||
Write-Host "- Added the square root function" -ForegroundColor White
|
||||
Write-Host "- Committed the feature work" -ForegroundColor White
|
||||
|
||||
if (-not $worktreeStillExists) {
|
||||
Write-Host "- Cleaned up the worktree" -ForegroundColor White
|
||||
}
|
||||
|
||||
Write-Host "`nYou now understand Git worktrees!" -ForegroundColor Green
|
||||
Write-Host "`nKey takeaway:" -ForegroundColor Yellow
|
||||
Write-Host "Worktrees let you work on multiple branches simultaneously" -ForegroundColor White
|
||||
Write-Host "without stashing, switching, or cloning the repository.`n" -ForegroundColor White
|
||||
|
||||
Set-Location ../..
|
||||
exit 0
|
||||
@@ -1,231 +0,0 @@
|
||||
# Module 14: Bisect - Finding Bugs with Binary Search
|
||||
|
||||
## Learning Objectives
|
||||
|
||||
By the end of this module, you will:
|
||||
- Understand what git bisect is and when to use it
|
||||
- Use binary search to find the commit that introduced a bug
|
||||
- Mark commits as good or bad during bisection
|
||||
- Automate bisect with test scripts
|
||||
- Understand the efficiency of binary search for debugging
|
||||
|
||||
## Challenge Description
|
||||
|
||||
A bug has appeared in your calculator application, but you don't know which commit introduced it. The project has many commits, and manually checking each one would take too long. You'll use `git bisect` to efficiently find the culprit commit using binary search.
|
||||
|
||||
Your task is to:
|
||||
1. Start a bisect session
|
||||
2. Mark the current commit as bad (bug exists)
|
||||
3. Mark an old commit as good (bug didn't exist)
|
||||
4. Test commits and mark them good or bad
|
||||
5. Let Git find the first bad commit
|
||||
6. Identify what change introduced the bug
|
||||
|
||||
## Key Concepts
|
||||
|
||||
### What is Git Bisect?
|
||||
|
||||
Git bisect uses binary search to find the commit that introduced a bug. Instead of checking every commit linearly, it cuts the search space in half with each test, making it extremely efficient.
|
||||
|
||||
### Binary Search Efficiency
|
||||
|
||||
**Linear Search (manual checking):**
|
||||
- 100 commits = up to 100 tests
|
||||
- 1000 commits = up to 1000 tests
|
||||
|
||||
**Binary Search (bisect):**
|
||||
- 100 commits = ~7 tests
|
||||
- 1000 commits = ~10 tests
|
||||
|
||||
Formula: log₂(n) tests needed for n commits
|
||||
|
||||
### How Bisect Works
|
||||
|
||||
```
|
||||
Commits: A---B---C---D---E---F---G---H
|
||||
✓ ✓ ✓ ? ? ? ? ✗
|
||||
|
||||
1. Start: Mark H (bad) and A (good)
|
||||
2. Git checks middle: E
|
||||
3. You test E: bad ✗
|
||||
|
||||
Commits: A---B---C---D---E
|
||||
✓ ✓ ✓ ? ✗
|
||||
|
||||
4. Git checks middle: C
|
||||
5. You test C: good ✓
|
||||
|
||||
Commits: C---D---E
|
||||
✓ ? ✗
|
||||
|
||||
6. Git checks: D
|
||||
7. You test D: bad ✗
|
||||
|
||||
Result: D is the first bad commit!
|
||||
```
|
||||
|
||||
### When to Use Bisect
|
||||
|
||||
Use bisect when:
|
||||
- You know a bug exists now but didn't exist in the past
|
||||
- You have many commits to check
|
||||
- You can reliably test for the bug
|
||||
- You want to find exactly when something broke
|
||||
|
||||
## Useful Commands
|
||||
|
||||
```bash
|
||||
# Start bisect session
|
||||
git bisect start
|
||||
|
||||
# Mark current commit as bad
|
||||
git bisect bad
|
||||
|
||||
# Mark a commit as good
|
||||
git bisect good <commit-hash>
|
||||
git bisect good HEAD~10
|
||||
|
||||
# After testing current commit
|
||||
git bisect good # This commit is fine
|
||||
git bisect bad # This commit has the bug
|
||||
|
||||
# Skip a commit (if you can't test it)
|
||||
git bisect skip
|
||||
|
||||
# End bisect session and return to original state
|
||||
git bisect reset
|
||||
|
||||
# Visualize bisect process
|
||||
git bisect visualize
|
||||
git bisect view
|
||||
|
||||
# Automate with a test script
|
||||
git bisect run <test-script>
|
||||
git bisect run npm test
|
||||
git bisect run ./test.sh
|
||||
|
||||
# Show bisect log
|
||||
git bisect log
|
||||
```
|
||||
|
||||
## Verification
|
||||
|
||||
Run the verification script to check your solution:
|
||||
|
||||
```bash
|
||||
.\verify.ps1
|
||||
```
|
||||
|
||||
The verification will check that:
|
||||
- You completed a bisect session
|
||||
- You identified the correct commit that introduced the bug
|
||||
- You understand which change caused the problem
|
||||
|
||||
## Challenge Steps
|
||||
|
||||
1. Navigate to the challenge directory
|
||||
2. View the bug: run the calculator and see it fails
|
||||
3. Start bisect: `git bisect start`
|
||||
4. Mark current as bad: `git bisect bad`
|
||||
5. Mark old commit as good: `git bisect good HEAD~10`
|
||||
6. Git will checkout a middle commit
|
||||
7. Test the current commit (run the test or check manually)
|
||||
8. Mark it: `git bisect good` or `git bisect bad`
|
||||
9. Repeat testing until Git identifies the bad commit
|
||||
10. Note the commit hash and message
|
||||
11. End bisect: `git bisect reset`
|
||||
12. Check the identified commit: `git show <bad-commit-hash>`
|
||||
13. Create a file named `bug-commit.txt` with the bad commit hash
|
||||
14. Run verification
|
||||
|
||||
## Tips
|
||||
|
||||
- Always start with a known good commit (far enough back)
|
||||
- Keep a clear way to test each commit (script or manual steps)
|
||||
- Use `git bisect log` to see your progress
|
||||
- `git bisect reset` returns you to your original state
|
||||
- You can bisect on any criteria, not just bugs (performance, features, etc.)
|
||||
- Automate with `git bisect run` for faster results
|
||||
- Each bisect step cuts remaining commits in half
|
||||
- Skip commits you can't build/test with `git bisect skip`
|
||||
|
||||
## Manual vs Automated Bisect
|
||||
|
||||
### Manual Bisect
|
||||
```bash
|
||||
git bisect start
|
||||
git bisect bad
|
||||
git bisect good HEAD~20
|
||||
|
||||
# For each commit Git checks out:
|
||||
npm test
|
||||
git bisect good # or bad
|
||||
|
||||
git bisect reset
|
||||
```
|
||||
|
||||
### Automated Bisect
|
||||
```bash
|
||||
git bisect start
|
||||
git bisect bad
|
||||
git bisect good HEAD~20
|
||||
git bisect run npm test
|
||||
# Git automatically tests each commit
|
||||
git bisect reset
|
||||
```
|
||||
|
||||
The test script should exit with:
|
||||
- 0 for good (test passes)
|
||||
- 1-127 (except 125) for bad (test fails)
|
||||
- 125 for skip (can't test this commit)
|
||||
|
||||
## Bisect Workflow Example
|
||||
|
||||
### Finding a Performance Regression
|
||||
```bash
|
||||
# App is slow now, was fast 50 commits ago
|
||||
git bisect start
|
||||
git bisect bad
|
||||
git bisect good HEAD~50
|
||||
|
||||
# Create test script
|
||||
echo '#!/bin/bash\ntime npm start | grep "Started in"' > test.sh
|
||||
chmod +x test.sh
|
||||
|
||||
git bisect run ./test.sh
|
||||
# Git finds the commit that made it slow
|
||||
```
|
||||
|
||||
### Finding When a Feature Broke
|
||||
```bash
|
||||
git bisect start
|
||||
git bisect bad
|
||||
git bisect good v1.0.0 # Last known good version
|
||||
|
||||
# For each commit
|
||||
npm test -- user-login.test.js
|
||||
git bisect good # or bad
|
||||
|
||||
# Found! Commit abc123 broke login
|
||||
git show abc123
|
||||
```
|
||||
|
||||
## Common Bisect Pitfalls
|
||||
|
||||
### Pitfall 1: Testing Incorrectly
|
||||
- Make sure your test is consistent
|
||||
- Automate when possible to avoid human error
|
||||
- Use the same test for every commit
|
||||
|
||||
### Pitfall 2: Wrong Good Commit
|
||||
- If the "good" commit actually has the bug, bisect will fail
|
||||
- Choose a commit you're confident was working
|
||||
|
||||
### Pitfall 3: Multiple Bugs
|
||||
- Bisect finds one commit at a time
|
||||
- If multiple bugs exist, they might confuse the search
|
||||
- Fix found bugs and bisect again for others
|
||||
|
||||
## What You'll Learn
|
||||
|
||||
Git bisect is a powerful debugging tool that turns a tedious manual search into an efficient automated process. By leveraging binary search, you can quickly pinpoint problematic commits even in repositories with thousands of commits. This is invaluable for debugging regressions, performance issues, or any situation where something that worked before is now broken. Mastering bisect makes you a more effective debugger and shows deep Git proficiency.
|
||||
@@ -1,22 +0,0 @@
|
||||
#!/usr/bin/env pwsh
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Resets the bisect 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"
|
||||
@@ -1,311 +0,0 @@
|
||||
#!/usr/bin/env pwsh
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Sets up the bisect challenge environment.
|
||||
|
||||
.DESCRIPTION
|
||||
Creates a Git repository with multiple commits where a bug is
|
||||
introduced in one of them. Students use bisect to find it.
|
||||
#>
|
||||
|
||||
# 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
|
||||
|
||||
# Commit 1: Initial calculator
|
||||
$calc1 = @"
|
||||
class Calculator:
|
||||
def add(self, a, b):
|
||||
return a + b
|
||||
"@
|
||||
Set-Content -Path "calculator.py" -Value $calc1
|
||||
git add calculator.py
|
||||
git commit -m "Initial calculator with add function" | Out-Null
|
||||
|
||||
# Commit 2: Add subtract
|
||||
$calc2 = @"
|
||||
class Calculator:
|
||||
def add(self, a, b):
|
||||
return a + b
|
||||
|
||||
def subtract(self, a, b):
|
||||
return a - b
|
||||
"@
|
||||
Set-Content -Path "calculator.py" -Value $calc2
|
||||
git add calculator.py
|
||||
git commit -m "Add subtract function" | Out-Null
|
||||
|
||||
# Commit 3: Add multiply
|
||||
$calc3 = @"
|
||||
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 $calc3
|
||||
git add calculator.py
|
||||
git commit -m "Add multiply function" | Out-Null
|
||||
|
||||
# Commit 4: Add divide
|
||||
$calc4 = @"
|
||||
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
|
||||
|
||||
def divide(self, a, b):
|
||||
if b == 0:
|
||||
raise ValueError('Division by zero')
|
||||
return a / b
|
||||
"@
|
||||
Set-Content -Path "calculator.py" -Value $calc4
|
||||
git add calculator.py
|
||||
git commit -m "Add divide function" | Out-Null
|
||||
|
||||
# Commit 5: Add modulo
|
||||
$calc5 = @"
|
||||
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
|
||||
|
||||
def divide(self, a, b):
|
||||
if b == 0:
|
||||
raise ValueError('Division by zero')
|
||||
return a / b
|
||||
|
||||
def modulo(self, a, b):
|
||||
return a % b
|
||||
"@
|
||||
Set-Content -Path "calculator.py" -Value $calc5
|
||||
git add calculator.py
|
||||
git commit -m "Add modulo function" | Out-Null
|
||||
|
||||
# Commit 6: BUG - Introduce error in add function
|
||||
$calc6 = @"
|
||||
class Calculator:
|
||||
def add(self, a, b):
|
||||
return a - b # BUG: Should be a + b
|
||||
|
||||
def subtract(self, a, b):
|
||||
return a - b
|
||||
|
||||
def multiply(self, a, b):
|
||||
return a * b
|
||||
|
||||
def divide(self, a, b):
|
||||
if b == 0:
|
||||
raise ValueError('Division by zero')
|
||||
return a / b
|
||||
|
||||
def modulo(self, a, b):
|
||||
return a % b
|
||||
"@
|
||||
Set-Content -Path "calculator.py" -Value $calc6
|
||||
git add calculator.py
|
||||
git commit -m "Refactor add function for clarity" | Out-Null
|
||||
|
||||
# Commit 7: Add power function (bug still exists)
|
||||
$calc7 = @"
|
||||
class Calculator:
|
||||
def add(self, a, b):
|
||||
return a - b # BUG: Should be a + b
|
||||
|
||||
def subtract(self, a, b):
|
||||
return a - b
|
||||
|
||||
def multiply(self, a, b):
|
||||
return a * b
|
||||
|
||||
def divide(self, a, b):
|
||||
if b == 0:
|
||||
raise ValueError('Division by zero')
|
||||
return a / b
|
||||
|
||||
def modulo(self, a, b):
|
||||
return a % b
|
||||
|
||||
def power(self, a, b):
|
||||
return a ** b
|
||||
"@
|
||||
Set-Content -Path "calculator.py" -Value $calc7
|
||||
git add calculator.py
|
||||
git commit -m "Add power function" | Out-Null
|
||||
|
||||
# Commit 8: Add square root
|
||||
$calc8 = @"
|
||||
import math
|
||||
|
||||
class Calculator:
|
||||
def add(self, a, b):
|
||||
return a - b # BUG: Should be a + b
|
||||
|
||||
def subtract(self, a, b):
|
||||
return a - b
|
||||
|
||||
def multiply(self, a, b):
|
||||
return a * b
|
||||
|
||||
def divide(self, a, b):
|
||||
if b == 0:
|
||||
raise ValueError('Division by zero')
|
||||
return a / b
|
||||
|
||||
def modulo(self, a, b):
|
||||
return a % b
|
||||
|
||||
def power(self, a, b):
|
||||
return a ** b
|
||||
|
||||
def sqrt(self, a):
|
||||
return math.sqrt(a)
|
||||
"@
|
||||
Set-Content -Path "calculator.py" -Value $calc8
|
||||
git add calculator.py
|
||||
git commit -m "Add square root function" | Out-Null
|
||||
|
||||
# Commit 9: Add absolute value
|
||||
$calc9 = @"
|
||||
import math
|
||||
|
||||
class Calculator:
|
||||
def add(self, a, b):
|
||||
return a - b # BUG: Should be a + b
|
||||
|
||||
def subtract(self, a, b):
|
||||
return a - b
|
||||
|
||||
def multiply(self, a, b):
|
||||
return a * b
|
||||
|
||||
def divide(self, a, b):
|
||||
if b == 0:
|
||||
raise ValueError('Division by zero')
|
||||
return a / b
|
||||
|
||||
def modulo(self, a, b):
|
||||
return a % b
|
||||
|
||||
def power(self, a, b):
|
||||
return a ** b
|
||||
|
||||
def sqrt(self, a):
|
||||
return math.sqrt(a)
|
||||
|
||||
def abs(self, a):
|
||||
return abs(a)
|
||||
"@
|
||||
Set-Content -Path "calculator.py" -Value $calc9
|
||||
git add calculator.py
|
||||
git commit -m "Add absolute value function" | Out-Null
|
||||
|
||||
# Commit 10: Add max function
|
||||
$calc10 = @"
|
||||
import math
|
||||
|
||||
class Calculator:
|
||||
def add(self, a, b):
|
||||
return a - b # BUG: Should be a + b
|
||||
|
||||
def subtract(self, a, b):
|
||||
return a - b
|
||||
|
||||
def multiply(self, a, b):
|
||||
return a * b
|
||||
|
||||
def divide(self, a, b):
|
||||
if b == 0:
|
||||
raise ValueError('Division by zero')
|
||||
return a / b
|
||||
|
||||
def modulo(self, a, b):
|
||||
return a % b
|
||||
|
||||
def power(self, a, b):
|
||||
return a ** b
|
||||
|
||||
def sqrt(self, a):
|
||||
return math.sqrt(a)
|
||||
|
||||
def abs(self, a):
|
||||
return abs(a)
|
||||
|
||||
def max(self, a, b):
|
||||
return a if a > b else b
|
||||
"@
|
||||
Set-Content -Path "calculator.py" -Value $calc10
|
||||
git add calculator.py
|
||||
git commit -m "Add max function" | Out-Null
|
||||
|
||||
# Create a test file
|
||||
$test = @"
|
||||
import sys
|
||||
from calculator import Calculator
|
||||
|
||||
calc = Calculator()
|
||||
|
||||
# Test addition (this will fail due to bug)
|
||||
result = calc.add(5, 3)
|
||||
if result != 8:
|
||||
print(f'FAIL: add(5, 3) returned {result}, expected 8')
|
||||
sys.exit(1)
|
||||
|
||||
print('PASS: All tests passed')
|
||||
sys.exit(0)
|
||||
"@
|
||||
Set-Content -Path "test.py" -Value $test
|
||||
|
||||
# Return to module directory
|
||||
Set-Location ..
|
||||
|
||||
Write-Host "`n========================================" -ForegroundColor Green
|
||||
Write-Host "Challenge environment created!" -ForegroundColor Green
|
||||
Write-Host "========================================" -ForegroundColor Green
|
||||
Write-Host "`nSituation:" -ForegroundColor Cyan
|
||||
Write-Host "The calculator has a bug - addition doesn't work correctly!" -ForegroundColor Red
|
||||
Write-Host "calc.add(5, 3) returns 2 instead of 8" -ForegroundColor Red
|
||||
Write-Host "`nThe bug was introduced somewhere in the last 10 commits." -ForegroundColor Yellow
|
||||
Write-Host "Manually checking each commit would be tedious." -ForegroundColor Yellow
|
||||
Write-Host "Use git bisect to find it efficiently!" -ForegroundColor Green
|
||||
Write-Host "`nYour task:" -ForegroundColor Yellow
|
||||
Write-Host "1. Navigate to the challenge directory: cd challenge" -ForegroundColor White
|
||||
Write-Host "2. Test the bug: python test.py (it will fail)" -ForegroundColor White
|
||||
Write-Host "3. Start bisect: git bisect start" -ForegroundColor White
|
||||
Write-Host "4. Mark current as bad: git bisect bad" -ForegroundColor White
|
||||
Write-Host "5. Mark old commit as good: git bisect good HEAD~10" -ForegroundColor White
|
||||
Write-Host "6. Git will checkout a commit - test it: python test.py" -ForegroundColor White
|
||||
Write-Host "7. Mark result: git bisect good (if test passes) or git bisect bad (if it fails)" -ForegroundColor White
|
||||
Write-Host "8. Repeat until Git finds the first bad commit" -ForegroundColor White
|
||||
Write-Host "9. Note the commit hash" -ForegroundColor White
|
||||
Write-Host "10. End bisect: git bisect reset" -ForegroundColor White
|
||||
Write-Host "11. Create bug-commit.txt with the bad commit hash" -ForegroundColor White
|
||||
Write-Host "`nHint: The bug is in commit 6 ('Refactor add function for clarity')" -ForegroundColor Cyan
|
||||
Write-Host "Run '../verify.ps1' from the challenge directory to check your solution.`n" -ForegroundColor Cyan
|
||||
@@ -1,133 +0,0 @@
|
||||
#!/usr/bin/env pwsh
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Verifies the bisect challenge solution.
|
||||
|
||||
.DESCRIPTION
|
||||
Checks that the user successfully used git bisect to find
|
||||
the commit that introduced the bug.
|
||||
#>
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
# Make sure we're not in a bisect session
|
||||
$bisectHead = Test-Path ".git/BISECT_HEAD"
|
||||
if ($bisectHead) {
|
||||
Write-Host "[FAIL] You're still in a bisect session." -ForegroundColor Red
|
||||
Write-Host "Hint: End the bisect with: git bisect reset" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check if bug-commit.txt exists
|
||||
if (-not (Test-Path "bug-commit.txt")) {
|
||||
Write-Host "[FAIL] bug-commit.txt not found." -ForegroundColor Red
|
||||
Write-Host "Hint: After finding the bad commit, create a file with its hash:" -ForegroundColor Yellow
|
||||
Write-Host " echo 'commit-hash' > bug-commit.txt" -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Read the commit hash from the file
|
||||
$userCommit = (Get-Content "bug-commit.txt" -Raw).Trim()
|
||||
|
||||
if (-not $userCommit) {
|
||||
Write-Host "[FAIL] bug-commit.txt is empty." -ForegroundColor Red
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Get all commit hashes
|
||||
$allCommits = git log --pretty=format:"%H" --reverse 2>$null
|
||||
$commitArray = $allCommits -split "`n"
|
||||
|
||||
# The bug was introduced in commit 6 (index 5 in 0-based array)
|
||||
# This is the "Refactor add function for clarity" commit
|
||||
$badCommitMessage = git log --pretty=format:"%s" --grep="Refactor add function" 2>$null
|
||||
$actualBadCommit = git log --pretty=format:"%H" --grep="Refactor add function" 2>$null
|
||||
|
||||
if (-not $actualBadCommit) {
|
||||
Write-Host "[FAIL] Could not find the expected bad commit." -ForegroundColor Red
|
||||
Write-Host "Something may be wrong with the repository setup." -ForegroundColor Yellow
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Check if user found the correct commit
|
||||
if ($userCommit -ne $actualBadCommit) {
|
||||
# Maybe they provided a short hash
|
||||
if ($actualBadCommit -like "$userCommit*") {
|
||||
Write-Host "[PASS] Correct commit identified (using short hash)!" -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "[FAIL] Incorrect commit identified." -ForegroundColor Red
|
||||
Write-Host "You identified: $userCommit" -ForegroundColor Yellow
|
||||
Write-Host "Expected: $actualBadCommit" -ForegroundColor Yellow
|
||||
Write-Host "Expected commit message: 'Refactor add function for clarity'" -ForegroundColor Yellow
|
||||
Write-Host "`nHint: Use git bisect to find where the add function broke:" -ForegroundColor Yellow
|
||||
Write-Host " git bisect start" -ForegroundColor White
|
||||
Write-Host " git bisect bad" -ForegroundColor White
|
||||
Write-Host " git bisect good HEAD~10" -ForegroundColor White
|
||||
Write-Host " # Then test with: node test.py" -ForegroundColor White
|
||||
Write-Host " git bisect good # or bad" -ForegroundColor White
|
||||
Set-Location ..
|
||||
exit 1
|
||||
}
|
||||
} else {
|
||||
Write-Host "[PASS] Correct commit identified!" -ForegroundColor Green
|
||||
}
|
||||
|
||||
# Verify the commit actually has the bug
|
||||
git checkout $actualBadCommit 2>$null | Out-Null
|
||||
$calcContent = Get-Content "calculator.py" -Raw
|
||||
|
||||
if ($calcContent -notmatch "add\(a, b\)[\s\S]*?return a - b") {
|
||||
Write-Host "[WARNING] The identified commit doesn't seem to have the expected bug." -ForegroundColor Yellow
|
||||
}
|
||||
|
||||
# Return to latest commit
|
||||
git checkout $(git branch --show-current 2>$null) 2>$null | Out-Null
|
||||
if (-not $?) {
|
||||
git checkout main 2>$null | Out-Null
|
||||
}
|
||||
|
||||
# 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 bisect to perform a binary search" -ForegroundColor White
|
||||
Write-Host "- Tested commits systematically" -ForegroundColor White
|
||||
Write-Host "- Identified the exact commit that introduced the bug" -ForegroundColor White
|
||||
Write-Host "- Found: '$badCommitMessage'" -ForegroundColor White
|
||||
Write-Host "`nThe bug was in commit 6 out of 10 commits." -ForegroundColor Yellow
|
||||
Write-Host "Manual checking: up to 10 tests" -ForegroundColor Yellow
|
||||
Write-Host "With bisect: only ~4 tests needed!" -ForegroundColor Green
|
||||
Write-Host "`nYou now understand git bisect!" -ForegroundColor Green
|
||||
Write-Host "`nKey takeaway:" -ForegroundColor Yellow
|
||||
Write-Host "Bisect uses binary search to efficiently find bugs," -ForegroundColor White
|
||||
Write-Host "saving massive amounts of time in large codebases.`n" -ForegroundColor White
|
||||
|
||||
Set-Location ..
|
||||
exit 0
|
||||
Reference in New Issue
Block a user