Skip to content

Commit 6ffd157

Browse files
committed
replace git checkout with git switch for moving HEAD
and adapting the working copy contents to match it
1 parent a013e32 commit 6ffd157

21 files changed

Lines changed: 84 additions & 76 deletions

File tree

‎C-git-commands.asc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ Finally, we go through some of the details of what it actually does behind the s
106106
==== git clone
107107

108108
The `git clone` command is actually something of a wrapper around several other commands.
109-
It creates a new directory, goes into it and runs `git init` to make it an empty Git repository, adds a remote (`git remote add`) to the URL that you pass it (by default named `origin`), runs a `git fetch` from that remote repository and then checks out the latest commit into your working directory with `git checkout`.
109+
It creates a new directory, goes into it and runs `git init` to make it an empty Git repository, adds a remote (`git remote add`) to the URL that you pass it (by default named `origin`), runs a `git fetch` from that remote repository and then checks out the latest commit into your working directory with `git switch`.
110110

111111
The `git clone` command is used in dozens of places throughout the book, but we'll just list a few interesting places.
112112

‎book/02-git-basics/sections/tagging.asc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ Thus, if you need to make changes -- say you're fixing a bug on an older version
292292

293293
[source,console]
294294
----
295-
$ git checkout -b version2 v2.0.0
295+
$ git switch -c version2 v2.0.0
296296
Switched to a new branch 'version2'
297297
----
298298

‎book/03-git-branching/sections/basic-branching-and-merging.asc‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ First, let's say you're working on your project and have a couple of commits alr
2525
image::images/basic-branching-1.png[A simple commit history]
2626

2727
You've decided that you're going to work on issue #53 in whatever issue-tracking system your company uses.
28-
To create a new branch and switch to it at the same time, you can run the `git checkout` command with the `-b` switch:
28+
To create a new branch and switch to it at the same time, you can run the `git switch` command with the `-c` switch:
2929

3030
[source,console]
3131
----
32-
$ git checkout -b iss53
32+
$ git switch -c iss53
3333
Switched to a new branch "iss53"
3434
----
3535

@@ -38,7 +38,7 @@ This is shorthand for:
3838
[source,console]
3939
----
4040
$ git branch iss53
41-
$ git checkout iss53
41+
$ git switch iss53
4242
----
4343

4444
.Creating a new branch pointer
@@ -67,7 +67,7 @@ For now, let's assume you've committed all your changes, so you can switch back
6767

6868
[source,console]
6969
----
70-
$ git checkout master
70+
$ git switch master
7171
Switched to branch 'master'
7272
----
7373

@@ -80,7 +80,7 @@ Let's create a `hotfix` branch on which to work until it's completed:
8080

8181
[source,console]
8282
----
83-
$ git checkout -b hotfix
83+
$ git switch -c hotfix
8484
Switched to a new branch 'hotfix'
8585
$ vim index.html
8686
$ git commit -a -m 'Fix broken email address'
@@ -96,7 +96,7 @@ You do this with the `git merge` command:(((git commands, merge)))
9696

9797
[source,console]
9898
----
99-
$ git checkout master
99+
$ git switch master
100100
$ git merge hotfix
101101
Updating f42c576..3a0874c
102102
Fast-forward
@@ -127,7 +127,7 @@ Now you can switch back to your work-in-progress branch on issue #53 and continu
127127

128128
[source,console]
129129
----
130-
$ git checkout iss53
130+
$ git switch iss53
131131
Switched to branch "iss53"
132132
$ vim index.html
133133
$ git commit -a -m 'Finish the new footer [issue 53]'
@@ -151,7 +151,7 @@ All you have to do is check out the branch you wish to merge into and then run t
151151

152152
[source,console]
153153
----
154-
$ git checkout master
154+
$ git switch master
155155
Switched to branch 'master'
156156
$ git merge iss53
157157
Merge made by the 'recursive' strategy.

‎book/03-git-branching/sections/branch-management.asc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ The options described above, `--merged` and `--no-merged` will, if not given a c
6767
You can always provide an additional argument to ask about the merge state with respect to some other branch without checking that other branch out first, as in, what is not merged into the `master` branch?
6868
[source,console]
6969
----
70-
$ git checkout testing
70+
$ git switch testing
7171
$ git branch --no-merged master
7272
topicA
7373
featureB

‎book/03-git-branching/sections/nutshell.asc‎

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ You can see the `master` and `testing` branches that are right there next to the
9191
==== Switching Branches
9292

9393
(((branches, switching)))
94-
To switch to an existing branch, you run the `git checkout` command.(((git commands, checkout)))
94+
To switch to an existing branch, you run the `git switch` command.(((git commands, switch)))
9595
Let's switch to the new `testing` branch:
9696

9797
[source,console]
9898
----
99-
$ git checkout testing
99+
$ git switch testing
100100
----
101101

102102
This moves `HEAD` to point to the `testing` branch.
@@ -116,12 +116,12 @@ $ git commit -a -m 'Make a change'
116116
.The HEAD branch moves forward when a commit is made
117117
image::images/advance-testing.png[The HEAD branch moves forward when a commit is made]
118118

119-
This is interesting, because now your `testing` branch has moved forward, but your `master` branch still points to the commit you were on when you ran `git checkout` to switch branches.
119+
This is interesting, because now your `testing` branch has moved forward, but your `master` branch still points to the commit you were on when you ran `git switch` to switch branches.
120120
Let's switch back to the `master` branch:
121121

122122
[source,console]
123123
----
124-
$ git checkout master
124+
$ git switch master
125125
----
126126

127127
[NOTE]
@@ -136,8 +136,8 @@ To show commit history for the desired branch you have to explicitly specify it:
136136
To show all of the branches, add `--all` to your `git log` command.
137137
====
138138

139-
.HEAD moves when you checkout
140-
image::images/checkout-master.png[HEAD moves when you checkout]
139+
.HEAD moves when you switch
140+
image::images/switch-to-master.png[HEAD moves when you switch]
141141

142142
That command did two things.
143143
It moved the HEAD pointer back to point to the `master` branch, and it reverted the files in your working directory back to the snapshot that `master` points to.
@@ -163,7 +163,7 @@ $ git commit -a -m 'Make other changes'
163163
Now your project history has diverged (see <<divergent_history>>).
164164
You created and switched to a branch, did some work on it, and then switched back to your main branch and did other work.
165165
Both of those changes are isolated in separate branches: you can switch back and forth between the branches and merge them together when you're ready.
166-
And you did all that with simple `branch`, `checkout`, and `commit` commands.
166+
And you did all that with simple `branch`, `switch`, and `commit` commands.
167167

168168
[[divergent_history]]
169169
.Divergent history
@@ -196,15 +196,22 @@ Let's see why you should do so.
196196
[NOTE]
197197
.Creating a new branch and switching to it at the same time
198198
====
199-
It's typical to create a new branch and want to switch to that new branch at the same time -- this can be done in one operation with `git checkout -b <newbranchname>`.
199+
It's typical to create a new branch and want to switch to that new branch at the same time -- this can be done in one operation with `git switch -c <newbranchname>`.
200+
201+
The `-c` flag stands for create, you can also use the full flag: `--create`.
202+
====
203+
204+
[TIP]
205+
====
206+
To return to your previously checked out branch, also `git switch -` works, so you don't have to type that branche's name.
200207
====
201208

202209
[NOTE]
203210
====
204-
From Git version 2.23 onwards you can use `git switch` instead of `git checkout` to:
211+
In Git versions before 2.23, `git switch` wasn't yet available.
212+
`git checkout` can be used instead:
205213
206-
- Switch to an existing branch: `git switch testing-branch`.
207-
- Create a new branch and switch to it: `git switch -c new-branch`.
208-
The `-c` flag stands for create, you can also use the full flag: `--create`.
209-
- Return to your previously checked out branch: `git switch -`.
214+
- Switch to an existing branch: `git checkout testing-branch --`.
215+
- Create a new branch and switch to it: `git checkout -b new-branch`.
216+
The `-b` flag stands for "`[create a new] **b**ranch`", but doesn't have a long-option equivalent.
210217
====

‎book/03-git-branching/sections/rebasing.asc‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ For this example, you would check out the `experiment` branch, and then rebase i
2727

2828
[source,console]
2929
----
30-
$ git checkout experiment
30+
$ git switch experiment
3131
$ git rebase master
3232
First, rewinding head to replay your work on top of it...
3333
Applying: added staged command
@@ -42,7 +42,7 @@ At this point, you can go back to the `master` branch and do a fast-forward merg
4242

4343
[source,console]
4444
----
45-
$ git checkout master
45+
$ git switch master
4646
$ git merge experiment
4747
----
4848

@@ -90,7 +90,7 @@ Now you can fast-forward your `master` branch (see <<rbdiag_g>>):
9090

9191
[source,console]
9292
----
93-
$ git checkout master
93+
$ git switch master
9494
$ git merge client
9595
----
9696

@@ -116,7 +116,7 @@ Then, you can fast-forward the base branch (`master`):
116116

117117
[source,console]
118118
----
119-
$ git checkout master
119+
$ git switch master
120120
$ git merge server
121121
----
122122

‎book/03-git-branching/sections/remote-branches.asc‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ If you want your own `serverfix` branch that you can work on, you can base it of
119119

120120
[source,console]
121121
----
122-
$ git checkout -b serverfix origin/serverfix
122+
$ git switch -c serverfix origin/serverfix
123123
Branch serverfix set up to track remote branch serverfix from origin.
124124
Switched to a new branch 'serverfix'
125125
----
@@ -136,12 +136,12 @@ If you're on a tracking branch and type `git pull`, Git automatically knows whic
136136

137137
When you clone a repository, it generally automatically creates a `master` branch that tracks `origin/master`.
138138
However, you can set up other tracking branches if you wish -- ones that track branches on other remotes, or don't track the `master` branch.
139-
The simple case is the example you just saw, running `git checkout -b <branch> <remote>/<branch>`.
139+
The simple case is the example you just saw, running `git switch -c <branch> <remote>/<branch>`.
140140
This is a common enough operation that Git provides the `--track` shorthand:
141141

142142
[source,console]
143143
----
144-
$ git checkout --track origin/serverfix
144+
$ git switch --track origin/serverfix
145145
Branch serverfix set up to track remote branch serverfix from origin.
146146
Switched to a new branch 'serverfix'
147147
----
@@ -151,7 +151,7 @@ If the branch name you're trying to checkout (a) doesn't exist and (b) exactly m
151151

152152
[source,console]
153153
----
154-
$ git checkout serverfix
154+
$ git switch serverfix
155155
Branch serverfix set up to track remote branch serverfix from origin.
156156
Switched to a new branch 'serverfix'
157157
----
@@ -160,7 +160,7 @@ To set up a local branch with a different name than the remote branch, you can e
160160

161161
[source,console]
162162
----
163-
$ git checkout -b sf origin/serverfix
163+
$ git switch -c sf origin/serverfix
164164
Branch sf set up to track remote branch serverfix from origin.
165165
Switched to a new branch 'sf'
166166
----

‎book/05-distributed-git/sections/contributing.asc‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ First (having committed all of the work on her `issue54` topic branch), Jessica
265265

266266
[source,console]
267267
----
268-
$ git checkout master
268+
$ git switch master
269269
Switched to branch 'master'
270270
Your branch is behind 'origin/master' by 2 commits, and can be fast-forwarded.
271271
----
@@ -341,7 +341,7 @@ She creates a new branch for the feature and does some work on it there:
341341
[source,console]
342342
----
343343
# Jessica's Machine
344-
$ git checkout -b featureA
344+
$ git switch -c featureA
345345
Switched to a new branch 'featureA'
346346
$ vim lib/simplegit.rb
347347
$ git commit -am 'Add limit to log function'
@@ -368,7 +368,7 @@ To begin, she starts a new feature branch, basing it off the server's `master` b
368368
----
369369
# Jessica's Machine
370370
$ git fetch origin
371-
$ git checkout -b featureB origin/master
371+
$ git switch -c featureB origin/master
372372
Switched to a new branch 'featureB'
373373
----
374374

@@ -456,7 +456,7 @@ If Jessica likes what she sees, she can merge John's new work into her local `fe
456456

457457
[source,console]
458458
----
459-
$ git checkout featureA
459+
$ git switch featureA
460460
Switched to branch 'featureA'
461461
$ git merge origin/featureA
462462
Updating 3300904..aad881d
@@ -513,7 +513,7 @@ The sequence looks basically like this:
513513
----
514514
$ git clone <url>
515515
$ cd project
516-
$ git checkout -b featureA
516+
$ git switch -c featureA
517517
... work ...
518518
$ git commit
519519
... work ...
@@ -579,7 +579,7 @@ For example, if you want to submit a second topic of work to the project, don't
579579

580580
[source,console]
581581
----
582-
$ git checkout -b featureB origin/master
582+
$ git switch -c featureB origin/master
583583
... work ...
584584
$ git commit
585585
$ git push myfork featureB
@@ -598,7 +598,7 @@ In this case, you can try to rebase that branch on top of `origin/master`, resol
598598

599599
[source,console]
600600
----
601-
$ git checkout featureA
601+
$ git switch featureA
602602
$ git rebase origin/master
603603
$ git push -f myfork featureA
604604
----
@@ -619,7 +619,7 @@ You start a new branch based off the current `origin/master` branch, squash the
619619
(((git commands, merge, squash)))
620620
[source,console]
621621
----
622-
$ git checkout -b featureBv2 origin/master
622+
$ git switch -c featureBv2 origin/master
623623
$ git merge --squash featureB
624624
... change implementation ...
625625
$ git commit
@@ -648,7 +648,7 @@ Instead of forking the project and pushing to your own writable version, you gen
648648

649649
[source,console]
650650
----
651-
$ git checkout -b topicA
651+
$ git switch -c topicA
652652
... work ...
653653
$ git commit
654654
... work ...

‎book/05-distributed-git/sections/maintaining.asc‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ As you'll remember, you can create the branch based off your `master` branch lik
1919
$ git branch sc/ruby_client master
2020
----
2121

22-
Or, if you want to also switch to it immediately, you can use the `checkout -b` option:
22+
Or, if you want to also switch to it immediately, you can use the `switch -c` option:
2323

2424
[source,console]
2525
----
26-
$ git checkout -b sc/ruby_client master
26+
$ git switch -c sc/ruby_client master
2727
----
2828

2929
Now you're ready to add the contributed work that you received into this topic branch and determine if you want to merge it into your longer-term branches.
@@ -193,7 +193,7 @@ For instance, if Jessica sends you an email saying that she has a great new feat
193193
----
194194
$ git remote add jessica https://github.com/jessica/myproject.git
195195
$ git fetch jessica
196-
$ git checkout -b rubyclient jessica/ruby-client
196+
$ git switch -c rubyclient jessica/ruby-client
197197
----
198198

199199
If she emails you again later with another branch containing another great feature, you could directly `fetch` and `checkout` because you already have the remote setup.
@@ -499,7 +499,7 @@ In fact, if you build Git from source code cloned from the Git repository, `git
499499
If you're describing a commit that you have directly tagged, it gives you simply the tag name.
500500
501501
By default, the `git describe` command requires annotated tags (tags created with the `-a` or `-s` flag); if you want to take advantage of lightweight (non-annotated) tags as well, add the `--tags` option to the command.
502-
You can also use this string as the target of a `git checkout` or `git show` command, although it relies on the abbreviated SHA-1 value at the end, so it may not be valid forever.
502+
You can also use this string as the target of a `git switch` or `git show` command, although it relies on the abbreviated SHA-1 value at the end, so it may not be valid forever.
503503
For instance, the Linux kernel recently jumped from 8 to 10 characters to ensure SHA-1 object uniqueness, so older `git describe` output names were invalidated.
504504
505505
[[_preparing_release]]

‎book/06-github/sections/2-contributing.asc‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ $ git clone https://github.com/tonychacon/blink <1>
7676
Cloning into 'blink'...
7777
7878
$ cd blink
79-
$ git checkout -b slow-blink <2>
79+
$ git switch -c slow-blink <2>
8080
Switched to a new branch 'slow-blink'
8181
8282
$ sed -i '' 's/1000/3000/' blink.ino (macOS) <3>
@@ -504,7 +504,7 @@ For example, if you forked from `https://github.com/progit/progit2.git`, you can
504504

505505
[source,console]
506506
----
507-
$ git checkout master <1>
507+
$ git switch master <1>
508508
$ git pull https://github.com/progit/progit2.git <2>
509509
$ git push origin master <3>
510510
----
@@ -534,7 +534,7 @@ Once this is done, the workflow becomes much simpler:
534534

535535
[source,console]
536536
----
537-
$ git checkout master <1>
537+
$ git switch master <1>
538538
$ git pull <2>
539539
$ git push <3>
540540
----

0 commit comments

Comments
 (0)