Skip to content

Commit

Permalink
use "git restore" and "git switch" throughout; closes #326
Browse files Browse the repository at this point in the history
  • Loading branch information
bast committed Aug 25, 2023
1 parent c0ca75e commit e0a1eb2
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 79 deletions.
6 changes: 1 addition & 5 deletions content/aliases.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"
Expand All @@ -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.
Expand Down
20 changes: 10 additions & 10 deletions content/archaeology.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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`:
Expand Down Expand Up @@ -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
```
````
`````
Expand Down Expand Up @@ -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 <name> <hash>` is the recommended mechanism to inspect old code.
- `git switch --create <name> <hash>` is the recommended mechanism to inspect old code.
- On newer Git you can use the more intuitive `git switch --create branchname somehash`.
```
67 changes: 19 additions & 48 deletions content/branches.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 <name> # create branch <name>
$ git checkout <name> # switch to branch <name>
$ git switch <name> # switch to branch <name>
$ git merge <name> # merge branch <name> (to current branch)
$ git branch -d <name> # delete branch <name>
$ git branch -D <name> # delete unmerged branch
Expand All @@ -560,34 +560,34 @@ Since the following command combo is so frequent:

```console
$ git branch <name> # create branch <name>
$ git checkout <name> # switch to branch <name>
$ git switch <name> # switch to branch <name>
```

There is a shortcut for it:

```console
$ git checkout -b <name> # create branch <name> and switch to it
$ git switch --create <name> # create branch <name> and switch to it
```

### Typical workflows

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.
Expand All @@ -602,53 +602,24 @@ 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
```
```{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 <branchname>
```
2) Bring the working tree to a specific state (commit):
```console
$ git checkout <hash>
```
3) Set a file/path to a specific state (**throws away all unstaged/uncommitted changes**):
```console
$ git checkout <path/file>
```
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 <branchname> # switch to a different branch
$ git restore <path/file> # discard changes in working directory
don't give the `--create` flag to `git switch` to create the new branch.
```
````

Expand Down
2 changes: 1 addition & 1 deletion content/conflicts.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ main?
The first merge will work:

```console
$ git checkout main
$ git switch main
$ git status
$ git merge like-cilantro

Expand Down
5 changes: 2 additions & 3 deletions content/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions content/img/staging-basics.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions content/interrupted.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ Sounds like a good time to make a feature branch.
You basically know how to do this:

```console
$ git checkout -b temporary # create a branch and switch to it
$ git add <paths> # 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 <paths> # 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.
Expand Down
4 changes: 2 additions & 2 deletions content/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ General work:
* `git commit --amend`: amend our last commit
* `git branch`: show which branch we're on
* `git branch <name>`: create a new branch called "name"
* `git checkout <file>`: checkout last committed version of &lt;file&gt;, losing unstaged changes
* `git checkout -b <branch-name>`: create a new branch and switch to it
* `git restore <file>`: restore last committed/staged version of &lt;file&gt;, losing unstaged changes
* `git switch --create <branch-name>`: 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
Expand Down
4 changes: 2 additions & 2 deletions content/staging-area.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)=
Expand Down Expand Up @@ -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`)
Expand Down
2 changes: 1 addition & 1 deletion content/under-the-hood.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
```
Expand Down

0 comments on commit e0a1eb2

Please sign in to comment.