2 .\" Title: git-checkout
3 .\" Author: [FIXME: author] [see http://www.docbook.org/tdg5/en/html/author]
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
7 .\" Source: Git 2.42.0.rc0
10 .TH "GIT\-CHECKOUT" "1" "2023\-08\-04" "Git 2\&.42\&.0\&.rc0" "Git Manual"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 git-checkout \- Switch branches or restore working tree files
35 \fIgit checkout\fR [\-q] [\-f] [\-m] [<branch>]
36 \fIgit checkout\fR [\-q] [\-f] [\-m] \-\-detach [<branch>]
37 \fIgit checkout\fR [\-q] [\-f] [\-m] [\-\-detach] <commit>
38 \fIgit checkout\fR [\-q] [\-f] [\-m] [[\-b|\-B|\-\-orphan] <new\-branch>] [<start\-point>]
39 \fIgit checkout\fR [\-f|\-\-ours|\-\-theirs|\-m|\-\-conflict=<style>] [<tree\-ish>] [\-\-] <pathspec>\&...
40 \fIgit checkout\fR [\-f|\-\-ours|\-\-theirs|\-m|\-\-conflict=<style>] [<tree\-ish>] \-\-pathspec\-from\-file=<file> [\-\-pathspec\-file\-nul]
41 \fIgit checkout\fR (\-p|\-\-patch) [<tree\-ish>] [\-\-] [<pathspec>\&...]
46 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\&.
48 \fIgit checkout\fR [<branch>]
50 To prepare for working on
51 \fB<branch>\fR, switch to it by updating the index and the files in the working tree, and by pointing
53 at the branch\&. Local modifications to the files in the working tree are kept, so that they can be committed to the
58 is not found but there does exist a tracking branch in exactly one remote (call it
59 \fB<remote>\fR) with a matching name and
61 is not specified, treat as equivalent to
67 $ git checkout \-b <branch> \-\-track <remote>/<branch>
74 \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 exists, for the current branch\&.
77 \fIgit checkout\fR \-b|\-B <new\-branch> [<start\-point>]
81 causes a new branch to be created as if
83 were called and then checked out\&. In this case you can use the
87 options, which will be passed to
88 \fIgit branch\fR\&. As a convenience,
92 implies branch creation; see the description of
100 is created if it doesn\(cqt exist; otherwise, it is reset\&. This is the transactional equivalent of
106 $ git branch \-f <branch> [<start\-point>]
107 $ git checkout <branch>
113 that is to say, the branch is not reset/created unless "git checkout" is successful\&.
116 \fIgit checkout\fR \-\-detach [<branch>], \fIgit checkout\fR [\-\-detach] <commit>
118 Prepare to work on top of
119 \fB<commit>\fR, by detaching
121 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\&.
125 argument is a branch name, the
127 option can be used to detach
129 at the tip of the branch (\fBgit checkout <branch>\fR
130 would check out that branch without detaching
137 at the tip of the current branch\&.
140 \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]
142 Overwrite the contents of the files that match the pathspec\&. When the
144 (most often a commit) is not given, overwrite working tree with the contents in the index\&. When the
146 is given, overwrite both the index and the working tree with the contents at the
149 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
151 will ignore these unmerged entries\&. The contents from a specific side of the merge can be checked out of the index by using
154 \fB\-\-theirs\fR\&. With
155 \fB\-m\fR, changes made to the working tree file can be discarded to re\-create the original conflicted merge result\&.
158 \fIgit checkout\fR (\-p|\-\-patch) [<tree\-ish>] [\-\-] [<pathspec>\&...]
160 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
168 Quiet, suppress feedback messages\&.
171 \-\-progress, \-\-no\-progress
173 Progress status is reported on the standard error stream by default when it is attached to a terminal, unless
175 is specified\&. This flag enables progress reporting even if not attached to a terminal, regardless of
181 When switching branches, proceed even if the index or the working tree differs from
182 \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\&.
184 When checking out paths from the index, do not fail upon unmerged entries; instead, unmerged entries are ignored\&.
189 When checking out paths from the index, check out stage #2 (\fIours\fR) or #3 (\fItheirs\fR) for unmerged paths\&.
194 \fBgit pull \-\-rebase\fR,
200 gives the version from the branch the changes are rebased onto, while
202 gives the version from the branch that holds your work that is being rebased\&.
206 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
208 (i\&.e\&. "our shared canonical history"), while what you did on your side branch as
210 (i\&.e\&. "one contributor\(cqs work on top of it")\&.
215 Create a new branch named
216 \fB<new\-branch>\fR, start it at
217 \fB<start\-point>\fR, and check the resulting branch out; see
225 \fB<new\-branch>\fR, start it at
226 \fB<start\-point>\fR; if it already exists, then reset it to
227 \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
232 \-t, \-\-track[=(direct|inherit)]
234 When creating a new branch, set up "upstream" configuration\&. See "\-\-track" in
240 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
242 as the local branch when branching off of
245 \fBremotes/origin/hack\fR, or even
246 \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
253 Do not set up "upstream" configuration, even if the
254 \fBbranch\&.autoSetupMerge\fR
255 configuration variable is true\&.
258 \-\-guess, \-\-no\-guess
262 is not found but there does exist a tracking branch in exactly one remote (call it
263 \fB<remote>\fR) with a matching name, treat as equivalent to
269 $ git checkout \-b <branch> \-\-track <remote>/<branch>
275 If the branch exists in multiple remotes and one of them is named by the
276 \fBcheckout\&.defaultRemote\fR
277 configuration variable, we\(cqll use that one for the purposes of disambiguation, even if the
279 isn\(cqt unique across all remotes\&. Set it to e\&.g\&.
280 \fBcheckout\&.defaultRemote=origin\fR
281 to always checkout remote branches from there if
283 is ambiguous but exists on the
286 \fBcheckout\&.defaultRemote\fR
288 \fBgit-config\fR(1)\&.
291 is the default behavior\&. Use
295 The default behavior can be set via the
296 \fBcheckout\&.guess\fR
297 configuration variable\&.
302 Create the new branch\(cqs reflog; see
309 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
310 \fBgit checkout <commit>\fR
313 is not a branch name\&. See the "DETACHED HEAD" section below for details\&.
316 \-\-orphan <new\-branch>
321 \fB<new\-branch>\fR, started from
323 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\&.
325 The index and the working tree are adjusted as if you had previously run
326 \fBgit checkout <start\-point>\fR\&. This allows you to start a new history that records a set of paths similar to
330 to make the root commit\&.
332 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\&.
334 If you want to start a disconnected history that records a set of paths that is totally different from the one of
335 \fB<start\-point>\fR, then you should clear the index and the working tree right after creating the orphan branch by running
336 \fBgit rm \-rf \&.\fR
337 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\&.
340 \-\-ignore\-skip\-worktree\-bits
342 In sparse checkout mode,
343 \fBgit checkout \-\- <paths>\fR
344 would update only entries matched by
346 and sparse patterns in
347 \fB$GIT_DIR/info/sparse\-checkout\fR\&. This option ignores the sparse patterns and adds back any files in
353 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\&.
355 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
359 if the merge should result in deletion of the path)\&.
361 When checking out paths from the index, this option lets you recreate the conflicted merge in the specified paths\&.
363 When switching branches with
364 \fB\-\-merge\fR, staged changes may be lost\&.
371 option above, but changes the way the conflicting hunks are presented, overriding the
372 \fBmerge\&.conflictStyle\fR
373 configuration variable\&. Possible values are "merge" (default), "diff3", and "zdiff3"\&.
378 Interactively select hunks in the difference between the
380 (or the index, if unspecified) and the working tree\&. The chosen hunks are then applied in reverse to the working tree (and if a
382 was specified, the index)\&.
384 This means that you can use
385 \fBgit checkout \-p\fR
386 to selectively discard edits from your current working tree\&. See the \(lqInteractive Mode\(rq section of
388 to learn how to operate the
392 Note that this option uses the no overlay mode by default (see also
393 \fB\-\-overlay\fR), and currently doesn\(cqt support overlay mode\&.
396 \-\-ignore\-other\-worktrees
399 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\&.
402 \-\-overwrite\-ignore, \-\-no\-overwrite\-ignore
404 Silently overwrite ignored files when switching branches\&. This is the default behavior\&. Use
405 \fB\-\-no\-overwrite\-ignore\fR
406 to abort the operation when the new branch contains ignored files\&.
409 \-\-recurse\-submodules, \-\-no\-recurse\-submodules
412 \fB\-\-recurse\-submodules\fR
413 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
415 is used\&. If nothing (or
416 \fB\-\-no\-recurse\-submodules\fR) is used, submodules working trees will not be updated\&. Just like
417 \fBgit-submodule\fR(1), this will detach
422 \-\-overlay, \-\-no\-overlay
424 In the default overlay mode,
426 never removes files from the index or the working tree\&. When specifying
427 \fB\-\-no\-overlay\fR, files that appear in the index and working tree, but not in
429 are removed, to make them match
434 \-\-pathspec\-from\-file=<file>
436 Pathspec is passed in
438 instead of commandline args\&. If
442 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
443 \fBcore\&.quotePath\fR
445 \fBgit-config\fR(1))\&. See also
446 \fB\-\-pathspec\-file\-nul\fR
448 \fB\-\-literal\-pathspecs\fR\&.
451 \-\-pathspec\-file\-nul
454 \fB\-\-pathspec\-from\-file\fR\&. Pathspec elements are separated with NUL character and all other characters are taken literally (including newlines and quotes)\&.
459 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
461 becomes "detached" and you are no longer on any branch (see below for details)\&.
465 syntax to refer to the N\-th last branch/commit checked out using "git checkout" operation\&. You may also specify
467 which is synonymous to
470 As a special case, you may use
472 as a shortcut for the merge base of
476 if there is exactly one merge base\&. You can leave out at most one of
479 \fBB\fR, in which case it defaults to
485 Name for the new branch\&.
490 The name of a commit at which to start the new branch; see
492 for details\&. Defaults to
495 As a special case, you may use
497 as a shortcut for the merge base of
501 if there is exactly one merge base\&. You can leave out at most one of
504 \fBB\fR, in which case it defaults to
510 Tree to checkout from (when paths are given)\&. If not specified, the index will be used\&.
512 As a special case, you may use
514 as a shortcut for the merge base of
518 if there is exactly one merge base\&. You can leave out at most one of
521 \fBB\fR, in which case it defaults to
527 Do not interpret any more arguments as options\&.
532 Limits the paths affected by the operation\&.
534 For more details, see the
537 \fBgitglossary\fR(7)\&.
541 \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:
547 HEAD (refers to branch \*(Aqmaster\*(Aq)
550 a\-\-\-b\-\-\-c branch \*(Aqmaster\*(Aq (refers to commit \*(Aqc\*(Aq)
553 tag \*(Aqv2\&.0\*(Aq (refers to commit \*(Aqb\*(Aq)
560 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:
566 $ edit; git add; git commit
568 HEAD (refers to branch \*(Aqmaster\*(Aq)
571 a\-\-\-b\-\-\-c\-\-\-d branch \*(Aqmaster\*(Aq (refers to commit \*(Aqd\*(Aq)
574 tag \*(Aqv2\&.0\*(Aq (refers to commit \*(Aqb\*(Aq)
581 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):
587 $ git checkout v2\&.0 # or
588 $ git checkout master^^
590 HEAD (refers to commit \*(Aqb\*(Aq)
593 a\-\-\-b\-\-\-c\-\-\-d branch \*(Aqmaster\*(Aq (refers to commit \*(Aqd\*(Aq)
596 tag \*(Aqv2\&.0\*(Aq (refers to commit \*(Aqb\*(Aq)
603 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:
609 $ edit; git add; git commit
611 HEAD (refers to commit \*(Aqe\*(Aq)
616 a\-\-\-b\-\-\-c\-\-\-d branch \*(Aqmaster\*(Aq (refers to commit \*(Aqd\*(Aq)
619 tag \*(Aqv2\&.0\*(Aq (refers to commit \*(Aqb\*(Aq)
626 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:
632 $ edit; git add; git commit
634 HEAD (refers to commit \*(Aqf\*(Aq)
639 a\-\-\-b\-\-\-c\-\-\-d branch \*(Aqmaster\*(Aq (refers to commit \*(Aqd\*(Aq)
642 tag \*(Aqv2\&.0\*(Aq (refers to commit \*(Aqb\*(Aq)
649 In fact, we can perform all the normal Git operations\&. But, let\(cqs look at what happens when we then checkout \fBmaster\fR:
655 $ git checkout master
657 HEAD (refers to branch \*(Aqmaster\*(Aq)
660 a\-\-\-b\-\-\-c\-\-\-d branch \*(Aqmaster\*(Aq (refers to commit \*(Aqd\*(Aq)
663 tag \*(Aqv2\&.0\*(Aq (refers to commit \*(Aqb\*(Aq)
670 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:
676 $ git checkout \-b foo # or "git switch \-c foo" \fB(1)\fR
677 $ git branch foo \fB(2)\fR
678 $ git tag foo \fB(3)\fR
686 r lw(\n(.lu*75u/100u).
689 \fBfoo\fR, which refers to commit
690 \fBf\fR, and then updates
693 \fBfoo\fR\&. In other words, we\(cqll no longer be in detached
695 state after this command\&.
698 similarly creates a new branch
699 \fBfoo\fR, which refers to commit
706 \fBfoo\fR, which refers to commit
713 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:
719 $ git reflog \-2 HEAD # or
720 $ git log \-g \-2 HEAD
726 .SH "ARGUMENT DISAMBIGUATION"
728 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\&.
732 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\&.
738 $ git checkout master \fB(1)\fR
739 $ git checkout master~2 Makefile \fB(2)\fR
741 $ git checkout hello\&.c \fB(3)\fR
749 r lw(\n(.lu*75u/100u).
754 take a file out of another commit
763 If you want to check out \fIall\fR C source files out of the index, you can say
769 $ git checkout \-\- \*(Aq*\&.c\*(Aq
776 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)\&.
778 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:
784 $ git checkout \-\- hello\&.c
792 After working in the wrong branch, switching to the correct branch would be done using:
798 $ git checkout mytopic
805 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:
811 $ git checkout mytopic
812 error: You have local changes to \*(Aqfrotz\*(Aq; not switching branches\&.
819 You can give the \fB\-m\fR flag to the command, which would try a three\-way merge:
825 $ git checkout \-m mytopic
833 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\&.
834 .SS "3\&. Merge conflict"
836 When a merge conflict happens during switching branches with the \fB\-m\fR option, you would see something like this:
842 $ git checkout \-m mytopic
844 ERROR: Merge conflict in frotz
845 fatal: merge program failed
852 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:
867 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:
869 checkout\&.defaultRemote
872 \fBgit checkout <something>\fR
874 \fBgit switch <something>\fR
875 and only have one remote, it may implicitly fall back on checking out and tracking e\&.g\&.
876 \fBorigin/<something>\fR\&. This stops working as soon as you have more than one remote with a
878 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
881 Currently this is used by
884 \fBgit-checkout\fR(1)
886 \fBgit checkout <something>\fR
888 \fBgit switch <something>\fR
891 branch on another remote, and by
892 \fBgit-worktree\fR(1)
894 \fBgit worktree add\fR
895 refers to a remote branch\&. This setting might be used for other checkout\-like commands or functionality in the future\&.
900 Provides the default value for the
907 \fBgit switch\fR\&. See
910 \fBgit-checkout\fR(1)\&.
915 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
916 \fBcheckout\&.thresholdForParallelism\fR
917 affect all commands that perform checkout\&. E\&.g\&. checkout, clone, reset, sparse\-checkout, etc\&.
919 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\&.
922 checkout\&.thresholdForParallelism
924 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 to define the minimum number of files for which parallel checkout should be attempted\&. The default is 100\&.
928 \fBgit-switch\fR(1), \fBgit-restore\fR(1)
931 Part of the \fBgit\fR(1) suite