summaryrefslogtreecommitdiffstats
path: root/man1/git-switch.1
blob: 4315c96fc9ae39379857dc7fc46b5916bfb3bd9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
'\" t
.\"     Title: git-switch
.\"    Author: [FIXME: author] [see http://www.docbook.org/tdg5/en/html/author]
.\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
.\"      Date: 2024-04-15
.\"    Manual: Git Manual
.\"    Source: Git 2.44.0.616.g548fe35913
.\"  Language: English
.\"
.TH "GIT\-SWITCH" "1" "2024\-04\-15" "Git 2\&.44\&.0\&.616\&.g548fe3" "Git Manual"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
git-switch \- Switch branches
.SH "SYNOPSIS"
.sp
.nf
\fIgit switch\fR [<options>] [\-\-no\-guess] <branch>
\fIgit switch\fR [<options>] \-\-detach [<start\-point>]
\fIgit switch\fR [<options>] (\-c|\-C) <new\-branch> [<start\-point>]
\fIgit switch\fR [<options>] \-\-orphan <new\-branch>
.fi
.sp
.SH "DESCRIPTION"
.sp
Switch to a specified branch\&. The working tree and the index are updated to match the branch\&. All new commits will be added to the tip of this branch\&.
.sp
Optionally a new branch could be created with either \fB\-c\fR, \fB\-C\fR, automatically from a remote branch of same name (see \fB\-\-guess\fR), or detach the working tree from any branch with \fB\-\-detach\fR, along with switching\&.
.sp
Switching branches does not require a clean index and working tree (i\&.e\&. no differences compared to \fBHEAD\fR)\&. The operation is aborted however if the operation leads to loss of local changes, unless told otherwise with \fB\-\-discard\-changes\fR or \fB\-\-merge\fR\&.
.sp
THIS COMMAND IS EXPERIMENTAL\&. THE BEHAVIOR MAY CHANGE\&.
.SH "OPTIONS"
.PP
<branch>
.RS 4
Branch to switch to\&.
.RE
.PP
<new\-branch>
.RS 4
Name for the new branch\&.
.RE
.PP
<start\-point>
.RS 4
The starting point for the new branch\&. Specifying a
\fB<start\-point>\fR
allows you to create a branch based on some other point in history than where HEAD currently points\&. (Or, in the case of
\fB\-\-detach\fR, allows you to inspect and detach from some other point\&.)
.sp
You can use the
\fB@{\-N}\fR
syntax to refer to the N\-th last branch/commit switched to using "git switch" or "git checkout" operation\&. You may also specify
\fB\-\fR
which is synonymous to
\fB@{\-1}\fR\&. This is often used to switch quickly between two branches, or to undo a branch switch by mistake\&.
.sp
As a special case, you may use
\fBA\&.\&.\&.B\fR
as a shortcut for the merge base of
\fBA\fR
and
\fBB\fR
if there is exactly one merge base\&. You can leave out at most one of
\fBA\fR
and
\fBB\fR, in which case it defaults to
\fBHEAD\fR\&.
.RE
.PP
\-c <new\-branch>, \-\-create <new\-branch>
.RS 4
Create a new branch named
\fB<new\-branch>\fR
starting at
\fB<start\-point>\fR
before switching to the branch\&. This is the transactional equivalent of
.sp
.if n \{\
.RS 4
.\}
.nf
$ git branch <new\-branch>
$ git switch <new\-branch>
.fi
.if n \{\
.RE
.\}
.sp
that is to say, the branch is not reset/created unless "git switch" is successful (e\&.g\&., when the branch is in use in another worktree, not just the current branch stays the same, but the branch is not reset to the start\-point, either)\&.
.RE
.PP
\-C <new\-branch>, \-\-force\-create <new\-branch>
.RS 4
Similar to
\fB\-\-create\fR
except that if
\fB<new\-branch>\fR
already exists, it will be reset to
\fB<start\-point>\fR\&. This is a convenient shortcut for:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git branch \-f <new\-branch>
$ git switch <new\-branch>
.fi
.if n \{\
.RE
.\}
.sp
.RE
.PP
\-d, \-\-detach
.RS 4
Switch to a commit for inspection and discardable experiments\&. See the "DETACHED HEAD" section in
\fBgit-checkout\fR(1)
for details\&.
.RE
.PP
\-\-guess, \-\-no\-guess
.RS 4
If
\fB<branch>\fR
is not found but there does exist a tracking branch in exactly one remote (call it
\fB<remote>\fR) with a matching name, treat as equivalent to
.sp
.if n \{\
.RS 4
.\}
.nf
$ git switch \-c <branch> \-\-track <remote>/<branch>
.fi
.if n \{\
.RE
.\}
.sp
If the branch exists in multiple remotes and one of them is named by the
\fBcheckout\&.defaultRemote\fR
configuration variable, we\(cqll use that one for the purposes of disambiguation, even if the
\fB<branch>\fR
isn\(cqt unique across all remotes\&. Set it to e\&.g\&.
\fBcheckout\&.defaultRemote=origin\fR
to always checkout remote branches from there if
\fB<branch>\fR
is ambiguous but exists on the
\fIorigin\fR
remote\&. See also
\fBcheckout\&.defaultRemote\fR
in
\fBgit-config\fR(1)\&.
.sp
\fB\-\-guess\fR
is the default behavior\&. Use
\fB\-\-no\-guess\fR
to disable it\&.
.sp
The default behavior can be set via the
\fBcheckout\&.guess\fR
configuration variable\&.
.RE
.PP
\-f, \-\-force
.RS 4
An alias for
\fB\-\-discard\-changes\fR\&.
.RE
.PP
\-\-discard\-changes
.RS 4
Proceed even if the index or the working tree differs from
\fBHEAD\fR\&. Both the index and working tree are restored to match the switching target\&. If
\fB\-\-recurse\-submodules\fR
is specified, submodule content is also restored to match the switching target\&. This is used to throw away local changes\&.
.RE
.PP
\-m, \-\-merge
.RS 4
If you have local modifications to one or more files that are different between the current branch and the branch to which you are switching, the command refuses to switch branches in order to preserve your modifications in context\&. However, with this option, a three\-way merge between the current branch, your working tree contents, and the new branch is done, and you will be on the new branch\&.
.sp
When a merge conflict happens, the index entries for conflicting paths are left unmerged, and you need to resolve the conflicts and mark the resolved paths with
\fBgit add\fR
(or
\fBgit rm\fR
if the merge should result in deletion of the path)\&.
.RE
.PP
\-\-conflict=<style>
.RS 4
The same as
\fB\-\-merge\fR
option above, but changes the way the conflicting hunks are presented, overriding the
\fBmerge\&.conflictStyle\fR
configuration variable\&. Possible values are "merge" (default), "diff3", and "zdiff3"\&.
.RE
.PP
\-q, \-\-quiet
.RS 4
Quiet, suppress feedback messages\&.
.RE
.PP
\-\-progress, \-\-no\-progress
.RS 4
Progress status is reported on the standard error stream by default when it is attached to a terminal, unless
\fB\-\-quiet\fR
is specified\&. This flag enables progress reporting even if not attached to a terminal, regardless of
\fB\-\-quiet\fR\&.
.RE
.PP
\-t, \-\-track [direct|inherit]
.RS 4
When creating a new branch, set up "upstream" configuration\&.
\fB\-c\fR
is implied\&. See
\fB\-\-track\fR
in
\fBgit-branch\fR(1)
for details\&.
.sp
If no
\fB\-c\fR
option is given, the name of the new branch will be derived from the remote\-tracking branch, by looking at the local part of the refspec configured for the corresponding remote, and then stripping the initial part up to the "*"\&. This would tell us to use
\fBhack\fR
as the local branch when branching off of
\fBorigin/hack\fR
(or
\fBremotes/origin/hack\fR, or even
\fBrefs/remotes/origin/hack\fR)\&. If the given name has no slash, or the above guessing results in an empty name, the guessing is aborted\&. You can explicitly give a name with
\fB\-c\fR
in such a case\&.
.RE
.PP
\-\-no\-track
.RS 4
Do not set up "upstream" configuration, even if the
\fBbranch\&.autoSetupMerge\fR
configuration variable is true\&.
.RE
.PP
\-\-orphan <new\-branch>
.RS 4
Create a new unborn branch, named
\fB<new\-branch>\fR\&. All tracked files are removed\&.
.RE
.PP
\-\-ignore\-other\-worktrees
.RS 4
\fBgit switch\fR
refuses when the wanted ref is already checked out by another worktree\&. This option makes it check the ref out anyway\&. In other words, the ref can be held by more than one worktree\&.
.RE
.PP
\-\-recurse\-submodules, \-\-no\-recurse\-submodules
.RS 4
Using
\fB\-\-recurse\-submodules\fR
will update the content of all active submodules according to the commit recorded in the superproject\&. If nothing (or
\fB\-\-no\-recurse\-submodules\fR) is used, submodules working trees will not be updated\&. Just like
\fBgit-submodule\fR(1), this will detach
\fBHEAD\fR
of the submodules\&.
.RE
.SH "EXAMPLES"
.sp
The following command switches to the "master" branch:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git switch master
.fi
.if n \{\
.RE
.\}
.sp
.sp
After working in the wrong branch, switching to the correct branch would be done using:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git switch mytopic
.fi
.if n \{\
.RE
.\}
.sp
.sp
However, your "wrong" branch and correct "mytopic" branch may differ in files that you have modified locally, in which case the above switch would fail like this:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git switch mytopic
error: You have local changes to \*(Aqfrotz\*(Aq; not switching branches\&.
.fi
.if n \{\
.RE
.\}
.sp
.sp
You can give the \fB\-m\fR flag to the command, which would try a three\-way merge:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git switch \-m mytopic
Auto\-merging frotz
.fi
.if n \{\
.RE
.\}
.sp
.sp
After this three\-way merge, the local modifications are \fInot\fR registered in your index file, so \fBgit diff\fR would show you what changes you made since the tip of the new branch\&.
.sp
To switch back to the previous branch before we switched to mytopic (i\&.e\&. "master" branch):
.sp
.if n \{\
.RS 4
.\}
.nf
$ git switch \-
.fi
.if n \{\
.RE
.\}
.sp
.sp
You can grow a new branch from any commit\&. For example, switch to "HEAD~3" and create branch "fixup":
.sp
.if n \{\
.RS 4
.\}
.nf
$ git switch \-c fixup HEAD~3
Switched to a new branch \*(Aqfixup\*(Aq
.fi
.if n \{\
.RE
.\}
.sp
.sp
If you want to start a new branch from a remote branch of the same name:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git switch new\-topic
Branch \*(Aqnew\-topic\*(Aq set up to track remote branch \*(Aqnew\-topic\*(Aq from \*(Aqorigin\*(Aq
Switched to a new branch \*(Aqnew\-topic\*(Aq
.fi
.if n \{\
.RE
.\}
.sp
.sp
To check out commit \fBHEAD~3\fR for temporary inspection or experiment without creating a new branch:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git switch \-\-detach HEAD~3
HEAD is now at 9fc9555312 Merge branch \*(Aqcc/shared\-index\-permbits\*(Aq
.fi
.if n \{\
.RE
.\}
.sp
.sp
If it turns out whatever you have done is worth keeping, you can always create a new name for it (without switching away):
.sp
.if n \{\
.RS 4
.\}
.nf
$ git switch \-c good\-surprises
.fi
.if n \{\
.RE
.\}
.sp
.SH "CONFIGURATION"
.sp
Everything below this line in this section is selectively included from the \fBgit-config\fR(1) documentation\&. The content is the same as what\(cqs found there:
.PP
checkout\&.defaultRemote
.RS 4
When you run
\fBgit checkout <something>\fR
or
\fBgit switch <something>\fR
and only have one remote, it may implicitly fall back on checking out and tracking e\&.g\&.
\fBorigin/<something>\fR\&. This stops working as soon as you have more than one remote with a
\fB<something>\fR
reference\&. This setting allows for setting the name of a preferred remote that should always win when it comes to disambiguation\&. The typical use\-case is to set this to
\fBorigin\fR\&.
.sp
Currently this is used by
\fBgit-switch\fR(1)
and
\fBgit-checkout\fR(1)
when
\fBgit checkout <something>\fR
or
\fBgit switch <something>\fR
will checkout the
\fB<something>\fR
branch on another remote, and by
\fBgit-worktree\fR(1)
when
\fBgit worktree add\fR
refers to a remote branch\&. This setting might be used for other checkout\-like commands or functionality in the future\&.
.RE
.PP
checkout\&.guess
.RS 4
Provides the default value for the
\fB\-\-guess\fR
or
\fB\-\-no\-guess\fR
option in
\fBgit checkout\fR
and
\fBgit switch\fR\&. See
\fBgit-switch\fR(1)
and
\fBgit-checkout\fR(1)\&.
.RE
.PP
checkout\&.workers
.RS 4
The number of parallel workers to use when updating the working tree\&. The default is one, i\&.e\&. sequential execution\&. If set to a value less than one, Git will use as many workers as the number of logical cores available\&. This setting and
\fBcheckout\&.thresholdForParallelism\fR
affect all commands that perform checkout\&. E\&.g\&. checkout, clone, reset, sparse\-checkout, etc\&.
.sp
Note: Parallel checkout usually delivers better performance for repositories located on SSDs or over NFS\&. For repositories on spinning disks and/or machines with a small number of cores, the default sequential checkout often performs better\&. The size and compression level of a repository might also influence how well the parallel version performs\&.
.RE
.PP
checkout\&.thresholdForParallelism
.RS 4
When running parallel checkout with a small number of files, the cost of subprocess spawning and inter\-process communication might outweigh the parallelization gains\&. This setting allows you to define the minimum number of files for which parallel checkout should be attempted\&. The default is 100\&.
.RE
.SH "SEE ALSO"
.sp
\fBgit-checkout\fR(1), \fBgit-branch\fR(1)
.SH "GIT"
.sp
Part of the \fBgit\fR(1) suite