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.34.1.182.ge773545c7f
10 .TH "GIT\-CHECKOUT" "1" "12/10/2021" "Git 2\&.34\&.1\&.182\&.ge77354" "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
218 \fB<start\-point>\fR; see
228 \fB<start\-point>\fR; if it already exists, then reset it to
229 \fB<start\-point>\fR\&. This is equivalent to running "git branch" with "\-f"; see
236 When creating a new branch, set up "upstream" configuration\&. See "\-\-track" in
242 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
244 as the local branch when branching off of
247 \fBremotes/origin/hack\fR, or even
248 \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
255 Do not set up "upstream" configuration, even if the
256 \fBbranch\&.autoSetupMerge\fR
257 configuration variable is true\&.
260 \-\-guess, \-\-no\-guess
264 is not found but there does exist a tracking branch in exactly one remote (call it
265 \fB<remote>\fR) with a matching name, treat as equivalent to
271 $ git checkout \-b <branch> \-\-track <remote>/<branch>
277 If the branch exists in multiple remotes and one of them is named by the
278 \fBcheckout\&.defaultRemote\fR
279 configuration variable, we\(cqll use that one for the purposes of disambiguation, even if the
281 isn\(cqt unique across all remotes\&. Set it to e\&.g\&.
282 \fBcheckout\&.defaultRemote=origin\fR
283 to always checkout remote branches from there if
285 is ambiguous but exists on the
288 \fBcheckout\&.defaultRemote\fR
290 \fBgit-config\fR(1)\&.
293 is the default behavior\&. Use
297 The default behavior can be set via the
298 \fBcheckout\&.guess\fR
299 configuration variable\&.
304 Create the new branch\(cqs reflog; see
311 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
312 \fBgit checkout <commit>\fR
315 is not a branch name\&. See the "DETACHED HEAD" section below for details\&.
318 \-\-orphan <new\-branch>
323 \fB<new\-branch>\fR, started from
325 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\&.
327 The index and the working tree are adjusted as if you had previously run
328 \fBgit checkout <start\-point>\fR\&. This allows you to start a new history that records a set of paths similar to
332 to make the root commit\&.
334 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\&.
336 If you want to start a disconnected history that records a set of paths that is totally different from the one of
337 \fB<start\-point>\fR, then you should clear the index and the working tree right after creating the orphan branch by running
338 \fBgit rm \-rf \&.\fR
339 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\&.
342 \-\-ignore\-skip\-worktree\-bits
344 In sparse checkout mode,
345 \fBgit checkout \-\- <paths>\fR
346 would update only entries matched by
348 and sparse patterns in
349 \fB$GIT_DIR/info/sparse\-checkout\fR\&. This option ignores the sparse patterns and adds back any files in
355 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\&.
357 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
361 if the merge should result in deletion of the path)\&.
363 When checking out paths from the index, this option lets you recreate the conflicted merge in the specified paths\&.
365 When switching branches with
366 \fB\-\-merge\fR, staged changes may be lost\&.
373 option above, but changes the way the conflicting hunks are presented, overriding the
374 \fBmerge\&.conflictStyle\fR
375 configuration variable\&. Possible values are "merge" (default) and "diff3" (in addition to what is shown by "merge" style, shows the original contents)\&.
380 Interactively select hunks in the difference between the
382 (or the index, if unspecified) and the working tree\&. The chosen hunks are then applied in reverse to the working tree (and if a
384 was specified, the index)\&.
386 This means that you can use
387 \fBgit checkout \-p\fR
388 to selectively discard edits from your current working tree\&. See the \(lqInteractive Mode\(rq section of
390 to learn how to operate the
394 Note that this option uses the no overlay mode by default (see also
395 \fB\-\-overlay\fR), and currently doesn\(cqt support overlay mode\&.
398 \-\-ignore\-other\-worktrees
401 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\&.
404 \-\-overwrite\-ignore, \-\-no\-overwrite\-ignore
406 Silently overwrite ignored files when switching branches\&. This is the default behavior\&. Use
407 \fB\-\-no\-overwrite\-ignore\fR
408 to abort the operation when the new branch contains ignored files\&.
411 \-\-recurse\-submodules, \-\-no\-recurse\-submodules
414 \fB\-\-recurse\-submodules\fR
415 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
417 is used\&. If nothing (or
418 \fB\-\-no\-recurse\-submodules\fR) is used, submodules working trees will not be updated\&. Just like
419 \fBgit-submodule\fR(1), this will detach
424 \-\-overlay, \-\-no\-overlay
426 In the default overlay mode,
428 never removes files from the index or the working tree\&. When specifying
429 \fB\-\-no\-overlay\fR, files that appear in the index and working tree, but not in
431 are removed, to make them match
436 \-\-pathspec\-from\-file=<file>
438 Pathspec is passed in
440 instead of commandline args\&. If
444 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
445 \fBcore\&.quotePath\fR
447 \fBgit-config\fR(1))\&. See also
448 \fB\-\-pathspec\-file\-nul\fR
450 \fB\-\-literal\-pathspecs\fR\&.
453 \-\-pathspec\-file\-nul
456 \fB\-\-pathspec\-from\-file\fR\&. Pathspec elements are separated with NUL character and all other characters are taken literally (including newlines and quotes)\&.
461 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
463 becomes "detached" and you are no longer on any branch (see below for details)\&.
467 syntax to refer to the N\-th last branch/commit checked out using "git checkout" operation\&. You may also specify
469 which is synonymous to
472 As a special case, you may use
474 as a shortcut for the merge base of
478 if there is exactly one merge base\&. You can leave out at most one of
481 \fBB\fR, in which case it defaults to
487 Name for the new branch\&.
492 The name of a commit at which to start the new branch; see
494 for details\&. Defaults to
497 As a special case, you may use
499 as a shortcut for the merge base of
503 if there is exactly one merge base\&. You can leave out at most one of
506 \fBB\fR, in which case it defaults to
512 Tree to checkout from (when paths are given)\&. If not specified, the index will be used\&.
514 As a special case, you may use
516 as a shortcut for the merge base of
520 if there is exactly one merge base\&. You can leave out at most one of
523 \fBB\fR, in which case it defaults to
529 Do not interpret any more arguments as options\&.
534 Limits the paths affected by the operation\&.
536 For more details, see the
539 \fBgitglossary\fR(7)\&.
543 \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:
549 HEAD (refers to branch \(aqmaster\(aq)
552 a\-\-\-b\-\-\-c branch \(aqmaster\(aq (refers to commit \(aqc\(aq)
555 tag \(aqv2\&.0\(aq (refers to commit \(aqb\(aq)
562 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:
568 $ edit; git add; git commit
570 HEAD (refers to branch \(aqmaster\(aq)
573 a\-\-\-b\-\-\-c\-\-\-d branch \(aqmaster\(aq (refers to commit \(aqd\(aq)
576 tag \(aqv2\&.0\(aq (refers to commit \(aqb\(aq)
583 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):
589 $ git checkout v2\&.0 # or
590 $ git checkout master^^
592 HEAD (refers to commit \(aqb\(aq)
595 a\-\-\-b\-\-\-c\-\-\-d branch \(aqmaster\(aq (refers to commit \(aqd\(aq)
598 tag \(aqv2\&.0\(aq (refers to commit \(aqb\(aq)
605 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:
611 $ edit; git add; git commit
613 HEAD (refers to commit \(aqe\(aq)
618 a\-\-\-b\-\-\-c\-\-\-d branch \(aqmaster\(aq (refers to commit \(aqd\(aq)
621 tag \(aqv2\&.0\(aq (refers to commit \(aqb\(aq)
628 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:
634 $ edit; git add; git commit
636 HEAD (refers to commit \(aqf\(aq)
641 a\-\-\-b\-\-\-c\-\-\-d branch \(aqmaster\(aq (refers to commit \(aqd\(aq)
644 tag \(aqv2\&.0\(aq (refers to commit \(aqb\(aq)
651 In fact, we can perform all the normal Git operations\&. But, let\(cqs look at what happens when we then checkout \fBmaster\fR:
657 $ git checkout master
659 HEAD (refers to branch \(aqmaster\(aq)
662 a\-\-\-b\-\-\-c\-\-\-d branch \(aqmaster\(aq (refers to commit \(aqd\(aq)
665 tag \(aqv2\&.0\(aq (refers to commit \(aqb\(aq)
672 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:
678 $ git checkout \-b foo \fB(1)\fR
679 $ git branch foo \fB(2)\fR
680 $ git tag foo \fB(3)\fR
687 \fB1. \fRcreates a new branch
688 \fBfoo\fR, which refers to commit
689 \fBf\fR, and then updates
692 \fBfoo\fR\&. In other words, we\(cqll no longer be in detached
694 state after this command\&.
696 \fB2. \fRsimilarly creates a new branch
697 \fBfoo\fR, which refers to commit
702 \fB3. \fRcreates a new tag
703 \fBfoo\fR, which refers to commit
709 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:
715 $ git reflog \-2 HEAD # or
716 $ git log \-g \-2 HEAD
722 .SH "ARGUMENT DISAMBIGUATION"
724 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\&.
735 The following sequence checks out the
739 to two revisions back, deletes
741 by mistake, and gets it back from the index\&.
747 $ git checkout master \fB(1)\fR
748 $ git checkout master~2 Makefile \fB(2)\fR
750 $ git checkout hello\&.c \fB(3)\fR
756 \fB1. \fRswitch branch
758 \fB2. \fRtake a file out of another commit
764 If you want to check out
766 C source files out of the index, you can say
772 $ git checkout \-\- \(aq*\&.c\(aq
778 Note the quotes around
779 \fB*\&.c\fR\&. The file
781 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)\&.
783 If you have an unfortunate branch that is named
784 \fBhello\&.c\fR, this step would be confused as an instruction to switch to that branch\&. You should instead write:
790 $ git checkout \-\- hello\&.c
807 After working in the wrong branch, switching to the correct branch would be done using:
813 $ git checkout mytopic
819 However, your "wrong" branch and correct
821 branch may differ in files that you have modified locally, in which case the above checkout would fail like this:
827 $ git checkout mytopic
828 error: You have local changes to \(aqfrotz\(aq; not switching branches\&.
836 flag to the command, which would try a three\-way merge:
842 $ git checkout \-m mytopic
849 After this three\-way merge, the local modifications are
851 registered in your index file, so
853 would show you what changes you made since the tip of the new branch\&.
864 When a merge conflict happens during switching branches with the
866 option, you would see something like this:
872 $ git checkout \-m mytopic
874 ERROR: Merge conflict in frotz
875 fatal: merge program failed
883 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
901 \fBgit-switch\fR(1), \fBgit-restore\fR(1)
904 Part of the \fBgit\fR(1) suite