2 .\" Title: git-checkout
3 .\" Author: [FIXME: author] [see http://www.docbook.org/tdg5/en/html/author]
4 .\" Generator: DocBook XSL Stylesheets v1.79.2 <http://docbook.sf.net/>
7 .\" Source: Git 2.47.0.rc0
10 .TH "GIT\-CHECKOUT" "1" "2024-09-25" "Git 2\&.47\&.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] <tree\-ish> [\-\-] <pathspec>\&...\:
40 \fIgit checkout\fR [\-f] <tree\-ish> \-\-pathspec\-from\-file=<file> [\-\-pathspec\-file\-nul]
41 \fIgit checkout\fR [\-f|\-\-ours|\-\-theirs|\-m|\-\-conflict=<style>] [\-\-] <pathspec>\&...\:
42 \fIgit checkout\fR [\-f|\-\-ours|\-\-theirs|\-m|\-\-conflict=<style>] \-\-pathspec\-from\-file=<file> [\-\-pathspec\-file\-nul]
43 \fIgit checkout\fR (\-p|\-\-patch) [<tree\-ish>] [\-\-] [<pathspec>\&...\:]
47 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\&.
49 \fIgit checkout\fR [<branch>]
51 To prepare for working on
52 \fB<branch>\fR, switch to it by updating the index and the files in the working tree, and by pointing
54 at the branch\&. Local modifications to the files in the working tree are kept, so that they can be committed to the
59 is not found but there does exist a tracking branch in exactly one remote (call it
60 \fB<remote>\fR) with a matching name and
62 is not specified, treat as equivalent to
68 $ git checkout \-b <branch> \-\-track <remote>/<branch>
75 \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\&.
78 \fIgit checkout\fR \-b|\-B <new\-branch> [<start\-point>]
82 causes a new branch to be created as if
84 were called and then checked out\&. In this case you can use the
88 options, which will be passed to
89 \fIgit branch\fR\&. As a convenience,
93 implies branch creation; see the description of
101 is created if it doesn\(cqt exist; otherwise, it is reset\&. This is the transactional equivalent of
107 $ git branch \-f <branch> [<start\-point>]
108 $ git checkout <branch>
114 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)\&.
117 \fIgit checkout\fR \-\-detach [<branch>], \fIgit checkout\fR [\-\-detach] <commit>
119 Prepare to work on top of
120 \fB<commit>\fR, by detaching
122 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\&.
126 argument is a branch name, the
128 option can be used to detach
130 at the tip of the branch (\fBgit checkout <branch>\fR
131 would check out that branch without detaching
138 at the tip of the current branch\&.
141 \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]
143 Overwrite the contents of the files that match the pathspec\&. When the
145 (most often a commit) is not given, overwrite working tree with the contents in the index\&. When the
147 is given, overwrite both the index and the working tree with the contents at the
150 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
152 will ignore these unmerged entries\&. The contents from a specific side of the merge can be checked out of the index by using
155 \fB\-\-theirs\fR\&. With
156 \fB\-m\fR, changes made to the working tree file can be discarded to re\-create the original conflicted merge result\&.
159 \fIgit checkout\fR (\-p|\-\-patch) [<tree\-ish>] [\-\-] [<pathspec>\&...\:]
161 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
169 Quiet, suppress feedback messages\&.
172 \-\-progress, \-\-no\-progress
174 Progress status is reported on the standard error stream by default when it is attached to a terminal, unless
176 is specified\&. This flag enables progress reporting even if not attached to a terminal, regardless of
182 When switching branches, proceed even if the index or the working tree differs from
183 \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\&.
185 When checking out paths from the index, do not fail upon unmerged entries; instead, unmerged entries are ignored\&.
190 When checking out paths from the index, check out stage #2 (\fIours\fR) or #3 (\fItheirs\fR) for unmerged paths\&.
195 \fBgit pull \-\-rebase\fR,
201 gives the version from the branch the changes are rebased onto, while
203 gives the version from the branch that holds your work that is being rebased\&.
207 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
209 (i\&.e\&. "our shared canonical history"), while what you did on your side branch as
211 (i\&.e\&. "one contributor\(cqs work on top of it")\&.
216 Create a new branch named
217 \fB<new\-branch>\fR, start it at
218 \fB<start\-point>\fR, and check the resulting branch out; see
226 \fB<new\-branch>\fR, start it at
227 \fB<start\-point>\fR; if it already exists, then reset it to
228 \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
233 \-t, \-\-track[=(direct|inherit)]
235 When creating a new branch, set up "upstream" configuration\&. See "\-\-track" in
241 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
243 as the local branch when branching off of
246 \fBremotes/origin/hack\fR, or even
247 \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
254 Do not set up "upstream" configuration, even if the
255 \fBbranch\&.autoSetupMerge\fR
256 configuration variable is true\&.
259 \-\-guess, \-\-no\-guess
263 is not found but there does exist a tracking branch in exactly one remote (call it
264 \fB<remote>\fR) with a matching name, treat as equivalent to
270 $ git checkout \-b <branch> \-\-track <remote>/<branch>
276 If the branch exists in multiple remotes and one of them is named by the
277 \fBcheckout\&.defaultRemote\fR
278 configuration variable, we\(cqll use that one for the purposes of disambiguation, even if the
280 isn\(cqt unique across all remotes\&. Set it to e\&.g\&.
281 \fBcheckout\&.defaultRemote=origin\fR
282 to always checkout remote branches from there if
284 is ambiguous but exists on the
287 \fBcheckout\&.defaultRemote\fR
289 \fBgit-config\fR(1)\&.
292 is the default behavior\&. Use
296 The default behavior can be set via the
297 \fBcheckout\&.guess\fR
298 configuration variable\&.
303 Create the new branch\(cqs reflog; see
310 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
311 \fBgit checkout <commit>\fR
314 is not a branch name\&. See the "DETACHED HEAD" section below for details\&.
317 \-\-orphan <new\-branch>
319 Create a new unborn branch, named
320 \fB<new\-branch>\fR, started from
322 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\&.
324 The index and the working tree are adjusted as if you had previously run
325 \fBgit checkout <start\-point>\fR\&. This allows you to start a new history that records a set of paths similar to
329 to make the root commit\&.
331 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\&.
333 If you want to start a disconnected history that records a set of paths that is totally different from the one of
334 \fB<start\-point>\fR, then you should clear the index and the working tree right after creating the orphan branch by running
335 \fBgit rm \-rf \&.\fR
336 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\&.
339 \-\-ignore\-skip\-worktree\-bits
341 In sparse checkout mode,
342 \fBgit checkout \-\- <paths>\fR
343 would update only entries matched by
345 and sparse patterns in
346 \fB$GIT_DIR/info/sparse\-checkout\fR\&. This option ignores the sparse patterns and adds back any files in
352 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\&.
354 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
358 if the merge should result in deletion of the path)\&.
360 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\&.
362 When switching branches with
363 \fB\-\-merge\fR, staged changes may be lost\&.
370 option above, but changes the way the conflicting hunks are presented, overriding the
371 \fBmerge\&.conflictStyle\fR
372 configuration variable\&. Possible values are "merge" (default), "diff3", and "zdiff3"\&.
377 Interactively select hunks in the difference between the
379 (or the index, if unspecified) and the working tree\&. The chosen hunks are then applied in reverse to the working tree (and if a
381 was specified, the index)\&.
383 This means that you can use
384 \fBgit checkout \-p\fR
385 to selectively discard edits from your current working tree\&. See the
386 \(lqInteractive Mode\(rq
389 to learn how to operate the
393 Note that this option uses the no overlay mode by default (see also
394 \fB\-\-overlay\fR), and currently doesn\(cqt support overlay mode\&.
397 \-\-ignore\-other\-worktrees
400 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\&.
403 \-\-overwrite\-ignore, \-\-no\-overwrite\-ignore
405 Silently overwrite ignored files when switching branches\&. This is the default behavior\&. Use
406 \fB\-\-no\-overwrite\-ignore\fR
407 to abort the operation when the new branch contains ignored files\&.
410 \-\-recurse\-submodules, \-\-no\-recurse\-submodules
413 \fB\-\-recurse\-submodules\fR
414 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
416 is used\&. If nothing (or
417 \fB\-\-no\-recurse\-submodules\fR) is used, submodules working trees will not be updated\&. Just like
418 \fBgit-submodule\fR(1), this will detach
423 \-\-overlay, \-\-no\-overlay
425 In the default overlay mode,
427 never removes files from the index or the working tree\&. When specifying
428 \fB\-\-no\-overlay\fR, files that appear in the index and working tree, but not in
430 are removed, to make them match
435 \-\-pathspec\-from\-file=<file>
437 Pathspec is passed in
439 instead of commandline args\&. If
443 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
444 \fBcore\&.quotePath\fR
446 \fBgit-config\fR(1))\&. See also
447 \fB\-\-pathspec\-file\-nul\fR
449 \fB\-\-literal\-pathspecs\fR\&.
452 \-\-pathspec\-file\-nul
455 \fB\-\-pathspec\-from\-file\fR\&. Pathspec elements are separated with NUL character and all other characters are taken literally (including newlines and quotes)\&.
460 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
462 becomes "detached" and you are no longer on any branch (see below for details)\&.
466 syntax to refer to the N\-th last branch/commit checked out using "git checkout" operation\&. You may also specify
468 which is synonymous to
471 As a special case, you may use
473 as a shortcut for the merge base of
477 if there is exactly one merge base\&. You can leave out at most one of
480 \fBB\fR, in which case it defaults to
486 Name for the new branch\&.
491 The name of a commit at which to start the new branch; see
493 for details\&. Defaults to
496 As a special case, you may use
498 as a shortcut for the merge base of
502 if there is exactly one merge base\&. You can leave out at most one of
505 \fBB\fR, in which case it defaults to
511 Tree to checkout from (when paths are given)\&. If not specified, the index will be used\&.
513 As a special case, you may use
515 as a shortcut for the merge base of
519 if there is exactly one merge base\&. You can leave out at most one of
522 \fBB\fR, in which case it defaults to
528 Do not interpret any more arguments as options\&.
533 Limits the paths affected by the operation\&.
535 For more details, see the
538 \fBgitglossary\fR(7)\&.
542 \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:
548 HEAD (refers to branch \*(Aqmaster\*(Aq)
551 a\-\-\-b\-\-\-c branch \*(Aqmaster\*(Aq (refers to commit \*(Aqc\*(Aq)
554 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)
580 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):
586 $ git checkout v2\&.0 # or
587 $ git checkout master^^
589 HEAD (refers to commit \*(Aqb\*(Aq)
592 a\-\-\-b\-\-\-c\-\-\-d branch \*(Aqmaster\*(Aq (refers to commit \*(Aqd\*(Aq)
595 tag \*(Aqv2\&.0\*(Aq (refers to commit \*(Aqb\*(Aq)
601 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:
607 $ edit; git add; git commit
609 HEAD (refers to commit \*(Aqe\*(Aq)
614 a\-\-\-b\-\-\-c\-\-\-d branch \*(Aqmaster\*(Aq (refers to commit \*(Aqd\*(Aq)
617 tag \*(Aqv2\&.0\*(Aq (refers to commit \*(Aqb\*(Aq)
623 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:
629 $ edit; git add; git commit
631 HEAD (refers to commit \*(Aqf\*(Aq)
636 a\-\-\-b\-\-\-c\-\-\-d branch \*(Aqmaster\*(Aq (refers to commit \*(Aqd\*(Aq)
639 tag \*(Aqv2\&.0\*(Aq (refers to commit \*(Aqb\*(Aq)
645 In fact, we can perform all the normal Git operations\&. But, let\(cqs look at what happens when we then checkout \fBmaster\fR:
651 $ git checkout master
653 HEAD (refers to branch \*(Aqmaster\*(Aq)
656 a\-\-\-b\-\-\-c\-\-\-d branch \*(Aqmaster\*(Aq (refers to commit \*(Aqd\*(Aq)
659 tag \*(Aqv2\&.0\*(Aq (refers to commit \*(Aqb\*(Aq)
665 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:
671 $ git checkout \-b foo # or "git switch \-c foo" \fB(1)\fR
672 $ git branch foo \fB(2)\fR
673 $ git tag foo \fB(3)\fR
681 r lw(\n(.lu*75u/100u).
684 \fBfoo\fR, which refers to commit
685 \fBf\fR, and then updates
688 \fBfoo\fR\&. In other words, we\(cqll no longer be in detached
690 state after this command\&.
693 similarly creates a new branch
694 \fBfoo\fR, which refers to commit
701 \fBfoo\fR, which refers to commit
708 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:
714 $ git reflog \-2 HEAD # or
715 $ git log \-g \-2 HEAD
720 .SH "ARGUMENT DISAMBIGUATION"
722 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\&.
726 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\&.
732 $ git checkout master \fB(1)\fR
733 $ git checkout master~2 Makefile \fB(2)\fR
735 $ git checkout hello\&.c \fB(3)\fR
743 r lw(\n(.lu*75u/100u).
748 take a file out of another commit
757 If you want to check out \fIall\fR C source files out of the index, you can say
763 $ git checkout \-\- \*(Aq*\&.c\*(Aq
769 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)\&.
771 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:
777 $ git checkout \-\- hello\&.c
784 After working in the wrong branch, switching to the correct branch would be done using:
790 $ git checkout mytopic
796 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:
802 $ git checkout mytopic
803 error: You have local changes to \*(Aqfrotz\*(Aq; not switching branches\&.
809 You can give the \fB\-m\fR flag to the command, which would try a three\-way merge:
815 $ git checkout \-m mytopic
822 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\&.
823 .SS "3\&. Merge conflict"
825 When a merge conflict happens during switching branches with the \fB\-m\fR option, you would see something like this:
831 $ git checkout \-m mytopic
833 ERROR: Merge conflict in frotz
834 fatal: merge program failed
840 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:
854 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:
856 checkout\&.defaultRemote
859 \fBgit checkout <something>\fR
861 \fBgit switch <something>\fR
862 and only have one remote, it may implicitly fall back on checking out and tracking e\&.g\&.
863 \fBorigin/<something>\fR\&. This stops working as soon as you have more than one remote with a
865 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
868 Currently this is used by
871 \fBgit-checkout\fR(1)
873 \fBgit checkout <something>\fR
875 \fBgit switch <something>\fR
878 branch on another remote, and by
879 \fBgit-worktree\fR(1)
881 \fBgit worktree add\fR
882 refers to a remote branch\&. This setting might be used for other checkout\-like commands or functionality in the future\&.
887 Provides the default value for the
894 \fBgit switch\fR\&. See
897 \fBgit-checkout\fR(1)\&.
902 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
903 \fBcheckout\&.thresholdForParallelism\fR
904 affect all commands that perform checkout\&. E\&.g\&. checkout, clone, reset, sparse\-checkout, etc\&.
906 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\&.
909 checkout\&.thresholdForParallelism
911 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\&.
915 \fBgit-switch\fR(1), \fBgit-restore\fR(1)
918 Part of the \fBgit\fR(1) suite