diff --git a/GIT-CHEATSHEET.md b/GIT-CHEATSHEET.md index e676fce..20d997f 100644 --- a/GIT-CHEATSHEET.md +++ b/GIT-CHEATSHEET.md @@ -117,24 +117,19 @@ git branch -D Force delete a branch (even if not merged). ```bash -git checkout +git switch ``` Switch to a different branch. ```bash -git checkout -b +git switch -c ``` Create a new branch and switch to it in one command. ```bash -git switch +git switch - ``` -Modern command to switch branches (alternative to checkout). - -```bash -git switch -c -``` -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 +``` +Discard changes in working directory (restore from last commit). + +```bash +git restore --staged +``` +Unstage a file (remove from staging area but keep changes). + +```bash +git restore --source= +``` +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 -- ` or `git restore ` to discard changes +- **Haven't committed yet?** Use `git restore ` to discard changes +- **Staged but want to unstage?** Use `git restore --staged ` - **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 diff --git a/module-03-branching/README.md b/module-03-branching/README.md index e504fac..37bbf00 100644 --- a/module-03-branching/README.md +++ b/module-03-branching/README.md @@ -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 ` creates and switches in one command -> - `git switch -c ` is the newer equivalent +> - Use `git switch` to change branches (modern Git command) +> - `git switch -c ` 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 # Create a new branch -git checkout # Switch to an existing branch -git checkout -b # Create and switch to new branch -git switch # Switch to a branch (newer syntax) -git switch -c # Create and switch (newer syntax) +git switch # Switch to an existing branch +git switch -c # Create and switch to new branch +git switch - # Switch back to previous branch git branch -d # Delete a branch (we won't use this yet) ``` diff --git a/module-04-merging/README.md b/module-04-merging/README.md index 11b87f5..ac93c86 100644 --- a/module-04-merging/README.md +++ b/module-04-merging/README.md @@ -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` diff --git a/module-06-rebasing/README.md b/module-06-rebasing/README.md index e173e03..71ada94 100644 --- a/module-06-rebasing/README.md +++ b/module-06-rebasing/README.md @@ -86,7 +86,7 @@ git rebase --abort git branch # Switch to a branch -git checkout +git switch ``` ## Verification diff --git a/module-08-cherry-pick/README.md b/module-08-cherry-pick/README.md index 3678896..e963683 100644 --- a/module-08-cherry-pick/README.md +++ b/module-08-cherry-pick/README.md @@ -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 ``` @@ -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 # 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 ``` diff --git a/module-09-reset-vs-revert/README.md b/module-09-reset-vs-revert/README.md index 03b4a58..2091945 100644 --- a/module-09-reset-vs-revert/README.md +++ b/module-09-reset-vs-revert/README.md @@ -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 ` to undo it safely diff --git a/module-10-stash/README.md b/module-10-stash/README.md index 038370b..47b6ead 100644 --- a/module-10-stash/README.md +++ b/module-10-stash/README.md @@ -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 diff --git a/module-11-remotes/README.md b/module-11-remotes/README.md index f5a9abc..d6b3a97 100644 --- a/module-11-remotes/README.md +++ b/module-11-remotes/README.md @@ -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 # Get latest from upstream git fetch upstream -git checkout main +git switch main git merge upstream/main # Push to your fork