summaryrefslogtreecommitdiffstats
path: root/man1/git-checkout.1
blob: e4b3ddb81924f805ba24c9006ea3ac5e2ef24c86 (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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
'\" t
.\"     Title: git-checkout
.\"    Author: [FIXME: author] [see http://www.docbook.org/tdg5/en/html/author]
.\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
.\"      Date: 2024-04-22
.\"    Manual: Git Manual
.\"    Source: Git 2.45.0.rc0.3.g00e10ef10e
.\"  Language: English
.\"
.TH "GIT\-CHECKOUT" "1" "2024\-04\-22" "Git 2\&.45\&.0\&.rc0\&.3\&.g00" "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-checkout \- Switch branches or restore working tree files
.SH "SYNOPSIS"
.sp
.nf
\fIgit checkout\fR [\-q] [\-f] [\-m] [<branch>]
\fIgit checkout\fR [\-q] [\-f] [\-m] \-\-detach [<branch>]
\fIgit checkout\fR [\-q] [\-f] [\-m] [\-\-detach] <commit>
\fIgit checkout\fR [\-q] [\-f] [\-m] [[\-b|\-B|\-\-orphan] <new\-branch>] [<start\-point>]
\fIgit checkout\fR [\-f] <tree\-ish> [\-\-] <pathspec>\&...
\fIgit checkout\fR [\-f] <tree\-ish> \-\-pathspec\-from\-file=<file> [\-\-pathspec\-file\-nul]
\fIgit checkout\fR [\-f|\-\-ours|\-\-theirs|\-m|\-\-conflict=<style>] [\-\-] <pathspec>\&...
\fIgit checkout\fR [\-f|\-\-ours|\-\-theirs|\-m|\-\-conflict=<style>] \-\-pathspec\-from\-file=<file> [\-\-pathspec\-file\-nul]
\fIgit checkout\fR (\-p|\-\-patch) [<tree\-ish>] [\-\-] [<pathspec>\&...]
.fi
.sp
.SH "DESCRIPTION"
.sp
Updates files in the working tree to match the version in the index or the specified tree\&. If no pathspec was given, \fIgit checkout\fR will also update \fBHEAD\fR to set the specified branch as the current branch\&.
.PP
\fIgit checkout\fR [<branch>]
.RS 4
To prepare for working on
\fB<branch>\fR, switch to it by updating the index and the files in the working tree, and by pointing
\fBHEAD\fR
at the branch\&. Local modifications to the files in the working tree are kept, so that they can be committed to the
\fB<branch>\fR\&.
.sp
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 and
\fB\-\-no\-guess\fR
is not specified, treat as equivalent to
.sp
.if n \{\
.RS 4
.\}
.nf
$ git checkout \-b <branch> \-\-track <remote>/<branch>
.fi
.if n \{\
.RE
.\}
.sp
You could omit
\fB<branch>\fR, in which case the command degenerates to "check out the current branch", which is a glorified no\-op with rather expensive side\-effects to show only the tracking information, if it exists, for the current branch\&.
.RE
.PP
\fIgit checkout\fR \-b|\-B <new\-branch> [<start\-point>]
.RS 4
Specifying
\fB\-b\fR
causes a new branch to be created as if
\fBgit-branch\fR(1)
were called and then checked out\&. In this case you can use the
\fB\-\-track\fR
or
\fB\-\-no\-track\fR
options, which will be passed to
\fIgit branch\fR\&. As a convenience,
\fB\-\-track\fR
without
\fB\-b\fR
implies branch creation; see the description of
\fB\-\-track\fR
below\&.
.sp
If
\fB\-B\fR
is given,
\fB<new\-branch>\fR
is created if it doesn\(cqt exist; otherwise, it is reset\&. This is the transactional equivalent of
.sp
.if n \{\
.RS 4
.\}
.nf
$ git branch \-f <branch> [<start\-point>]
$ git checkout <branch>
.fi
.if n \{\
.RE
.\}
.sp
that is to say, the branch is not reset/created unless "git checkout" 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
\fIgit checkout\fR \-\-detach [<branch>], \fIgit checkout\fR [\-\-detach] <commit>
.RS 4
Prepare to work on top of
\fB<commit>\fR, by detaching
\fBHEAD\fR
at it (see "DETACHED HEAD" section), and updating the index and the files in the working tree\&. Local modifications to the files in the working tree are kept, so that the resulting working tree will be the state recorded in the commit plus the local modifications\&.
.sp
When the
\fB<commit>\fR
argument is a branch name, the
\fB\-\-detach\fR
option can be used to detach
\fBHEAD\fR
at the tip of the branch (\fBgit checkout <branch>\fR
would check out that branch without detaching
\fBHEAD\fR)\&.
.sp
Omitting
\fB<branch>\fR
detaches
\fBHEAD\fR
at the tip of the current branch\&.
.RE
.PP
\fIgit checkout\fR [\-f|\-\-ours|\-\-theirs|\-m|\-\-conflict=<style>] [<tree\-ish>] [\-\-] <pathspec>\&..., \fIgit checkout\fR [\-f|\-\-ours|\-\-theirs|\-m|\-\-conflict=<style>] [<tree\-ish>] \-\-pathspec\-from\-file=<file> [\-\-pathspec\-file\-nul]
.RS 4
Overwrite the contents of the files that match the pathspec\&. When the
\fB<tree\-ish>\fR
(most often a commit) is not given, overwrite working tree with the contents in the index\&. When the
\fB<tree\-ish>\fR
is given, overwrite both the index and the working tree with the contents at the
\fB<tree\-ish>\fR\&.
.sp
The index may contain unmerged entries because of a previous failed merge\&. By default, if you try to check out such an entry from the index, the checkout operation will fail and nothing will be checked out\&. Using
\fB\-f\fR
will ignore these unmerged entries\&. The contents from a specific side of the merge can be checked out of the index by using
\fB\-\-ours\fR
or
\fB\-\-theirs\fR\&. With
\fB\-m\fR, changes made to the working tree file can be discarded to re\-create the original conflicted merge result\&.
.RE
.PP
\fIgit checkout\fR (\-p|\-\-patch) [<tree\-ish>] [\-\-] [<pathspec>\&...]
.RS 4
This is similar to the previous mode, but lets you use the interactive interface to show the "diff" output and choose which hunks to use in the result\&. See below for the description of
\fB\-\-patch\fR
option\&.
.RE
.SH "OPTIONS"
.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
\-f, \-\-force
.RS 4
When switching branches, proceed even if the index or the working tree differs from
\fBHEAD\fR, and even if there are untracked files in the way\&. This is used to throw away local changes and any untracked files or directories that are in the way\&.
.sp
When checking out paths from the index, do not fail upon unmerged entries; instead, unmerged entries are ignored\&.
.RE
.PP
\-\-ours, \-\-theirs
.RS 4
When checking out paths from the index, check out stage #2 (\fIours\fR) or #3 (\fItheirs\fR) for unmerged paths\&.
.sp
Note that during
\fBgit rebase\fR
and
\fBgit pull \-\-rebase\fR,
\fIours\fR
and
\fItheirs\fR
may appear swapped;
\fB\-\-ours\fR
gives the version from the branch the changes are rebased onto, while
\fB\-\-theirs\fR
gives the version from the branch that holds your work that is being rebased\&.
.sp
This is because
\fBrebase\fR
is used in a workflow that treats the history at the remote as the shared canonical one, and treats the work done on the branch you are rebasing as the third\-party work to be integrated, and you are temporarily assuming the role of the keeper of the canonical history during the rebase\&. As the keeper of the canonical history, you need to view the history from the remote as
\fBours\fR
(i\&.e\&. "our shared canonical history"), while what you did on your side branch as
\fBtheirs\fR
(i\&.e\&. "one contributor\(cqs work on top of it")\&.
.RE
.PP
\-b <new\-branch>
.RS 4
Create a new branch named
\fB<new\-branch>\fR, start it at
\fB<start\-point>\fR, and check the resulting branch out; see
\fBgit-branch\fR(1)
for details\&.
.RE
.PP
\-B <new\-branch>
.RS 4
Creates the branch
\fB<new\-branch>\fR, start it at
\fB<start\-point>\fR; if it already exists, then reset it to
\fB<start\-point>\fR\&. And then check the resulting branch out\&. This is equivalent to running "git branch" with "\-f" followed by "git checkout" of that branch; see
\fBgit-branch\fR(1)
for details\&.
.RE
.PP
\-t, \-\-track[=(direct|inherit)]
.RS 4
When creating a new branch, set up "upstream" configuration\&. See "\-\-track" in
\fBgit-branch\fR(1)
for details\&.
.sp
If no
\fB\-b\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\-b\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
\-\-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 checkout \-b <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
\-l
.RS 4
Create the new branch\(cqs reflog; see
\fBgit-branch\fR(1)
for details\&.
.RE
.PP
\-d, \-\-detach
.RS 4
Rather than checking out a branch to work on it, check out a commit for inspection and discardable experiments\&. This is the default behavior of
\fBgit checkout <commit>\fR
when
\fB<commit>\fR
is not a branch name\&. See the "DETACHED HEAD" section below for details\&.
.RE
.PP
\-\-orphan <new\-branch>
.RS 4
Create a new unborn branch, named
\fB<new\-branch>\fR, started from
\fB<start\-point>\fR
and switch to it\&. The first commit made on this new branch will have no parents and it will be the root of a new history totally disconnected from all the other branches and commits\&.
.sp
The index and the working tree are adjusted as if you had previously run
\fBgit checkout <start\-point>\fR\&. This allows you to start a new history that records a set of paths similar to
\fB<start\-point>\fR
by easily running
\fBgit commit \-a\fR
to make the root commit\&.
.sp
This can be useful when you want to publish the tree from a commit without exposing its full history\&. You might want to do this to publish an open source branch of a project whose current tree is "clean", but whose full history contains proprietary or otherwise encumbered bits of code\&.
.sp
If you want to start a disconnected history that records a set of paths that is totally different from the one of
\fB<start\-point>\fR, then you should clear the index and the working tree right after creating the orphan branch by running
\fBgit rm \-rf \&.\fR
from the top level of the working tree\&. Afterwards you will be ready to prepare your new files, repopulating the working tree, by copying them from elsewhere, extracting a tarball, etc\&.
.RE
.PP
\-\-ignore\-skip\-worktree\-bits
.RS 4
In sparse checkout mode,
\fBgit checkout \-\- <paths>\fR
would update only entries matched by
\fB<paths>\fR
and sparse patterns in
\fB$GIT_DIR/info/sparse\-checkout\fR\&. This option ignores the sparse patterns and adds back any files in
\fB<paths>\fR\&.
.RE
.PP
\-m, \-\-merge
.RS 4
When switching branches, 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)\&.
.sp
When checking out paths from the index, this option lets you recreate the conflicted merge in the specified paths\&. This option cannot be used when checking out paths from a tree\-ish\&.
.sp
When switching branches with
\fB\-\-merge\fR, staged changes may be lost\&.
.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
\-p, \-\-patch
.RS 4
Interactively select hunks in the difference between the
\fB<tree\-ish>\fR
(or the index, if unspecified) and the working tree\&. The chosen hunks are then applied in reverse to the working tree (and if a
\fB<tree\-ish>\fR
was specified, the index)\&.
.sp
This means that you can use
\fBgit checkout \-p\fR
to selectively discard edits from your current working tree\&. See the \(lqInteractive Mode\(rq section of
\fBgit-add\fR(1)
to learn how to operate the
\fB\-\-patch\fR
mode\&.
.sp
Note that this option uses the no overlay mode by default (see also
\fB\-\-overlay\fR), and currently doesn\(cqt support overlay mode\&.
.RE
.PP
\-\-ignore\-other\-worktrees
.RS 4
\fBgit checkout\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
\-\-overwrite\-ignore, \-\-no\-overwrite\-ignore
.RS 4
Silently overwrite ignored files when switching branches\&. This is the default behavior\&. Use
\fB\-\-no\-overwrite\-ignore\fR
to abort the operation when the new branch contains ignored files\&.
.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 local modifications in a submodule would be overwritten the checkout will fail unless
\fB\-f\fR
is used\&. 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 submodule\&.
.RE
.PP
\-\-overlay, \-\-no\-overlay
.RS 4
In the default overlay mode,
\fBgit checkout\fR
never removes files from the index or the working tree\&. When specifying
\fB\-\-no\-overlay\fR, files that appear in the index and working tree, but not in
\fB<tree\-ish>\fR
are removed, to make them match
\fB<tree\-ish>\fR
exactly\&.
.RE
.PP
\-\-pathspec\-from\-file=<file>
.RS 4
Pathspec is passed in
\fB<file>\fR
instead of commandline args\&. If
\fB<file>\fR
is exactly
\fB\-\fR
then standard input is used\&. Pathspec elements are separated by LF or CR/LF\&. Pathspec elements can be quoted as explained for the configuration variable
\fBcore\&.quotePath\fR
(see
\fBgit-config\fR(1))\&. See also
\fB\-\-pathspec\-file\-nul\fR
and global
\fB\-\-literal\-pathspecs\fR\&.
.RE
.PP
\-\-pathspec\-file\-nul
.RS 4
Only meaningful with
\fB\-\-pathspec\-from\-file\fR\&. Pathspec elements are separated with NUL character and all other characters are taken literally (including newlines and quotes)\&.
.RE
.PP
<branch>
.RS 4
Branch to checkout; if it refers to a branch (i\&.e\&., a name that, when prepended with "refs/heads/", is a valid ref), then that branch is checked out\&. Otherwise, if it refers to a valid commit, your
\fBHEAD\fR
becomes "detached" and you are no longer on any branch (see below for details)\&.
.sp
You can use the
\fB@{\-N}\fR
syntax to refer to the N\-th last branch/commit checked out using "git checkout" operation\&. You may also specify
\fB\-\fR
which is synonymous to
\fB@{\-1}\fR\&.
.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
<new\-branch>
.RS 4
Name for the new branch\&.
.RE
.PP
<start\-point>
.RS 4
The name of a commit at which to start the new branch; see
\fBgit-branch\fR(1)
for details\&. Defaults to
\fBHEAD\fR\&.
.sp
As a special case, you may use
\fB"A\&.\&.\&.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
<tree\-ish>
.RS 4
Tree to checkout from (when paths are given)\&. If not specified, the index will be used\&.
.sp
As a special case, you may use
\fB"A\&.\&.\&.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
\-\-
.RS 4
Do not interpret any more arguments as options\&.
.RE
.PP
<pathspec>\&...
.RS 4
Limits the paths affected by the operation\&.
.sp
For more details, see the
\fIpathspec\fR
entry in
\fBgitglossary\fR(7)\&.
.RE
.SH "DETACHED HEAD"
.sp
\fBHEAD\fR normally refers to a named branch (e\&.g\&. \fBmaster\fR)\&. Meanwhile, each branch refers to a specific commit\&. Let\(cqs look at a repo with three commits, one of them tagged, and with branch \fBmaster\fR checked out:
.sp
.if n \{\
.RS 4
.\}
.nf
           HEAD (refers to branch \*(Aqmaster\*(Aq)
            |
            v
a\-\-\-b\-\-\-c  branch \*(Aqmaster\*(Aq (refers to commit \*(Aqc\*(Aq)
    ^
    |
  tag \*(Aqv2\&.0\*(Aq (refers to commit \*(Aqb\*(Aq)
.fi
.if n \{\
.RE
.\}
.sp
.sp
When a commit is created in this state, the branch is updated to refer to the new commit\&. Specifically, \fIgit commit\fR creates a new commit \fBd\fR, whose parent is commit \fBc\fR, and then updates branch \fBmaster\fR to refer to new commit \fBd\fR\&. \fBHEAD\fR still refers to branch \fBmaster\fR and so indirectly now refers to commit \fBd\fR:
.sp
.if n \{\
.RS 4
.\}
.nf
$ edit; git add; git commit

               HEAD (refers to branch \*(Aqmaster\*(Aq)
                |
                v
a\-\-\-b\-\-\-c\-\-\-d  branch \*(Aqmaster\*(Aq (refers to commit \*(Aqd\*(Aq)
    ^
    |
  tag \*(Aqv2\&.0\*(Aq (refers to commit \*(Aqb\*(Aq)
.fi
.if n \{\
.RE
.\}
.sp
.sp
It is sometimes useful to be able to checkout a commit that is not at the tip of any named branch, or even to create a new commit that is not referenced by a named branch\&. Let\(cqs look at what happens when we checkout commit \fBb\fR (here we show two ways this may be done):
.sp
.if n \{\
.RS 4
.\}
.nf
$ git checkout v2\&.0  # or
$ git checkout master^^

   HEAD (refers to commit \*(Aqb\*(Aq)
    |
    v
a\-\-\-b\-\-\-c\-\-\-d  branch \*(Aqmaster\*(Aq (refers to commit \*(Aqd\*(Aq)
    ^
    |
  tag \*(Aqv2\&.0\*(Aq (refers to commit \*(Aqb\*(Aq)
.fi
.if n \{\
.RE
.\}
.sp
.sp
Notice that regardless of which checkout command we use, \fBHEAD\fR now refers directly to commit \fBb\fR\&. This is known as being in detached \fBHEAD\fR state\&. It means simply that \fBHEAD\fR refers to a specific commit, as opposed to referring to a named branch\&. Let\(cqs see what happens when we create a commit:
.sp
.if n \{\
.RS 4
.\}
.nf
$ edit; git add; git commit

     HEAD (refers to commit \*(Aqe\*(Aq)
      |
      v
      e
     /
a\-\-\-b\-\-\-c\-\-\-d  branch \*(Aqmaster\*(Aq (refers to commit \*(Aqd\*(Aq)
    ^
    |
  tag \*(Aqv2\&.0\*(Aq (refers to commit \*(Aqb\*(Aq)
.fi
.if n \{\
.RE
.\}
.sp
.sp
There is now a new commit \fBe\fR, but it is referenced only by \fBHEAD\fR\&. We can of course add yet another commit in this state:
.sp
.if n \{\
.RS 4
.\}
.nf
$ edit; git add; git commit

         HEAD (refers to commit \*(Aqf\*(Aq)
          |
          v
      e\-\-\-f
     /
a\-\-\-b\-\-\-c\-\-\-d  branch \*(Aqmaster\*(Aq (refers to commit \*(Aqd\*(Aq)
    ^
    |
  tag \*(Aqv2\&.0\*(Aq (refers to commit \*(Aqb\*(Aq)
.fi
.if n \{\
.RE
.\}
.sp
.sp
In fact, we can perform all the normal Git operations\&. But, let\(cqs look at what happens when we then checkout \fBmaster\fR:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git checkout master

               HEAD (refers to branch \*(Aqmaster\*(Aq)
      e\-\-\-f     |
     /          v
a\-\-\-b\-\-\-c\-\-\-d  branch \*(Aqmaster\*(Aq (refers to commit \*(Aqd\*(Aq)
    ^
    |
  tag \*(Aqv2\&.0\*(Aq (refers to commit \*(Aqb\*(Aq)
.fi
.if n \{\
.RE
.\}
.sp
.sp
It is important to realize that at this point nothing refers to commit \fBf\fR\&. Eventually commit \fBf\fR (and by extension commit \fBe\fR) will be deleted by the routine Git garbage collection process, unless we create a reference before that happens\&. If we have not yet moved away from commit \fBf\fR, any of these will create a reference to it:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git checkout \-b foo  # or "git switch \-c foo"  \fB(1)\fR
$ git branch foo                                 \fB(2)\fR
$ git tag foo                                    \fB(3)\fR
.fi
.if n \{\
.RE
.\}
.sp
.TS
tab(:);
r lw(\n(.lu*75u/100u).
\fB1.\fR\h'-2n':T{
creates a new branch
\fBfoo\fR, which refers to commit
\fBf\fR, and then updates
\fBHEAD\fR
to refer to branch
\fBfoo\fR\&. In other words, we\(cqll no longer be in detached
\fBHEAD\fR
state after this command\&.
T}
\fB2.\fR\h'-2n':T{
similarly creates a new branch
\fBfoo\fR, which refers to commit
\fBf\fR, but leaves
\fBHEAD\fR
detached\&.
T}
\fB3.\fR\h'-2n':T{
creates a new tag
\fBfoo\fR, which refers to commit
\fBf\fR, leaving
\fBHEAD\fR
detached\&.
T}
.TE
.sp
If we have moved away from commit \fBf\fR, then we must first recover its object name (typically by using git reflog), and then we can create a reference to it\&. For example, to see the last two commits to which \fBHEAD\fR referred, we can use either of these commands:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git reflog \-2 HEAD # or
$ git log \-g \-2 HEAD
.fi
.if n \{\
.RE
.\}
.sp
.SH "ARGUMENT DISAMBIGUATION"
.sp
When there is only one argument given and it is not \fB\-\-\fR (e\&.g\&. \fBgit checkout abc\fR), and when the argument is both a valid \fB<tree\-ish>\fR (e\&.g\&. a branch \fBabc\fR exists) and a valid \fB<pathspec>\fR (e\&.g\&. a file or a directory whose name is "abc" exists), Git would usually ask you to disambiguate\&. Because checking out a branch is so common an operation, however, \fBgit checkout abc\fR takes "abc" as a \fB<tree\-ish>\fR in such a situation\&. Use \fBgit checkout \-\- <pathspec>\fR if you want to checkout these paths out of the index\&.
.SH "EXAMPLES"
.SS "1\&. Paths"
.sp
The following sequence checks out the \fBmaster\fR branch, reverts the \fBMakefile\fR to two revisions back, deletes \fBhello\&.c\fR by mistake, and gets it back from the index\&.
.sp
.if n \{\
.RS 4
.\}
.nf
$ git checkout master             \fB(1)\fR
$ git checkout master~2 Makefile  \fB(2)\fR
$ rm \-f hello\&.c
$ git checkout hello\&.c            \fB(3)\fR
.fi
.if n \{\
.RE
.\}
.sp
.TS
tab(:);
r lw(\n(.lu*75u/100u).
\fB1.\fR\h'-2n':T{
switch branch
T}
\fB2.\fR\h'-2n':T{
take a file out of another commit
T}
\fB3.\fR\h'-2n':T{
restore
\fBhello\&.c\fR
from the index
T}
.TE
.sp
If you want to check out \fIall\fR C source files out of the index, you can say
.sp
.if n \{\
.RS 4
.\}
.nf
$ git checkout \-\- \*(Aq*\&.c\*(Aq
.fi
.if n \{\
.RE
.\}
.sp
.sp
Note the quotes around \fB*\&.c\fR\&. The file \fBhello\&.c\fR will also be checked out, even though it is no longer in the working tree, because the file globbing is used to match entries in the index (not in the working tree by the shell)\&.
.sp
If you have an unfortunate branch that is named \fBhello\&.c\fR, this step would be confused as an instruction to switch to that branch\&. You should instead write:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git checkout \-\- hello\&.c
.fi
.if n \{\
.RE
.\}
.sp
.SS "2\&. Merge"
.sp
After working in the wrong branch, switching to the correct branch would be done using:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git checkout mytopic
.fi
.if n \{\
.RE
.\}
.sp
.sp
However, your "wrong" branch and correct \fBmytopic\fR branch may differ in files that you have modified locally, in which case the above checkout would fail like this:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git checkout 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 checkout \-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\&.
.SS "3\&. Merge conflict"
.sp
When a merge conflict happens during switching branches with the \fB\-m\fR option, you would see something like this:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git checkout \-m mytopic
Auto\-merging frotz
ERROR: Merge conflict in frotz
fatal: merge program failed
.fi
.if n \{\
.RE
.\}
.sp
.sp
At this point, \fBgit diff\fR shows the changes cleanly merged as in the previous example, as well as the changes in the conflicted files\&. Edit and resolve the conflict and mark it resolved with \fBgit add\fR as usual:
.sp
.if n \{\
.RS 4
.\}
.nf
$ edit frotz
$ git add frotz
.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-switch\fR(1), \fBgit-restore\fR(1)
.SH "GIT"
.sp
Part of the \fBgit\fR(1) suite