From e0a1eb2cbe60844c9522d988af3ff987f2cf8247 Mon Sep 17 00:00:00 2001 From: Radovan Bast Date: Fri, 25 Aug 2023 13:19:12 +0200 Subject: [PATCH] use "git restore" and "git switch" throughout; closes #326 --- content/aliases.md | 6 +-- content/archaeology.md | 20 +++++----- content/branches.md | 67 ++++++++++------------------------ content/conflicts.md | 2 +- content/guide.md | 5 +-- content/img/staging-basics.svg | 4 +- content/interrupted.md | 10 ++--- content/reference.md | 4 +- content/staging-area.md | 4 +- content/under-the-hood.md | 2 +- 10 files changed, 45 insertions(+), 79 deletions(-) diff --git a/content/aliases.md b/content/aliases.md index 15a24e6f..24ce7e4d 100644 --- a/content/aliases.md +++ b/content/aliases.md @@ -13,7 +13,7 @@ There is plenty of other configuration for Git, that can make it nicer. ## Aliases -- Aliases are a simple way to improve the usability of Git: for +- Aliases offer a way to improve the usability of Git: for example `git ci` instead of `git commit`. - Aliases are based on simple string replacement in the command. - Aliases can either be specific to a repository or global. @@ -63,8 +63,6 @@ $ git config --global alias.br branch $ git config --global alias.ci "commit -v" $ git config --global alias.cip "commit --patch -v" $ git config --global alias.cl "clone --recursive" -$ git config --global alias.co checkout -$ git config --global alias.cop "checkout --patch" $ git config --global alias.di diff $ git config --global alias.dic "diff --staged --color-words" $ git config --global alias.diw "diff --color-words" @@ -82,8 +80,6 @@ Here is what they do: - `ci`: commit (check in), with `-v` option for clarity - `cip`: commit, selecting parts individually, interactively. - `cl`: clone, init submodules (submodules are an advanced topic) -- `co`: checkout (obvious) -- `cop`: checkout, selecting parts individually, interactively. - `di`: diff (obvious) - `dic`: diff of staging area vs last commit (what is about to be committed) - `diw`: a word diff, color. Useful for small changes. diff --git a/content/archaeology.md b/content/archaeology.md index 898acc21..f74e362c 100644 --- a/content/archaeology.md +++ b/content/archaeology.md @@ -116,26 +116,26 @@ Discuss how these two affect the annotation: ``` -### git checkout -b: to inspect code in the past +### git switch --create: to inspect code in the past We can create branches pointing to a commit in the past. This is the recommended mechanism to inspect old code: ```console -$ git checkout -b branchname somehash +$ git switch --create branchname somehash ``` Example (lines starting with "#" are only comments): ```console $ # create branch called "older-code" from hash 347e6292419b -$ git checkout -b older-code 347e6292419bd0e4bff077fe971f983932d7a0e9 +$ git switch --create older-code 347e6292419bd0e4bff077fe971f983932d7a0e9 $ # now you can navigate and inspect the code as it was back then $ # ... $ # after we are done we can switch back to "main" -$ git checkout main +$ git switch main $ # if we like we can delete the "older-code" branch $ git branch -d older-code @@ -163,7 +163,7 @@ $ git switch --create branchname somehash ```console $ git clone https://github.com/networkx/networkx.git $ cd networkx - $ git checkout -b exercise networkx-2.6.3 + $ git switch --create exercise networkx-2.6.3 ``` Then using the above toolbox try to: @@ -172,7 +172,7 @@ $ git switch --create branchname somehash 3. Inspect that commit with `git show`. 4. Create a branch pointing to the past when that commit was created to be able to browse and use the code as it was back then. - 5. How would you checkout the version of the code right before that line was last modified? + 5. How would you bring the code to the version of the code right before that line was last modified? ````{solution} 1. We use `git grep`: @@ -204,7 +204,7 @@ $ git switch --create branchname somehash 5. This is a compact way to access the first parent of `90544b4fa` (here we called the branch "just-before"): ```console - $ git checkout -b just-before 90544b4fa~1 + $ git switch --create just-before 90544b4fa~1 ``` ```` ````` @@ -308,17 +308,17 @@ We will probably arrive at a solution which is similar to `git bisect`: How to navigate to the parent of a commit with hash `somehash`: ```console - $ git checkout -b branchname somehash~1 + $ git switch --create branchname somehash~1 ``` Instead of a tilde you can also use this: ```console - $ git checkout -b branchname somehash^ + $ git switch --create branchname somehash^ ``` ```` ```{keypoints} - git log/grep/annotate/show/bisect is a powerful combination when doing archaeology in a project. -- `git checkout -b ` is the recommended mechanism to inspect old code. +- `git switch --create ` is the recommended mechanism to inspect old code. - On newer Git you can use the more intuitive `git switch --create branchname somehash`. ``` diff --git a/content/branches.md b/content/branches.md index 1d00e381..e93fa398 100644 --- a/content/branches.md +++ b/content/branches.md @@ -137,7 +137,7 @@ Let's create a branch called `experiment` where we add cilantro to `ingredients. ```console $ git branch experiment main # creates branch "experiment" from "main" -$ git checkout experiment # switch to branch "experiment" +$ git switch experiment # switch to branch "experiment" $ git branch # list all local branches and show on which branch we are ``` @@ -285,9 +285,9 @@ Our goal now is to merge `experiment` into `main`. $ git clone https://github.com/coderefinery/recipe-before-merge.git $ cd recipe-before-merge - $ git checkout experiment - $ git checkout less-salt - $ git checkout main + $ git switch experiment + $ git switch less-salt + $ git switch main $ git remote remove origin @@ -550,7 +550,7 @@ Let us pause for a moment and recapitulate what we have just learned: ```console $ git branch # see where we are $ git branch # create branch -$ git checkout # switch to branch +$ git switch # switch to branch $ git merge # merge branch (to current branch) $ git branch -d # delete branch $ git branch -D # delete unmerged branch @@ -560,13 +560,13 @@ Since the following command combo is so frequent: ```console $ git branch # create branch -$ git checkout # switch to branch +$ git switch # switch to branch ``` There is a shortcut for it: ```console -$ git checkout -b # create branch and switch to it +$ git switch --create # create branch and switch to it ``` ### Typical workflows @@ -574,20 +574,20 @@ $ git checkout -b # create branch and switch to it With this there are two typical workflows: ```console -$ git checkout -b new-feature # create branch, switch to it -$ git commit # work, work, work, ..., and test -$ git checkout main # once feature is ready, switch to main -$ git merge new-feature # merge work to main -$ git branch -d new-feature # remove branch +$ git switch --create new-feature # create branch, switch to it +$ git commit # work, work, work, ..., and test +$ git switch main # once feature is ready, switch to main +$ git merge new-feature # merge work to main +$ git branch -d new-feature # remove branch ``` Sometimes you have a wild idea which does not work. Or you want some throw-away branch for debugging: ```console -$ git checkout -b wild-idea # create branch, switch to it, work, work, work ... -$ git checkout main # realize it was a bad idea, back to main -$ git branch -D wild-idea # it is gone, off to a new idea +$ git switch --create wild-idea # create branch, switch to it, work, work, work ... +$ git switch main # realize it was a bad idea, back to main +$ git branch -D wild-idea # it is gone, off to a new idea ``` No problem: we worked on a branch, branch is deleted, `main` is clean. @@ -602,16 +602,16 @@ No problem: we worked on a branch, branch is deleted, `main` is clean. 2. ```console $ git add file.txt $ git branch new-branch - $ git checkout new-branch + $ git switch new-branch $ git commit ``` 3. ```console - $ git checkout -b new-branch + $ git switch --create new-branch $ git add file.txt $ git commit ``` 4. ```console - $ git checkout new-branch + $ git switch new-branch $ git add file.txt $ git commit ``` @@ -619,36 +619,7 @@ No problem: we worked on a branch, branch is deleted, `main` is clean. ```{solution} Both 2 and 3 would do the job. Note that in 2 we first stage the file, and then create the branch and commit to it. In 1 we create the branch but do not switch to it, while in 4 we - don't give the `-b` flag to `git checkout` to create the new branch. - ``` -```` - -````{discussion} Different meanings of "checkout" - Depending on the context `git checkout` can do very different actions: - - 1) Switch to a branch: - ```console - $ git checkout - ``` - - 2) Bring the working tree to a specific state (commit): - ```console - $ git checkout - ``` - - 3) Set a file/path to a specific state (**throws away all unstaged/uncommitted changes**): - ```console - $ git checkout - ``` - - This is unfortunate from the user's point of view but the way Git is implemented it makes sense. - Picture `git checkout` as an operation that brings the working tree to a specific state. - The state can be a commit or a branch (pointing to a commit). - - In Git 2.23 (2019-08-16) and later this is much nicer: - ```console - $ git switch # switch to a different branch - $ git restore # discard changes in working directory + don't give the `--create` flag to `git switch` to create the new branch. ``` ```` diff --git a/content/conflicts.md b/content/conflicts.md index aa1bf53b..d984e02d 100644 --- a/content/conflicts.md +++ b/content/conflicts.md @@ -173,7 +173,7 @@ main? The first merge will work: ```console -$ git checkout main +$ git switch main $ git status $ git merge like-cilantro diff --git a/content/guide.md b/content/guide.md index 47b6cf24..d373abd9 100644 --- a/content/guide.md +++ b/content/guide.md @@ -87,8 +87,8 @@ By the end of this lesson, learners should: - have a mental model of the different states a file can have in Git (untracked, modified, staged, unmodified) - know how to write good commit messages - have an idea of how the staging area can be used to craft good commits -- know how to undo commits using `git revert` and discard changes using `git checkout` -- understand that `git checkout` can be dangerous if changes have not been staged +- know how to undo commits using `git revert` and discard changes using `git restore` +- understand that `git restore` can be dangerous if changes have not been staged - know how to create branches and switch between branches - have a mental model of how branches work and get used to thinking of branches in a graphical (tree-structure) way - know how to merge branches and understand what that means in terms of combining different modifications @@ -97,7 +97,6 @@ By the end of this lesson, learners should: - be able to set up a repository on GitHub and connect it with local repository - push changes to a remote repository - know a few ways to search through a repository and its history -- know about the different meanings of `git checkout`, and realize how they're in fact similar ## How to teach this lesson diff --git a/content/img/staging-basics.svg b/content/img/staging-basics.svg index 6c7a2c3c..13df8bf5 100644 --- a/content/img/staging-basics.svg +++ b/content/img/staging-basics.svg @@ -386,7 +386,7 @@ id="tspan863-6" x="73.973495" y="250.51981" - style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:5.64444447px;font-family:Courier;-inkscape-font-specification:'Courier, Normal';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-feature-settings:normal;text-align:center;writing-mode:lr-tb;text-anchor:middle;fill:#999999;stroke-width:0.26458332">checkout + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:5.64444447px;font-family:Courier;-inkscape-font-specification:'Courier, Normal';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-feature-settings:normal;text-align:center;writing-mode:lr-tb;text-anchor:middle;fill:#999999;stroke-width:0.26458332">restore checkout + style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:5.64444447px;font-family:Courier;-inkscape-font-specification:'Courier, Normal';font-variant-ligatures:normal;font-variant-caps:normal;font-variant-numeric:normal;font-feature-settings:normal;text-align:center;writing-mode:lr-tb;text-anchor:middle;fill:#999999;stroke-width:0.26458332">restore # stage changes -$ git commit # commit them -$ git checkout main # back to main, continue your work there ... -$ git checkout temporary # continue where you left off +$ git switch --create temporary # create a branch and switch to it +$ git add # stage changes +$ git commit # commit them +$ git switch main # back to main, continue your work there ... +$ git switch temporary # continue where you left off ``` Later you can merge it to main or rebase it on top of main and resume work. diff --git a/content/reference.md b/content/reference.md index 6547f32b..f2d4821f 100644 --- a/content/reference.md +++ b/content/reference.md @@ -53,8 +53,8 @@ General work: * `git commit --amend`: amend our last commit * `git branch`: show which branch we're on * `git branch `: create a new branch called "name" -* `git checkout `: checkout last committed version of <file>, losing unstaged changes -* `git checkout -b `: create a new branch and switch to it +* `git restore `: restore last committed/staged version of <file>, losing unstaged changes +* `git switch --create `: create a new branch and switch to it * `git revert abc123`: create a new commit which reverts commit abc123 * `git reset --soft abc123`: remove all commits after abc123, but keep their modifications as staged changes * `git reset --hard abc123`: remove all commits after abc123, permanently throwing away their changes diff --git a/content/staging-area.md b/content/staging-area.md index ff071535..80285b11 100644 --- a/content/staging-area.md +++ b/content/staging-area.md @@ -90,7 +90,7 @@ commit or having one logical change spread over several commits. separated by a blank line, split them into separate choices. - `q` aborts everything. - `?` for more options. -- The `-p` option is also available on `commit`, `checkout`, `reset`, and `add`. +- The `-p` option is also available on `commit`, `restore`, `checkout`, `reset`, and `add`. (exercise-interactive-commits)= @@ -156,7 +156,7 @@ We give two examples and the instructor can pick one or both: - What you actually do: - Go through the store and put everything you need in your shopping basket. - - Get to the checkout. Put your home stuff on the conveyor belt + - Get to the check-out. Put your home stuff on the conveyor belt (`git add`). Check both the belt (`git diff --staged`) and your basket (`git diff`) to make sure you got all your home stuff. - Pay (`git commit`) diff --git a/content/under-the-hood.md b/content/under-the-hood.md index 148134ae..3f2c9d4a 100644 --- a/content/under-the-hood.md +++ b/content/under-the-hood.md @@ -112,7 +112,7 @@ nothing to commit, working tree clean ``` ```console -$ git checkout -b idea +$ git switch --create idea Switched to a new branch 'idea' ```