1GIT-CHECKOUT(1)                   Git Manual                   GIT-CHECKOUT(1)
2
3
4

NAME

6       git-checkout - Switch branches or restore working tree files
7

SYNOPSIS

9       git checkout [-q] [-f] [-m] [<branch>]
10       git checkout [-q] [-f] [-m] --detach [<branch>]
11       git checkout [-q] [-f] [-m] [--detach] <commit>
12       git checkout [-q] [-f] [-m] [[-b|-B|--orphan] <new-branch>] [<start-point>]
13       git checkout [-f] <tree-ish> [--] <pathspec>...
14       git checkout [-f] <tree-ish> --pathspec-from-file=<file> [--pathspec-file-nul]
15       git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [--] <pathspec>...
16       git checkout [-f|--ours|--theirs|-m|--conflict=<style>] --pathspec-from-file=<file> [--pathspec-file-nul]
17       git checkout (-p|--patch) [<tree-ish>] [--] [<pathspec>...]
18

DESCRIPTION

20       Updates files in the working tree to match the version in the index or
21       the specified tree. If no pathspec was given, git checkout will also
22       update HEAD to set the specified branch as the current branch.
23
24       git checkout [<branch>]
25           To prepare for working on <branch>, switch to it by updating the
26           index and the files in the working tree, and by pointing HEAD at
27           the branch. Local modifications to the files in the working tree
28           are kept, so that they can be committed to the <branch>.
29
30           If <branch> is not found but there does exist a tracking branch in
31           exactly one remote (call it <remote>) with a matching name and
32           --no-guess is not specified, treat as equivalent to
33
34               $ git checkout -b <branch> --track <remote>/<branch>
35
36           You could omit <branch>, in which case the command degenerates to
37           "check out the current branch", which is a glorified no-op with
38           rather expensive side-effects to show only the tracking
39           information, if it exists, for the current branch.
40
41       git checkout -b|-B <new-branch> [<start-point>]
42           Specifying -b causes a new branch to be created as if git-branch(1)
43           were called and then checked out. In this case you can use the
44           --track or --no-track options, which will be passed to git branch.
45           As a convenience, --track without -b implies branch creation; see
46           the description of --track below.
47
48           If -B is given, <new-branch> is created if it doesn’t exist;
49           otherwise, it is reset. This is the transactional equivalent of
50
51               $ git branch -f <branch> [<start-point>]
52               $ git checkout <branch>
53
54           that is to say, the branch is not reset/created unless "git
55           checkout" is successful.
56
57       git checkout --detach [<branch>], git checkout [--detach] <commit>
58           Prepare to work on top of <commit>, by detaching HEAD at it (see
59           "DETACHED HEAD" section), and updating the index and the files in
60           the working tree. Local modifications to the files in the working
61           tree are kept, so that the resulting working tree will be the state
62           recorded in the commit plus the local modifications.
63
64           When the <commit> argument is a branch name, the --detach option
65           can be used to detach HEAD at the tip of the branch (git checkout
66           <branch> would check out that branch without detaching HEAD).
67
68           Omitting <branch> detaches HEAD at the tip of the current branch.
69
70       git checkout [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>]
71       [--] <pathspec>..., git checkout
72       [-f|--ours|--theirs|-m|--conflict=<style>] [<tree-ish>]
73       --pathspec-from-file=<file> [--pathspec-file-nul]
74           Overwrite the contents of the files that match the pathspec. When
75           the <tree-ish> (most often a commit) is not given, overwrite
76           working tree with the contents in the index. When the <tree-ish> is
77           given, overwrite both the index and the working tree with the
78           contents at the <tree-ish>.
79
80           The index may contain unmerged entries because of a previous failed
81           merge. By default, if you try to check out such an entry from the
82           index, the checkout operation will fail and nothing will be checked
83           out. Using -f will ignore these unmerged entries. The contents from
84           a specific side of the merge can be checked out of the index by
85           using --ours or --theirs. With -m, changes made to the working tree
86           file can be discarded to re-create the original conflicted merge
87           result.
88
89       git checkout (-p|--patch) [<tree-ish>] [--] [<pathspec>...]
90           This is similar to the previous mode, but lets you use the
91           interactive interface to show the "diff" output and choose which
92           hunks to use in the result. See below for the description of
93           --patch option.
94

OPTIONS

96       -q, --quiet
97           Quiet, suppress feedback messages.
98
99       --progress, --no-progress
100           Progress status is reported on the standard error stream by default
101           when it is attached to a terminal, unless --quiet is specified.
102           This flag enables progress reporting even if not attached to a
103           terminal, regardless of --quiet.
104
105       -f, --force
106           When switching branches, proceed even if the index or the working
107           tree differs from HEAD, and even if there are untracked files in
108           the way. This is used to throw away local changes and any untracked
109           files or directories that are in the way.
110
111           When checking out paths from the index, do not fail upon unmerged
112           entries; instead, unmerged entries are ignored.
113
114       --ours, --theirs
115           When checking out paths from the index, check out stage #2 (ours)
116           or #3 (theirs) for unmerged paths.
117
118           Note that during git rebase and git pull --rebase, ours and theirs
119           may appear swapped; --ours gives the version from the branch the
120           changes are rebased onto, while --theirs gives the version from the
121           branch that holds your work that is being rebased.
122
123           This is because rebase is used in a workflow that treats the
124           history at the remote as the shared canonical one, and treats the
125           work done on the branch you are rebasing as the third-party work to
126           be integrated, and you are temporarily assuming the role of the
127           keeper of the canonical history during the rebase. As the keeper of
128           the canonical history, you need to view the history from the remote
129           as ours (i.e. "our shared canonical history"), while what you did
130           on your side branch as theirs (i.e. "one contributor’s work on top
131           of it").
132
133       -b <new-branch>
134           Create a new branch named <new-branch>, start it at <start-point>,
135           and check the resulting branch out; see git-branch(1) for details.
136
137       -B <new-branch>
138           Creates the branch <new-branch>, start it at <start-point>; if it
139           already exists, then reset it to <start-point>. And then check the
140           resulting branch out. This is equivalent to running "git branch"
141           with "-f" followed by "git checkout" of that branch; see git-
142           branch(1) for details.
143
144       -t, --track[=(direct|inherit)]
145           When creating a new branch, set up "upstream" configuration. See
146           "--track" in git-branch(1) for details.
147
148           If no -b option is given, the name of the new branch will be
149           derived from the remote-tracking branch, by looking at the local
150           part of the refspec configured for the corresponding remote, and
151           then stripping the initial part up to the "*". This would tell us
152           to use hack as the local branch when branching off of origin/hack
153           (or remotes/origin/hack, or even refs/remotes/origin/hack). If the
154           given name has no slash, or the above guessing results in an empty
155           name, the guessing is aborted. You can explicitly give a name with
156           -b in such a case.
157
158       --no-track
159           Do not set up "upstream" configuration, even if the
160           branch.autoSetupMerge configuration variable is true.
161
162       --guess, --no-guess
163           If <branch> is not found but there does exist a tracking branch in
164           exactly one remote (call it <remote>) with a matching name, treat
165           as equivalent to
166
167               $ git checkout -b <branch> --track <remote>/<branch>
168
169           If the branch exists in multiple remotes and one of them is named
170           by the checkout.defaultRemote configuration variable, we’ll use
171           that one for the purposes of disambiguation, even if the <branch>
172           isn’t unique across all remotes. Set it to e.g.
173           checkout.defaultRemote=origin to always checkout remote branches
174           from there if <branch> is ambiguous but exists on the origin
175           remote. See also checkout.defaultRemote in git-config(1).
176
177           --guess is the default behavior. Use --no-guess to disable it.
178
179           The default behavior can be set via the checkout.guess
180           configuration variable.
181
182       -l
183           Create the new branch’s reflog; see git-branch(1) for details.
184
185       -d, --detach
186           Rather than checking out a branch to work on it, check out a commit
187           for inspection and discardable experiments. This is the default
188           behavior of git checkout <commit> when <commit> is not a branch
189           name. See the "DETACHED HEAD" section below for details.
190
191       --orphan <new-branch>
192           Create a new orphan branch, named <new-branch>, started from
193           <start-point> and switch to it. The first commit made on this new
194           branch will have no parents and it will be the root of a new
195           history totally disconnected from all the other branches and
196           commits.
197
198           The index and the working tree are adjusted as if you had
199           previously run git checkout <start-point>. This allows you to start
200           a new history that records a set of paths similar to <start-point>
201           by easily running git commit -a to make the root commit.
202
203           This can be useful when you want to publish the tree from a commit
204           without exposing its full history. You might want to do this to
205           publish an open source branch of a project whose current tree is
206           "clean", but whose full history contains proprietary or otherwise
207           encumbered bits of code.
208
209           If you want to start a disconnected history that records a set of
210           paths that is totally different from the one of <start-point>, then
211           you should clear the index and the working tree right after
212           creating the orphan branch by running git rm -rf .  from the top
213           level of the working tree. Afterwards you will be ready to prepare
214           your new files, repopulating the working tree, by copying them from
215           elsewhere, extracting a tarball, etc.
216
217       --ignore-skip-worktree-bits
218           In sparse checkout mode, git checkout -- <paths> would update only
219           entries matched by <paths> and sparse patterns in
220           $GIT_DIR/info/sparse-checkout. This option ignores the sparse
221           patterns and adds back any files in <paths>.
222
223       -m, --merge
224           When switching branches, if you have local modifications to one or
225           more files that are different between the current branch and the
226           branch to which you are switching, the command refuses to switch
227           branches in order to preserve your modifications in context.
228           However, with this option, a three-way merge between the current
229           branch, your working tree contents, and the new branch is done, and
230           you will be on the new branch.
231
232           When a merge conflict happens, the index entries for conflicting
233           paths are left unmerged, and you need to resolve the conflicts and
234           mark the resolved paths with git add (or git rm if the merge should
235           result in deletion of the path).
236
237           When checking out paths from the index, this option lets you
238           recreate the conflicted merge in the specified paths. This option
239           cannot be used when checking out paths from a tree-ish.
240
241           When switching branches with --merge, staged changes may be lost.
242
243       --conflict=<style>
244           The same as --merge option above, but changes the way the
245           conflicting hunks are presented, overriding the merge.conflictStyle
246           configuration variable. Possible values are "merge" (default),
247           "diff3", and "zdiff3".
248
249       -p, --patch
250           Interactively select hunks in the difference between the <tree-ish>
251           (or the index, if unspecified) and the working tree. The chosen
252           hunks are then applied in reverse to the working tree (and if a
253           <tree-ish> was specified, the index).
254
255           This means that you can use git checkout -p to selectively discard
256           edits from your current working tree. See the “Interactive Mode”
257           section of git-add(1) to learn how to operate the --patch mode.
258
259           Note that this option uses the no overlay mode by default (see also
260           --overlay), and currently doesn’t support overlay mode.
261
262       --ignore-other-worktrees
263           git checkout refuses when the wanted ref is already checked out by
264           another worktree. This option makes it check the ref out anyway. In
265           other words, the ref can be held by more than one worktree.
266
267       --overwrite-ignore, --no-overwrite-ignore
268           Silently overwrite ignored files when switching branches. This is
269           the default behavior. Use --no-overwrite-ignore to abort the
270           operation when the new branch contains ignored files.
271
272       --recurse-submodules, --no-recurse-submodules
273           Using --recurse-submodules will update the content of all active
274           submodules according to the commit recorded in the superproject. If
275           local modifications in a submodule would be overwritten the
276           checkout will fail unless -f is used. If nothing (or
277           --no-recurse-submodules) is used, submodules working trees will not
278           be updated. Just like git-submodule(1), this will detach HEAD of
279           the submodule.
280
281       --overlay, --no-overlay
282           In the default overlay mode, git checkout never removes files from
283           the index or the working tree. When specifying --no-overlay, files
284           that appear in the index and working tree, but not in <tree-ish>
285           are removed, to make them match <tree-ish> exactly.
286
287       --pathspec-from-file=<file>
288           Pathspec is passed in <file> instead of commandline args. If <file>
289           is exactly - then standard input is used. Pathspec elements are
290           separated by LF or CR/LF. Pathspec elements can be quoted as
291           explained for the configuration variable core.quotePath (see git-
292           config(1)). See also --pathspec-file-nul and global
293           --literal-pathspecs.
294
295       --pathspec-file-nul
296           Only meaningful with --pathspec-from-file. Pathspec elements are
297           separated with NUL character and all other characters are taken
298           literally (including newlines and quotes).
299
300       <branch>
301           Branch to checkout; if it refers to a branch (i.e., a name that,
302           when prepended with "refs/heads/", is a valid ref), then that
303           branch is checked out. Otherwise, if it refers to a valid commit,
304           your HEAD becomes "detached" and you are no longer on any branch
305           (see below for details).
306
307           You can use the @{-N} syntax to refer to the N-th last
308           branch/commit checked out using "git checkout" operation. You may
309           also specify - which is synonymous to @{-1}.
310
311           As a special case, you may use A...B as a shortcut for the merge
312           base of A and B if there is exactly one merge base. You can leave
313           out at most one of A and B, in which case it defaults to HEAD.
314
315       <new-branch>
316           Name for the new branch.
317
318       <start-point>
319           The name of a commit at which to start the new branch; see git-
320           branch(1) for details. Defaults to HEAD.
321
322           As a special case, you may use "A...B" as a shortcut for the merge
323           base of A and B if there is exactly one merge base. You can leave
324           out at most one of A and B, in which case it defaults to HEAD.
325
326       <tree-ish>
327           Tree to checkout from (when paths are given). If not specified, the
328           index will be used.
329
330           As a special case, you may use "A...B" as a shortcut for the merge
331           base of A and B if there is exactly one merge base. You can leave
332           out at most one of A and B, in which case it defaults to HEAD.
333
334       --
335           Do not interpret any more arguments as options.
336
337       <pathspec>...
338           Limits the paths affected by the operation.
339
340           For more details, see the pathspec entry in gitglossary(7).
341

DETACHED HEAD

343       HEAD normally refers to a named branch (e.g. master). Meanwhile, each
344       branch refers to a specific commit. Let’s look at a repo with three
345       commits, one of them tagged, and with branch master checked out:
346
347                      HEAD (refers to branch 'master')
348                       |
349                       v
350           a---b---c  branch 'master' (refers to commit 'c')
351               ^
352               |
353             tag 'v2.0' (refers to commit 'b')
354
355       When a commit is created in this state, the branch is updated to refer
356       to the new commit. Specifically, git commit creates a new commit d,
357       whose parent is commit c, and then updates branch master to refer to
358       new commit d. HEAD still refers to branch master and so indirectly now
359       refers to commit d:
360
361           $ edit; git add; git commit
362
363                          HEAD (refers to branch 'master')
364                           |
365                           v
366           a---b---c---d  branch 'master' (refers to commit 'd')
367               ^
368               |
369             tag 'v2.0' (refers to commit 'b')
370
371       It is sometimes useful to be able to checkout a commit that is not at
372       the tip of any named branch, or even to create a new commit that is not
373       referenced by a named branch. Let’s look at what happens when we
374       checkout commit b (here we show two ways this may be done):
375
376           $ git checkout v2.0  # or
377           $ git checkout master^^
378
379              HEAD (refers to commit 'b')
380               |
381               v
382           a---b---c---d  branch 'master' (refers to commit 'd')
383               ^
384               |
385             tag 'v2.0' (refers to commit 'b')
386
387       Notice that regardless of which checkout command we use, HEAD now
388       refers directly to commit b. This is known as being in detached HEAD
389       state. It means simply that HEAD refers to a specific commit, as
390       opposed to referring to a named branch. Let’s see what happens when we
391       create a commit:
392
393           $ edit; git add; git commit
394
395                HEAD (refers to commit 'e')
396                 |
397                 v
398                 e
399                /
400           a---b---c---d  branch 'master' (refers to commit 'd')
401               ^
402               |
403             tag 'v2.0' (refers to commit 'b')
404
405       There is now a new commit e, but it is referenced only by HEAD. We can
406       of course add yet another commit in this state:
407
408           $ edit; git add; git commit
409
410                    HEAD (refers to commit 'f')
411                     |
412                     v
413                 e---f
414                /
415           a---b---c---d  branch 'master' (refers to commit 'd')
416               ^
417               |
418             tag 'v2.0' (refers to commit 'b')
419
420       In fact, we can perform all the normal Git operations. But, let’s look
421       at what happens when we then checkout master:
422
423           $ git checkout master
424
425                          HEAD (refers to branch 'master')
426                 e---f     |
427                /          v
428           a---b---c---d  branch 'master' (refers to commit 'd')
429               ^
430               |
431             tag 'v2.0' (refers to commit 'b')
432
433       It is important to realize that at this point nothing refers to commit
434       f. Eventually commit f (and by extension commit e) will be deleted by
435       the routine Git garbage collection process, unless we create a
436       reference before that happens. If we have not yet moved away from
437       commit f, any of these will create a reference to it:
438
439           $ git checkout -b foo  # or "git switch -c foo"  (1)
440           $ git branch foo                                 (2)
441           $ git tag foo                                    (3)
442
443
444        1. creates a new branch foo, which refers to commit f, and
445           then updates HEAD to refer to branch foo. In other words,
446           we’ll no longer be in detached HEAD state after this
447           command.
448        2. similarly creates a new branch foo, which refers to commit
449           f, but leaves HEAD detached.
450        3. creates a new tag foo, which refers to commit f, leaving
451           HEAD detached.
452
453       If we have moved away from commit f, then we must first recover its
454       object name (typically by using git reflog), and then we can create a
455       reference to it. For example, to see the last two commits to which HEAD
456       referred, we can use either of these commands:
457
458           $ git reflog -2 HEAD # or
459           $ git log -g -2 HEAD
460

ARGUMENT DISAMBIGUATION

462       When there is only one argument given and it is not -- (e.g. git
463       checkout abc), and when the argument is both a valid <tree-ish> (e.g. a
464       branch abc exists) and a valid <pathspec> (e.g. a file or a directory
465       whose name is "abc" exists), Git would usually ask you to disambiguate.
466       Because checking out a branch is so common an operation, however, git
467       checkout abc takes "abc" as a <tree-ish> in such a situation. Use git
468       checkout -- <pathspec> if you want to checkout these paths out of the
469       index.
470

EXAMPLES

472   1. Paths
473       The following sequence checks out the master branch, reverts the
474       Makefile to two revisions back, deletes hello.c by mistake, and gets it
475       back from the index.
476
477           $ git checkout master             (1)
478           $ git checkout master~2 Makefile  (2)
479           $ rm -f hello.c
480           $ git checkout hello.c            (3)
481
482
483        1. switch branch
484        2. take a file out of another commit
485        3. restore hello.c from the index
486
487       If you want to check out all C source files out of the index, you can
488       say
489
490           $ git checkout -- '*.c'
491
492       Note the quotes around *.c. The file hello.c will also be checked out,
493       even though it is no longer in the working tree, because the file
494       globbing is used to match entries in the index (not in the working tree
495       by the shell).
496
497       If you have an unfortunate branch that is named hello.c, this step
498       would be confused as an instruction to switch to that branch. You
499       should instead write:
500
501           $ git checkout -- hello.c
502
503   2. Merge
504       After working in the wrong branch, switching to the correct branch
505       would be done using:
506
507           $ git checkout mytopic
508
509       However, your "wrong" branch and correct mytopic branch may differ in
510       files that you have modified locally, in which case the above checkout
511       would fail like this:
512
513           $ git checkout mytopic
514           error: You have local changes to 'frotz'; not switching branches.
515
516       You can give the -m flag to the command, which would try a three-way
517       merge:
518
519           $ git checkout -m mytopic
520           Auto-merging frotz
521
522       After this three-way merge, the local modifications are not registered
523       in your index file, so git diff would show you what changes you made
524       since the tip of the new branch.
525
526   3. Merge conflict
527       When a merge conflict happens during switching branches with the -m
528       option, you would see something like this:
529
530           $ git checkout -m mytopic
531           Auto-merging frotz
532           ERROR: Merge conflict in frotz
533           fatal: merge program failed
534
535       At this point, git diff shows the changes cleanly merged as in the
536       previous example, as well as the changes in the conflicted files. Edit
537       and resolve the conflict and mark it resolved with git add as usual:
538
539           $ edit frotz
540           $ git add frotz
541

CONFIGURATION

543       Everything below this line in this section is selectively included from
544       the git-config(1) documentation. The content is the same as what’s
545       found there:
546
547       checkout.defaultRemote
548           When you run git checkout <something> or git switch <something> and
549           only have one remote, it may implicitly fall back on checking out
550           and tracking e.g.  origin/<something>. This stops working as soon
551           as you have more than one remote with a <something> reference. This
552           setting allows for setting the name of a preferred remote that
553           should always win when it comes to disambiguation. The typical
554           use-case is to set this to origin.
555
556           Currently this is used by git-switch(1) and git-checkout(1) when
557           git checkout <something> or git switch <something> will checkout
558           the <something> branch on another remote, and by git-worktree(1)
559           when git worktree add refers to a remote branch. This setting might
560           be used for other checkout-like commands or functionality in the
561           future.
562
563       checkout.guess
564           Provides the default value for the --guess or --no-guess option in
565           git checkout and git switch. See git-switch(1) and git-checkout(1).
566
567       checkout.workers
568           The number of parallel workers to use when updating the working
569           tree. The default is one, i.e. sequential execution. If set to a
570           value less than one, Git will use as many workers as the number of
571           logical cores available. This setting and
572           checkout.thresholdForParallelism affect all commands that perform
573           checkout. E.g. checkout, clone, reset, sparse-checkout, etc.
574
575           Note: Parallel checkout usually delivers better performance for
576           repositories located on SSDs or over NFS. For repositories on
577           spinning disks and/or machines with a small number of cores, the
578           default sequential checkout often performs better. The size and
579           compression level of a repository might also influence how well the
580           parallel version performs.
581
582       checkout.thresholdForParallelism
583           When running parallel checkout with a small number of files, the
584           cost of subprocess spawning and inter-process communication might
585           outweigh the parallelization gains. This setting allows you to
586           define the minimum number of files for which parallel checkout
587           should be attempted. The default is 100.
588

SEE ALSO

590       git-switch(1), git-restore(1)
591

GIT

593       Part of the git(1) suite
594
595
596
597Git 2.43.0                        11/20/2023                   GIT-CHECKOUT(1)
Impressum