feat: use switch instead of checkout
This commit is contained in:
@@ -117,24 +117,19 @@ git branch -D <branch-name>
|
|||||||
Force delete a branch (even if not merged).
|
Force delete a branch (even if not merged).
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git checkout <branch-name>
|
git switch <branch-name>
|
||||||
```
|
```
|
||||||
Switch to a different branch.
|
Switch to a different branch.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git checkout -b <branch-name>
|
git switch -c <branch-name>
|
||||||
```
|
```
|
||||||
Create a new branch and switch to it in one command.
|
Create a new branch and switch to it in one command.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
git switch <branch-name>
|
git switch -
|
||||||
```
|
```
|
||||||
Modern command to switch branches (alternative to checkout).
|
Switch back to the previous branch.
|
||||||
|
|
||||||
```bash
|
|
||||||
git switch -c <branch-name>
|
|
||||||
```
|
|
||||||
Create a new branch and switch to it (alternative to checkout -b).
|
|
||||||
|
|
||||||
### Merging
|
### Merging
|
||||||
|
|
||||||
@@ -162,6 +157,23 @@ git commit
|
|||||||
```
|
```
|
||||||
Stage resolved files and complete the merge.
|
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
|
## Advanced
|
||||||
@@ -420,7 +432,8 @@ Automatically test commits using a script to find the bad commit.
|
|||||||
|
|
||||||
### Undoing Changes
|
### 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
|
- **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`
|
- **Want to change last commit message?** Use `git commit --amend`
|
||||||
- **Pushed to remote already?** Use `git revert` (not reset) to safely undo
|
- **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 01**: init, add, commit, status
|
||||||
- **Module 02**: log, show, diff
|
- **Module 02**: log, show, diff
|
||||||
- **Module 03**: branch, checkout/switch
|
- **Module 03**: branch, switch
|
||||||
- **Module 04**: merge (fast-forward and three-way)
|
- **Module 04**: merge (fast-forward and three-way)
|
||||||
- **Module 05**: merge (conflict resolution)
|
- **Module 05**: merge (conflict resolution)
|
||||||
- **Module 06**: rebase
|
- **Module 06**: rebase
|
||||||
|
|||||||
@@ -4,8 +4,8 @@
|
|||||||
|
|
||||||
In this module, you will:
|
In this module, you will:
|
||||||
- Understand what a branch is in Git
|
- Understand what a branch is in Git
|
||||||
- Create new branches using `git branch` or `git checkout -b`
|
- Create new branches using `git branch` or `git switch -c`
|
||||||
- Switch between branches using `git checkout` or `git switch`
|
- Switch between branches using `git switch`
|
||||||
- View all branches with `git branch`
|
- View all branches with `git branch`
|
||||||
- Understand that branches allow parallel development
|
- 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`
|
1. Navigate to the challenge directory: `cd challenge`
|
||||||
2. View existing branches: `git branch`
|
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
|
4. Create `login.py` with any content you like
|
||||||
5. Stage and commit: `git add login.py` and `git commit -m "Add login functionality"`
|
5. Stage and commit: `git add login.py` and `git commit -m "Add login functionality"`
|
||||||
6. Modify `login.py`, then commit again
|
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!
|
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!
|
10. Run `ls` again and see that `login.py` is back!
|
||||||
|
|
||||||
> **Important Notes:**
|
> **Important Notes:**
|
||||||
> - You can use either `git checkout` or `git switch` to change branches
|
> - Use `git switch` to change branches (modern Git command)
|
||||||
> - `git checkout -b <name>` creates and switches in one command
|
> - `git switch -c <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
|
> - 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
|
> - 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
|
```bash
|
||||||
git branch # List all branches (* shows current)
|
git branch # List all branches (* shows current)
|
||||||
git branch <name> # Create a new branch
|
git branch <name> # Create a new branch
|
||||||
git checkout <branch> # Switch to an existing branch
|
git switch <branch> # Switch to an existing branch
|
||||||
git checkout -b <name> # Create and switch to new branch
|
git switch -c <name> # Create and switch to new branch
|
||||||
git switch <branch> # Switch to a branch (newer syntax)
|
git switch - # Switch back to previous branch
|
||||||
git switch -c <name> # Create and switch (newer syntax)
|
|
||||||
git branch -d <name> # Delete a branch (we won't use this yet)
|
git branch -d <name> # Delete a branch (we won't use this yet)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -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`
|
3. View existing branches: `git branch -a`
|
||||||
4. Merge feature-api: `git merge feature-api`
|
4. Merge feature-api: `git merge feature-api`
|
||||||
5. View the log: `git log --oneline --graph`
|
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
|
7. Create a new file `ui.py` and commit it
|
||||||
8. Make another commit on feature-ui (modify ui.py)
|
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
|
10. Make a change on main (modify api.py) and commit it
|
||||||
11. Merge feature-ui: `git merge feature-ui`
|
11. Merge feature-ui: `git merge feature-ui`
|
||||||
12. View the merge history: `git log --oneline --graph --all`
|
12. View the merge history: `git log --oneline --graph --all`
|
||||||
|
|||||||
@@ -86,7 +86,7 @@ git rebase --abort
|
|||||||
git branch
|
git branch
|
||||||
|
|
||||||
# Switch to a branch
|
# Switch to a branch
|
||||||
git checkout <branch-name>
|
git switch <branch-name>
|
||||||
```
|
```
|
||||||
|
|
||||||
## Verification
|
## Verification
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ The verification will check that:
|
|||||||
2. You're currently on the development branch
|
2. You're currently on the development branch
|
||||||
3. View the commits: `git log --oneline`
|
3. View the commits: `git log --oneline`
|
||||||
4. You'll see several commits - identify the bug fixes
|
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)
|
6. Cherry-pick the bug fix commits (you'll need their commit hashes)
|
||||||
7. Verify the result with `git log --oneline`
|
7. Verify the result with `git log --oneline`
|
||||||
8. Run the verification script
|
8. Run the verification script
|
||||||
@@ -131,7 +131,7 @@ The verification will check that:
|
|||||||
### Hotfix to Production
|
### Hotfix to Production
|
||||||
You have a critical bug fix on a development branch that needs to go to production immediately:
|
You have a critical bug fix on a development branch that needs to go to production immediately:
|
||||||
```bash
|
```bash
|
||||||
git checkout production
|
git switch production
|
||||||
git cherry-pick <bugfix-commit-hash>
|
git cherry-pick <bugfix-commit-hash>
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -141,17 +141,17 @@ You accidentally committed on the wrong branch:
|
|||||||
# On wrong branch, note the commit hash
|
# On wrong branch, note the commit hash
|
||||||
git log --oneline
|
git log --oneline
|
||||||
# Switch to correct branch
|
# Switch to correct branch
|
||||||
git checkout correct-branch
|
git switch correct-branch
|
||||||
git cherry-pick <commit-hash>
|
git cherry-pick <commit-hash>
|
||||||
# Go back and remove from wrong branch
|
# Go back and remove from wrong branch
|
||||||
git checkout wrong-branch
|
git switch wrong-branch
|
||||||
git reset --hard HEAD~1
|
git reset --hard HEAD~1
|
||||||
```
|
```
|
||||||
|
|
||||||
### Backporting
|
### Backporting
|
||||||
You need to apply a fix to an older release branch:
|
You need to apply a fix to an older release branch:
|
||||||
```bash
|
```bash
|
||||||
git checkout release-2.0
|
git switch release-2.0
|
||||||
git cherry-pick <fix-from-main>
|
git cherry-pick <fix-from-main>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -135,7 +135,7 @@ The verification will check that:
|
|||||||
2. You're on the local-feature branch with a bad commit
|
2. You're on the local-feature branch with a bad commit
|
||||||
3. View commits: `git log --oneline`
|
3. View commits: `git log --oneline`
|
||||||
4. Use `git reset --hard HEAD~1` to remove the bad commit
|
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`
|
6. View commits: `git log --oneline`
|
||||||
7. Find the hash of the "Add broken feature" commit
|
7. Find the hash of the "Add broken feature" commit
|
||||||
8. Use `git revert <commit-hash>` to undo it safely
|
8. Use `git revert <commit-hash>` to undo it safely
|
||||||
|
|||||||
@@ -121,10 +121,10 @@ The verification will check that:
|
|||||||
3. Check status: `git status` (you'll see modified files)
|
3. Check status: `git status` (you'll see modified files)
|
||||||
4. Stash your changes: `git stash save "WIP: login feature"`
|
4. Stash your changes: `git stash save "WIP: login feature"`
|
||||||
5. Verify working directory is clean: `git status`
|
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)
|
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"`
|
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`
|
10. Restore your work: `git stash pop`
|
||||||
11. Complete the feature (the TODOs in login.js)
|
11. Complete the feature (the TODOs in login.js)
|
||||||
12. Commit your completed feature
|
12. Commit your completed feature
|
||||||
@@ -147,9 +147,9 @@ The verification will check that:
|
|||||||
```bash
|
```bash
|
||||||
# Working on feature, need to switch to main
|
# Working on feature, need to switch to main
|
||||||
git stash
|
git stash
|
||||||
git checkout main
|
git switch main
|
||||||
# Do work on main
|
# Do work on main
|
||||||
git checkout feature
|
git switch feature
|
||||||
git stash pop
|
git stash pop
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -176,10 +176,10 @@ git stash pop # Restore original work
|
|||||||
```bash
|
```bash
|
||||||
# Same fix needed on multiple branches
|
# Same fix needed on multiple branches
|
||||||
git stash
|
git stash
|
||||||
git checkout branch1
|
git switch branch1
|
||||||
git stash apply
|
git stash apply
|
||||||
git commit -am "Apply fix"
|
git commit -am "Apply fix"
|
||||||
git checkout branch2
|
git switch branch2
|
||||||
git stash apply
|
git stash apply
|
||||||
git commit -am "Apply fix"
|
git commit -am "Apply fix"
|
||||||
git stash drop # Clean up when done
|
git stash drop # Clean up when done
|
||||||
|
|||||||
@@ -134,7 +134,7 @@ The verification will check that:
|
|||||||
2. You'll find a simulated "remote" repository
|
2. You'll find a simulated "remote" repository
|
||||||
3. Clone it: `git clone remote-repo local-repo`
|
3. Clone it: `git clone remote-repo local-repo`
|
||||||
4. Navigate into your clone: `cd 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)
|
6. Make changes to the project (add a new feature to app.js)
|
||||||
7. Commit your changes
|
7. Commit your changes
|
||||||
8. Push to remote: `git push -u origin add-feature`
|
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
|
### Daily Work Flow
|
||||||
```bash
|
```bash
|
||||||
# Start of day: get latest changes
|
# Start of day: get latest changes
|
||||||
git checkout main
|
git switch main
|
||||||
git pull origin main
|
git pull origin main
|
||||||
|
|
||||||
# Create feature branch
|
# Create feature branch
|
||||||
git checkout -b my-feature
|
git switch -c my-feature
|
||||||
|
|
||||||
# Do work, make commits
|
# Do work, make commits
|
||||||
git add .
|
git add .
|
||||||
git commit -m "Add feature"
|
git commit -m "Add feature"
|
||||||
|
|
||||||
# Before pushing, update with latest main
|
# Before pushing, update with latest main
|
||||||
git checkout main
|
git switch main
|
||||||
git pull origin main
|
git pull origin main
|
||||||
git checkout my-feature
|
git switch my-feature
|
||||||
git merge main
|
git merge main
|
||||||
|
|
||||||
# Push your feature
|
# Push your feature
|
||||||
@@ -223,7 +223,7 @@ git remote add upstream <original-repo-url>
|
|||||||
|
|
||||||
# Get latest from upstream
|
# Get latest from upstream
|
||||||
git fetch upstream
|
git fetch upstream
|
||||||
git checkout main
|
git switch main
|
||||||
git merge upstream/main
|
git merge upstream/main
|
||||||
|
|
||||||
# Push to your fork
|
# Push to your fork
|
||||||
|
|||||||
Reference in New Issue
Block a user