feat: use switch instead of checkout

This commit is contained in:
Bjarke Sporring
2026-01-05 12:38:35 +01:00
parent a7b511c8cb
commit e29b9bca70
8 changed files with 55 additions and 44 deletions

View File

@@ -117,24 +117,19 @@ git branch -D <branch-name>
Force delete a branch (even if not merged).
```bash
git checkout <branch-name>
git switch <branch-name>
```
Switch to a different branch.
```bash
git checkout -b <branch-name>
git switch -c <branch-name>
```
Create a new branch and switch to it in one command.
```bash
git switch <branch-name>
git switch -
```
Modern command to switch branches (alternative to checkout).
```bash
git switch -c <branch-name>
```
Create a new branch and switch to it (alternative to checkout -b).
Switch back to the previous branch.
### Merging
@@ -162,6 +157,23 @@ 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
@@ -420,7 +432,8 @@ Automatically test commits using a script to find the bad commit.
### Undoing Changes
- **Haven't committed yet?** Use `git checkout -- <file>` or `git restore <file>` to discard 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
@@ -459,7 +472,7 @@ Each workshop module focuses on specific commands:
- **Module 01**: init, add, commit, status
- **Module 02**: log, show, diff
- **Module 03**: branch, checkout/switch
- **Module 03**: branch, switch
- **Module 04**: merge (fast-forward and three-way)
- **Module 05**: merge (conflict resolution)
- **Module 06**: rebase

View File

@@ -4,8 +4,8 @@
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`
- Create new branches using `git branch` or `git switch -c`
- Switch between branches using `git switch`
- View all branches with `git branch`
- Understand that branches allow parallel development
@@ -38,19 +38,18 @@ Your goal is to create a feature branch, make commits on it, and understand how
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`
3. Create and switch to new branch: `git switch -c 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`
7. Switch back to main: `git switch main`
8. Run `ls` and notice that `login.py` doesn't exist on main!
9. Switch back to feature-login: `git checkout feature-login`
9. Switch back to feature-login: `git switch 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
> - Use `git switch` to change branches (modern Git command)
> - `git switch -c <name>` creates and switches in one command
> - 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
@@ -66,10 +65,9 @@ Your goal is to create a feature branch, make commits on it, and understand how
```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 switch <branch> # Switch to an existing branch
git switch -c <name> # Create and switch to new branch
git switch - # Switch back to previous branch
git branch -d <name> # Delete a branch (we won't use this yet)
```

View File

@@ -43,10 +43,10 @@ This challenge has two parts that teach you about the two types of merges in Git
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`
6. Create feature-ui branch: `git switch -c 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`
9. Switch back to main: `git switch 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`

View File

@@ -86,7 +86,7 @@ git rebase --abort
git branch
# Switch to a branch
git checkout <branch-name>
git switch <branch-name>
```
## Verification

View File

@@ -112,7 +112,7 @@ The verification will check that:
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`
5. Switch to main branch: `git switch 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
@@ -131,7 +131,7 @@ The verification will check that:
### 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 switch production
git cherry-pick <bugfix-commit-hash>
```
@@ -141,17 +141,17 @@ You accidentally committed on the wrong branch:
# On wrong branch, note the commit hash
git log --oneline
# Switch to correct branch
git checkout correct-branch
git switch correct-branch
git cherry-pick <commit-hash>
# Go back and remove from wrong branch
git checkout wrong-branch
git switch 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 switch release-2.0
git cherry-pick <fix-from-main>
```

View File

@@ -135,7 +135,7 @@ The verification will check that:
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`
5. Switch to shared-feature: `git switch shared-feature`
6. View commits: `git log --oneline`
7. Find the hash of the "Add broken feature" commit
8. Use `git revert <commit-hash>` to undo it safely

View File

@@ -121,10 +121,10 @@ The verification will check that:
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`
6. Switch to main: `git switch 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`
9. Switch back to feature: `git switch feature-login`
10. Restore your work: `git stash pop`
11. Complete the feature (the TODOs in login.js)
12. Commit your completed feature
@@ -147,9 +147,9 @@ The verification will check that:
```bash
# Working on feature, need to switch to main
git stash
git checkout main
git switch main
# Do work on main
git checkout feature
git switch feature
git stash pop
```
@@ -176,10 +176,10 @@ git stash pop # Restore original work
```bash
# Same fix needed on multiple branches
git stash
git checkout branch1
git switch branch1
git stash apply
git commit -am "Apply fix"
git checkout branch2
git switch branch2
git stash apply
git commit -am "Apply fix"
git stash drop # Clean up when done

View File

@@ -134,7 +134,7 @@ The verification will check that:
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`
5. Create and switch to a feature branch: `git switch -c 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`
@@ -185,20 +185,20 @@ Safer than pull because it doesn't automatically merge
### Daily Work Flow
```bash
# Start of day: get latest changes
git checkout main
git switch main
git pull origin main
# Create feature branch
git checkout -b my-feature
git switch -c my-feature
# Do work, make commits
git add .
git commit -m "Add feature"
# Before pushing, update with latest main
git checkout main
git switch main
git pull origin main
git checkout my-feature
git switch my-feature
git merge main
# Push your feature
@@ -223,7 +223,7 @@ git remote add upstream <original-repo-url>
# Get latest from upstream
git fetch upstream
git checkout main
git switch main
git merge upstream/main
# Push to your fork