'\" t
.\"     Title: git-checkout
.\"    Author: [FIXME: author] [see http://www.docbook.org/tdg5/en/html/author]
.\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
.\"      Date: 06/29/2026
.\"    Manual: Git Manual
.\"    Source: Git 2.55.0
.\"  Language: English
.\"
.TH "GIT\-CHECKOUT" "1" "06/29/2026" "Git 2\&.55\&.0" "Git Manual"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
git-checkout \- Switch branches or restore working tree files
.SH "SYNOPSIS"
.sp
.nf
\fBgit\fR \fBcheckout\fR [\fB\-q\fR] [\fB\-f\fR] [\fB\-m\fR] [\fI<branch>\fR]
\fBgit\fR \fBcheckout\fR [\fB\-q\fR] [\fB\-f\fR] [\fB\-m\fR] \fB\-\-detach\fR [\fI<branch>\fR]
\fBgit\fR \fBcheckout\fR [\fB\-q\fR] [\fB\-f\fR] [\fB\-m\fR] [\fB\-\-detach\fR] \fI<commit>\fR
\fBgit\fR \fBcheckout\fR [\fB\-q\fR] [\fB\-f\fR] [\fB\-m\fR] [[\fB\-b\fR|\fB\-B\fR|\fB\-\-orphan\fR] \fI<new\-branch>\fR] [\fI<start\-point>\fR]
\fBgit\fR \fBcheckout\fR \fI<tree\-ish>\fR [\fB\-\-\fR] \fI<pathspec>\fR\&...\:
\fBgit\fR \fBcheckout\fR \fI<tree\-ish>\fR \fB\-\-pathspec\-from\-file=\fR\fI<file>\fR [\fB\-\-pathspec\-file\-nul\fR]
\fBgit\fR \fBcheckout\fR [\fB\-f\fR|\fB\-\-ours\fR|\fB\-\-theirs\fR|\fB\-m\fR|\fB\-\-conflict=\fR\fI<style>\fR] [\fB\-\-\fR] \fI<pathspec>\fR\&...\:
\fBgit\fR \fBcheckout\fR [\fB\-f\fR|\fB\-\-ours\fR|\fB\-\-theirs\fR|\fB\-m\fR|\fB\-\-conflict=\fR\fI<style>\fR] \fB\-\-pathspec\-from\-file=\fR\fI<file>\fR [\fB\-\-pathspec\-file\-nul\fR]
\fBgit\fR \fBcheckout\fR (\fB\-p\fR|\fB\-\-patch\fR) [\fI<tree\-ish>\fR] [\fB\-\-\fR] [\fI<pathspec>\fR\&...\:]
.fi
.SH "DESCRIPTION"
.sp
\fBgit\fR \fBcheckout\fR has two main modes:
.sp
.RS 4
.ie n \{\
\h'-04' 1.\h'+01'\c
.\}
.el \{\
.sp -1
.IP "  1." 4.2
.\}
\fBSwitch branches\fR, with
\fBgit\fR
\fBcheckout\fR
\fI<branch>\fR
.RE
.sp
.RS 4
.ie n \{\
\h'-04' 2.\h'+01'\c
.\}
.el \{\
.sp -1
.IP "  2." 4.2
.\}
\fBRestore a different version of a file\fR, for example with
\fBgit\fR
\fBcheckout\fR
\fI<commit>\fR
\fI<filename>\fR
or
\fBgit\fR
\fBcheckout\fR
\fI<filename>\fR
.RE
.sp
See ARGUMENT DISAMBIGUATION below for how Git decides which one to do\&.
.PP
\fBgit\fR \fBcheckout\fR [\fI<branch>\fR]
.RS 4
Switch to
\fI<branch>\fR\&. This sets the current branch to
\fI<branch>\fR
and updates the files in your working directory\&. The checkout will fail if there are uncommitted changes to any files where
\fI<branch>\fR
and your current commit have different content\&. Uncommitted changes will otherwise be kept\&.
.sp
If
\fI<branch>\fR
is not found but there does exist a tracking branch in exactly one remote (call it
\fI<remote>\fR) with a matching name and
\fB\-\-no\-guess\fR
is not specified, treat as equivalent to
.sp
.if n \{\
.RS 4
.\}
.nf
$ git checkout \-b <branch> \-\-track <remote>/<branch>
.fi
.if n \{\
.RE
.\}
.sp
Running
\fBgit\fR
\fBcheckout\fR
without specifying a branch has no effect except to print out the tracking information for the current branch\&.
.RE
.PP
\fBgit\fR \fBcheckout\fR \fB\-b\fR \fI<new\-branch>\fR [\fI<start\-point>\fR]
.RS 4
Create a new branch named
\fI<new\-branch>\fR, start it at
\fI<start\-point>\fR
(defaults to the current commit), and check out the new branch\&. You can use the
\fB\-\-track\fR
or
\fB\-\-no\-track\fR
options to set the branch\(cqs upstream tracking information\&.
.sp
This will fail if there\(cqs an error checking out
\fI<new\-branch>\fR, for example if checking out the
\fI<start\-point>\fR
commit would overwrite your uncommitted changes\&.
.RE
.PP
\fBgit\fR \fBcheckout\fR \fB\-B\fR \fI<branch>\fR [\fI<start\-point>\fR]
.RS 4
The same as
\fB\-b\fR, except that if the branch already exists it resets
\fI<branch>\fR
to the start point instead of failing\&.
.RE
.PP
\fBgit\fR \fBcheckout\fR \fB\-\-detach\fR [\fI<branch>\fR], \fBgit\fR \fBcheckout\fR [\fB\-\-detach\fR] \fI<commit>\fR
.RS 4
The same as
\fBgit\fR
\fBcheckout\fR
\fI<branch>\fR, except that instead of pointing
\fBHEAD\fR
at the branch, it points
\fBHEAD\fR
at the commit ID\&. See the "DETACHED HEAD" section below for more\&.
.sp
Omitting
\fI<branch>\fR
detaches
\fBHEAD\fR
at the tip of the current branch\&.
.RE
.PP
\fBgit\fR \fBcheckout\fR \fI<tree\-ish>\fR [\fB\-\-\fR] \fI<pathspec>\fR\&.\&.\&., \fBgit\fR \fBcheckout\fR \fI<tree\-ish>\fR \fB\-\-pathspec\-from\-file=\fR\fI<file>\fR [\fB\-\-pathspec\-file\-nul\fR]
.RS 4
Replace the specified files and/or directories with the version from the given commit or tree and add them to the index (also known as "staging area")\&.
.sp
For example,
\fBgit\fR
\fBcheckout\fR
\fBmain\fR
\fBfile\&.txt\fR
will replace
\fBfile\&.txt\fR
with the version from
\fBmain\fR\&.
.RE
.PP
\fBgit\fR \fBcheckout\fR [\fB\-f\fR|\fB\-\-ours\fR|\fB\-\-theirs\fR|\fB\-m\fR|\fB\-\-conflict=\fR\fI<style>\fR] [\fB\-\-\fR] \fI<pathspec>\fR\&.\&.\&., \fBgit\fR \fBcheckout\fR [\fB\-f\fR|\fB\-\-ours\fR|\fB\-\-theirs\fR|\fB\-m\fR|\fB\-\-conflict=\fR\fI<style>\fR] \fB\-\-pathspec\-from\-file=\fR\fI<file>\fR [\fB\-\-pathspec\-file\-nul\fR]
.RS 4
Replace the specified files and/or directories with the version from the index\&.
.sp
For example, if you check out a commit, edit
\fBfile\&.txt\fR, and then decide those changes were a mistake,
\fBgit\fR
\fBcheckout\fR
\fBfile\&.txt\fR
will discard any unstaged changes to
\fBfile\&.txt\fR\&.
.sp
This will fail if the file has a merge conflict and you haven\(cqt yet run
\fBgit\fR
\fBadd\fR
\fBfile\&.txt\fR
(or something equivalent) to mark it as resolved\&. You can use
\fB\-f\fR
to ignore the unmerged files instead of failing, use
\fB\-\-ours\fR
or
\fB\-\-theirs\fR
to replace them with the version from a specific side of the merge, or use
\fB\-m\fR
to replace them with the original conflicted merge result\&.
.RE
.PP
\fBgit\fR \fBcheckout\fR (\fB\-p\fR|\fB\-\-patch\fR) [\fI<tree\-ish>\fR] [\fB\-\-\fR] [\fI<pathspec>\fR\&.\&.\&.]
.RS 4
This is similar to the previous two modes, but lets you use the interactive interface to show the "diff" output and choose which hunks to use in the result\&. See below for the description of
\fB\-\-patch\fR
option\&.
.RE
.SH "OPTIONS"
.PP
\fB\-q\fR, \fB\-\-quiet\fR
.RS 4
Quiet, suppress feedback messages\&.
.RE
.PP
\fB\-\-progress\fR, \fB\-\-no\-progress\fR
.RS 4
Progress status is reported on the standard error stream by default when it is attached to a terminal, unless
\fB\-\-quiet\fR
is specified\&. This flag enables progress reporting even if not attached to a terminal, regardless of
\fB\-\-quiet\fR\&.
.RE
.PP
\fB\-f\fR, \fB\-\-force\fR
.RS 4
When switching branches, proceed even if the index or the working tree differs from
\fBHEAD\fR, and even if there are untracked files in the way\&. This is used to throw away local changes and any untracked files or directories that are in the way\&.
.sp
When checking out paths from the index, do not fail upon unmerged entries; instead, unmerged entries are ignored\&.
.RE
.PP
\fB\-\-ours\fR, \fB\-\-theirs\fR
.RS 4
When checking out paths from the index, check out stage #2 (\fBours\fR) or #3 (\fBtheirs\fR) for unmerged paths\&.
.sp
Note that during
\fBgit\fR
\fBrebase\fR
and
\fBgit\fR
\fBpull\fR
\fB\-\-rebase\fR,
\fBours\fR
and
\fBtheirs\fR
may appear swapped;
\fB\-\-ours\fR
gives the version from the branch the changes are rebased onto, while
\fB\-\-theirs\fR
gives the version from the branch that holds your work that is being rebased\&.
.sp
This is because
\fBrebase\fR
is used in a workflow that treats the history at the remote as the shared canonical one, and treats the work done on the branch you are rebasing as the third\-party work to be integrated, and you are temporarily assuming the role of the keeper of the canonical history during the rebase\&. As the keeper of the canonical history, you need to view the history from the remote as
\fBours\fR
(i\&.e\&. "our shared canonical history"), while what you did on your side branch as
\fBtheirs\fR
(i\&.e\&. "one contributor\(cqs work on top of it")\&.
.RE
.PP
\fB\-b\fR \fI<new\-branch>\fR
.RS 4
Create a new branch named
\fI<new\-branch>\fR, start it at
\fI<start\-point>\fR, and check the resulting branch out; see
\fBgit-branch\fR(1)
for details\&.
.RE
.PP
\fB\-B\fR \fI<new\-branch>\fR
.RS 4
The same as
\fB\-b\fR, except that if the branch already exists it resets
\fI<branch>\fR
to the start point instead of failing\&.
.RE
.PP
\fB\-t\fR, \fB\-\-track\fR[\fB=\fR(\fBdirect\fR|\fBinherit\fR)]
.RS 4
When creating a new branch, set up "upstream" configuration\&. See
\fB\-\-track\fR
in
\fBgit-branch\fR(1)
for details\&. As a convenience, \-\-track without \-b implies branch creation\&.
.sp
If no
\fB\-b\fR
option is given, the name of the new branch will be derived from the remote\-tracking branch, by looking at the local part of the refspec configured for the corresponding remote, and then stripping the initial part up to the "*"\&. This would tell us to use
\fBhack\fR
as the local branch when branching off of
\fBorigin/hack\fR
(or
\fBremotes/origin/hack\fR, or even
\fBrefs/remotes/origin/hack\fR)\&. If the given name has no slash, or the above guessing results in an empty name, the guessing is aborted\&. You can explicitly give a name with
\fB\-b\fR
in such a case\&.
.RE
.PP
\fB\-\-no\-track\fR
.RS 4
Do not set up "upstream" configuration, even if the
\fBbranch\&.autoSetupMerge\fR
configuration variable is true\&.
.RE
.PP
\fB\-\-guess\fR, \fB\-\-no\-guess\fR
.RS 4
If
\fI<branch>\fR
is not found but there does exist a tracking branch in exactly one remote (call it
\fI<remote>\fR) with a matching name, treat as equivalent to
.sp
.if n \{\
.RS 4
.\}
.nf
$ git checkout \-b <branch> \-\-track <remote>/<branch>
.fi
.if n \{\
.RE
.\}
.sp
If the branch exists in multiple remotes and one of them is named by the
\fBcheckout\&.defaultRemote\fR
configuration variable, we\(cqll use that one for the purposes of disambiguation, even if the
\fI<branch>\fR
isn\(cqt unique across all remotes\&. Set it to e\&.g\&.
\fBcheckout\&.defaultRemote=origin\fR
to always checkout remote branches from there if
\fI<branch>\fR
is ambiguous but exists on the
\fIorigin\fR
remote\&. See also
\fBcheckout\&.defaultRemote\fR
in
\fBgit-config\fR(1)\&.
.sp
\fB\-\-guess\fR
is the default behavior\&. Use
\fB\-\-no\-guess\fR
to disable it\&.
.sp
The default behavior can be set via the
\fBcheckout\&.guess\fR
configuration variable\&.
.RE
.PP
\fB\-l\fR
.RS 4
Create the new branch\(cqs reflog; see
\fBgit-branch\fR(1)
for details\&.
.RE
.PP
\fB\-d\fR, \fB\-\-detach\fR
.RS 4
Rather than checking out a branch to work on it, check out a commit for inspection and discardable experiments\&. This is the default behavior of
\fBgit\fR
\fBcheckout\fR
\fI<commit>\fR
when
\fI<commit>\fR
is not a branch name\&. See the "DETACHED HEAD" section below for details\&.
.RE
.PP
\fB\-\-orphan\fR \fI<new\-branch>\fR
.RS 4
Create a new unborn branch, named
\fI<new\-branch>\fR, started from
\fI<start\-point>\fR
and switch to it\&. The first commit made on this new branch will have no parents and it will be the root of a new history totally disconnected from all the other branches and commits\&.
.sp
The index and the working tree are adjusted as if you had previously run
\fBgit\fR
\fBcheckout\fR
\fI<start\-point>\fR\&. This allows you to start a new history that records a set of paths similar to
\fI<start\-point>\fR
by easily running
\fBgit\fR
\fBcommit\fR
\fB\-a\fR
to make the root commit\&.
.sp
This can be useful when you want to publish the tree from a commit without exposing its full history\&. You might want to do this to publish an open source branch of a project whose current tree is "clean", but whose full history contains proprietary or otherwise encumbered bits of code\&.
.sp
If you want to start a disconnected history that records a set of paths that is totally different from the one of
\fI<start\-point>\fR, then you should clear the index and the working tree right after creating the orphan branch by running
\fBgit\fR
\fBrm\fR
\fB\-rf\fR
\&. from the top level of the working tree\&. Afterwards you will be ready to prepare your new files, repopulating the working tree, by copying them from elsewhere, extracting a tarball, etc\&.
.RE
.PP
\fB\-\-ignore\-skip\-worktree\-bits\fR
.RS 4
In sparse checkout mode,
\fBgit\fR
\fBcheckout\fR
\fB\-\-\fR
\fI<path>\fR\&.\&.\&. would update only entries matched by
\fI<paths>\fR
and sparse patterns in
\fB$GIT_DIR/info/sparse\-checkout\fR\&. This option ignores the sparse patterns and adds back any files in
\fI<path>\fR\&.\&.\&.\&.
.RE
.PP
\fB\-m\fR, \fB\-\-merge\fR
.RS 4
When switching branches, if you have local modifications to one or more files that are different between the current branch and the branch to which you are switching, the command refuses to switch branches in order to preserve your modifications in context\&. With this option, the conflicting local changes are automatically stashed before the switch and reapplied afterwards\&. If the local changes do not overlap with the differences between branches, the switch proceeds without stashing\&. If reapplying the stash results in conflicts, the entry is saved to the stash list\&. Resolve the conflicts and run
\fBgit\fR
\fBstash\fR
\fBdrop\fR
when done, or clear the working tree (e\&.g\&. with
\fBgit\fR
\fBreset\fR
\fB\-\-hard\fR) before running
\fBgit\fR
\fBstash\fR
\fBpop\fR
later to re\-apply your changes\&.
.sp
When checking out paths from the index, this option lets you recreate the conflicted merge in the specified paths\&. This option cannot be used when checking out paths from a tree\-ish\&.
.RE
.PP
\fB\-\-conflict=\fR\fI<style>\fR
.RS 4
The same as
\fB\-\-merge\fR
option above, but changes the way the conflicting hunks are presented, overriding the
\fBmerge\&.conflictStyle\fR
configuration variable\&. Possible values are
\fBmerge\fR
(default),
\fBdiff3\fR, and
\fBzdiff3\fR\&.
.RE
.PP
\fB\-p\fR, \fB\-\-patch\fR
.RS 4
Interactively select hunks in the difference between the
\fI<tree\-ish>\fR
(or the index, if unspecified) and the working tree\&. The chosen hunks are then applied in reverse to the working tree (and if a
\fI<tree\-ish>\fR
was specified, the index)\&.
.sp
This means that you can use
\fBgit\fR
\fBcheckout\fR
\fB\-p\fR
to selectively discard edits from your current working tree\&. See the "Interactive Mode" section of
\fBgit-add\fR(1)
to learn how to operate the
\fB\-\-patch\fR
mode\&.
.sp
Note that this option uses the no overlay mode by default (see also
\fB\-\-overlay\fR), and currently doesn\(cqt support overlay mode\&.
.RE
.PP
\fB\-U\fR\fI<n>\fR, \fB\-\-unified=\fR\fI<n>\fR
.RS 4
Generate diffs with
\fI<n>\fR
lines of context\&. The number of context lines defaults to
\fBdiff\&.context\fR
or 3 if the configuration variable is unset\&. (\fB\-U\fR
without
\fI<n>\fR
is silently accepted as a synonym for
\fB\-p\fR
due to a historical accident)\&.
.RE
.PP
\fB\-\-inter\-hunk\-context=\fR\fI<n>\fR
.RS 4
Show the context between diff hunks, up to the specified
\fI<number>\fR
of lines, thereby fusing hunks that are close to each other\&. Defaults to
\fBdiff\&.interHunkContext\fR
or 0 if the config option is unset\&.
.RE
.PP
\fB\-\-ignore\-other\-worktrees\fR
.RS 4
\fBgit\fR
\fBcheckout\fR
refuses when the wanted branch is already checked out or otherwise in use by another worktree\&. This option makes it check the branch out anyway\&. In other words, the branch can be in use by more than one worktree\&.
.RE
.PP
\fB\-\-overwrite\-ignore\fR, \fB\-\-no\-overwrite\-ignore\fR
.RS 4
Silently overwrite ignored files when switching branches\&. This is the default behavior\&. Use
\fB\-\-no\-overwrite\-ignore\fR
to abort the operation when the new branch contains ignored files\&.
.RE
.PP
\fB\-\-recurse\-submodules\fR, \fB\-\-no\-recurse\-submodules\fR
.RS 4
Using
\fB\-\-recurse\-submodules\fR
will update the content of all active submodules according to the commit recorded in the superproject\&. If local modifications in a submodule would be overwritten the checkout will fail unless
\fB\-f\fR
is used\&. If nothing (or
\fB\-\-no\-recurse\-submodules\fR) is used, submodules working trees will not be updated\&. Just like
\fBgit-submodule\fR(1), this will detach
\fBHEAD\fR
of the submodule\&.
.RE
.PP
\fB\-\-overlay\fR, \fB\-\-no\-overlay\fR
.RS 4
In the default overlay mode,
\fBgit\fR
\fBcheckout\fR
never removes files from the index or the working tree\&. When specifying
\fB\-\-no\-overlay\fR, files that appear in the index and working tree, but not in
\fI<tree\-ish>\fR
are removed, to make them match
\fI<tree\-ish>\fR
exactly\&.
.RE
.PP
\fB\-\-pathspec\-from\-file=\fR\fI<file>\fR
.RS 4
Pathspec is passed in
\fI<file>\fR
instead of commandline args\&. If
\fI<file>\fR
is exactly
\fB\-\fR
then standard input is used\&. Pathspec elements are separated by
\fILF\fR
or
\fICR\fR/\fILF\fR\&. Pathspec elements can be quoted as explained for the configuration variable
\fBcore\&.quotePath\fR
(see
\fBgit-config\fR(1))\&. See also
\fB\-\-pathspec\-file\-nul\fR
and global
\fB\-\-literal\-pathspecs\fR\&.
.RE
.PP
\fB\-\-pathspec\-file\-nul\fR
.RS 4
Only meaningful with
\fB\-\-pathspec\-from\-file\fR\&. Pathspec elements are separated with
\fINUL\fR
character and all other characters are taken literally (including newlines and quotes)\&.
.RE
.PP
\fI<branch>\fR
.RS 4
Branch to checkout; if it refers to a branch (i\&.e\&., a name that, when prepended with "refs/heads/", is a valid ref), then that branch is checked out\&. Otherwise, if it refers to a valid commit, your
\fBHEAD\fR
becomes "detached" and you are no longer on any branch (see below for details)\&.
.sp
You can use the
\fB@\fR{\-N} syntax to refer to the N\-th last branch/commit checked out using "git checkout" operation\&. You may also specify
\fB\-\fR
which is synonymous to
\fB@\fR{\-1}\&.
.sp
As a special case, you may use
\fI<rev\-a>\fR\fB\&.\&.\&.\fR\fI<rev\-b>\fR
as a shortcut for the merge base of
\fI<rev\-a>\fR
and
\fI<rev\-b>\fR
if there is exactly one merge base\&. You can leave out at most one of
\fI<rev\-a>\fR
and
\fI<rev\-b>\fR, in which case it defaults to
\fBHEAD\fR\&.
.RE
.PP
\fI<new\-branch>\fR
.RS 4
Name for the new branch\&.
.RE
.PP
\fI<start\-point>\fR
.RS 4
The name of a commit at which to start the new branch; see
\fBgit-branch\fR(1)
for details\&. Defaults to
\fBHEAD\fR\&.
.sp
As a special case, you may use
\fI<rev\-a>\fR\fB\&.\&.\&.\fR\fI<rev\-b>\fR
as a shortcut for the merge base of
\fI<rev\-a>\fR
and
\fI<rev\-b>\fR
if there is exactly one merge base\&. You can leave out at most one of
\fI<rev\-a>\fR
and
\fI<rev\-b>\fR, in which case it defaults to
\fBHEAD\fR\&.
.RE
.PP
\fI<tree\-ish>\fR
.RS 4
Tree to checkout from (when paths are given)\&. If not specified, the index will be used\&.
.sp
As a special case, you may use
\fI<rev\-a>\fR\fB\&.\&.\&.\fR\fI<rev\-b>\fR
as a shortcut for the merge base of
\fI<rev\-a>\fR
and
\fI<rev\-b>\fR
if there is exactly one merge base\&. You can leave out at most one of
\fI<rev\-a>\fR
and
\fI<rev\-b>\fR, in which case it defaults to
\fBHEAD\fR\&.
.RE
.PP
\fB\-\-\fR
.RS 4
Do not interpret any more arguments as options\&.
.RE
.PP
\fI<pathspec>\fR\&.\&.\&.
.RS 4
Limits the paths affected by the operation\&.
.sp
For more details, see the
\fIpathspec\fR
entry in
\fBgitglossary\fR(7)\&.
.RE
.SH "DETACHED HEAD"
.sp
\fBHEAD\fR normally refers to a named branch (e\&.g\&. \fBmaster\fR)\&. Meanwhile, each branch refers to a specific commit\&. Let\(cqs look at a repo with three commits, one of them tagged, and with branch \fBmaster\fR checked out:
.sp
.if n \{\
.RS 4
.\}
.nf
           HEAD (refers to branch \*(Aqmaster\*(Aq)
            |
            v
a\-\-\-b\-\-\-c  branch \*(Aqmaster\*(Aq (refers to commit \*(Aqc\*(Aq)
    ^
    |
  tag \*(Aqv2\&.0\*(Aq (refers to commit \*(Aqb\*(Aq)
.fi
.if n \{\
.RE
.\}
.sp
When a commit is created in this state, the branch is updated to refer to the new commit\&. Specifically, \fBgit\fR \fBcommit\fR creates a new commit \fBd\fR, whose parent is commit \fBc\fR, and then updates branch \fBmaster\fR to refer to new commit \fBd\fR\&. \fBHEAD\fR still refers to branch \fBmaster\fR and so indirectly now refers to commit \fBd\fR:
.sp
.if n \{\
.RS 4
.\}
.nf
$ edit; git add; git commit

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

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

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

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

               HEAD (refers to branch \*(Aqmaster\*(Aq)
      e\-\-\-f     |
     /          v
a\-\-\-b\-\-\-c\-\-\-d  branch \*(Aqmaster\*(Aq (refers to commit \*(Aqd\*(Aq)
    ^
    |
  tag \*(Aqv2\&.0\*(Aq (refers to commit \*(Aqb\*(Aq)
.fi
.if n \{\
.RE
.\}
.sp
It is important to realize that at this point nothing refers to commit \fBf\fR\&. Eventually commit \fBf\fR (and by extension commit \fBe\fR) will be deleted by the routine Git garbage collection process, unless we create a reference before that happens\&. If we have not yet moved away from commit \fBf\fR, any of these will create a reference to it:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git checkout \-b foo  # or "git switch \-c foo"  \fB(1)\fR
$ git branch foo                                 \fB(2)\fR
$ git tag foo                                    \fB(3)\fR
.fi
.if n \{\
.RE
.\}
.sp
.TS
tab(:);
r lw(\n(.lu*75u/100u).
\fB1.\fR\h'-2n':T{
creates a new branch
\fBfoo\fR, which refers to commit
\fBf\fR, and then updates
\fBHEAD\fR
to refer to branch
\fBfoo\fR\&. In other words, we\(cqll no longer be in detached
\fBHEAD\fR
state after this command\&.
T}
\fB2.\fR\h'-2n':T{
similarly creates a new branch
\fBfoo\fR, which refers to commit
\fBf\fR, but leaves
\fBHEAD\fR
detached\&.
T}
\fB3.\fR\h'-2n':T{
creates a new tag
\fBfoo\fR, which refers to commit
\fBf\fR, leaving
\fBHEAD\fR
detached\&.
T}
.TE
.sp
If we have moved away from commit \fBf\fR, then we must first recover its object name (typically by using git reflog), and then we can create a reference to it\&. For example, to see the last two commits to which \fBHEAD\fR referred, we can use either of these commands:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git reflog \-2 HEAD # or
$ git log \-g \-2 HEAD
.fi
.if n \{\
.RE
.\}
.SH "ARGUMENT DISAMBIGUATION"
.sp
When you run \fBgit\fR \fBcheckout\fR \fI<something>\fR, Git tries to guess whether \fI<something>\fR is intended to be a branch, a commit, or a set of file(s), and then either switches to that branch or commit, or restores the specified files\&.
.sp
If there\(cqs any ambiguity, Git will treat \fI<something>\fR as a branch or commit, but you can use the double dash \fB\-\-\fR to force Git to treat the parameter as a list of files and/or directories, like this:
.sp
.if n \{\
.RS 4
.\}
.nf
git checkout \-\- file\&.txt
.fi
.if n \{\
.RE
.\}
.SH "EXAMPLES"
.SS "1\&. Paths"
.sp
The following sequence checks out the \fBmaster\fR branch, reverts the \fBMakefile\fR to two revisions back, deletes \fBhello\&.c\fR by mistake, and gets it back from the index\&.
.sp
.if n \{\
.RS 4
.\}
.nf
$ git checkout master             \fB(1)\fR
$ git checkout master~2 Makefile  \fB(2)\fR
$ rm \-f hello\&.c
$ git checkout hello\&.c            \fB(3)\fR
.fi
.if n \{\
.RE
.\}
.sp
.TS
tab(:);
r lw(\n(.lu*75u/100u).
\fB1.\fR\h'-2n':T{
switch branch
T}
\fB2.\fR\h'-2n':T{
take a file out of another commit
T}
\fB3.\fR\h'-2n':T{
restore
\fBhello\&.c\fR
from the index
T}
.TE
.sp
If you want to check out \fIall\fR C source files out of the index, you can say
.sp
.if n \{\
.RS 4
.\}
.nf
$ git checkout \-\- \*(Aq*\&.c\*(Aq
.fi
.if n \{\
.RE
.\}
.sp
Note the quotes around \fB*\&.c\fR\&. The file \fBhello\&.c\fR will also be checked out, even though it is no longer in the working tree, because the file globbing is used to match entries in the index (not in the working tree by the shell)\&.
.sp
If you have an unfortunate branch that is named \fBhello\&.c\fR, this step would be confused as an instruction to switch to that branch\&. You should instead write:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git checkout \-\- hello\&.c
.fi
.if n \{\
.RE
.\}
.SS "2\&. Merge"
.sp
After working in the wrong branch, switching to the correct branch would be done using:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git checkout mytopic
.fi
.if n \{\
.RE
.\}
.sp
However, your "wrong" branch and correct \fBmytopic\fR branch may differ in files that you have modified locally, in which case the above checkout would fail like this:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git checkout mytopic
error: You have local changes to \*(Aqfrotz\*(Aq; not switching branches\&.
.fi
.if n \{\
.RE
.\}
.sp
You can give the \fB\-m\fR flag to the command, which will carry your local changes to the new branch:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git checkout \-m mytopic
Applied autostash\&.
Switched to branch \*(Aqmytopic\*(Aq
The following paths have local changes:
M       frotz
.fi
.if n \{\
.RE
.\}
.sp
After the switch, the local modifications are reapplied and are \fInot\fR registered in your index file, so \fBgit\fR \fBdiff\fR would show you what changes you made since the tip of the new branch\&.
.SS "3\&. Merge conflict"
.sp
When the \fB\-\-merge\fR (\fB\-m\fR) option is given and the local changes overlap with the changes in the branch we\(cqre switching to, the changes are stashed and reapplied after the switch\&. If this process results in conflicts, the stash entry is saved and a message is printed:
.sp
.if n \{\
.RS 4
.\}
.nf
$ git checkout \-m mytopic
Your local changes are stashed, however applying them
resulted in conflicts\&.  You can either resolve the conflicts
and then discard the stash with "git stash drop", or, if you
do not want to resolve them now, run "git reset \-\-hard" and
apply the local changes later by running "git stash pop"\&.
.fi
.if n \{\
.RE
.\}
.SH "CONFIGURATION"
.sp
Everything below this line in this section is selectively included from the \fBgit-config\fR(1) documentation\&. The content is the same as what\(cqs found there:
.PP
\fBcheckout\&.defaultRemote\fR
.RS 4
When you run
\fBgit\fR
\fBcheckout\fR
\fI<something>\fR
or
\fBgit\fR
\fBswitch\fR
\fI<something>\fR
and only have one remote, it may implicitly fall back on checking out and tracking e\&.g\&.
\fBorigin/\fR\fI<something>\fR\&. This stops working as soon as you have more than one remote with a
\fI<something>\fR
reference\&. This setting allows for setting the name of a preferred remote that should always win when it comes to disambiguation\&. The typical use\-case is to set this to
\fBorigin\fR\&.
.sp
Currently this is used by
\fBgit-switch\fR(1)
and
\fBgit-checkout\fR(1)
when
\fBgit\fR
\fBcheckout\fR
\fI<something>\fR
or
\fBgit\fR
\fBswitch\fR
\fI<something>\fR
will checkout the
\fI<something>\fR
branch on another remote, and by
\fBgit-worktree\fR(1)
when
\fBgit\fR
\fBworktree\fR
\fBadd\fR
refers to a remote branch\&. This setting might be used for other checkout\-like commands or functionality in the future\&.
.RE
.PP
\fBcheckout\&.guess\fR
.RS 4
Provides the default value for the
\fB\-\-guess\fR
or
\fB\-\-no\-guess\fR
option in
\fBgit\fR
\fBcheckout\fR
and
\fBgit\fR
\fBswitch\fR\&. See
\fBgit-switch\fR(1)
and
\fBgit-checkout\fR(1)\&.
.RE
.PP
\fBcheckout\&.workers\fR
.RS 4
The number of parallel workers to use when updating the working tree\&. The default is one, i\&.e\&. sequential execution\&. If set to a value less than one, Git will use as many workers as the number of logical cores available\&. This setting and
\fBcheckout\&.thresholdForParallelism\fR
affect all commands that perform checkout\&. E\&.g\&. checkout, clone, reset, sparse\-checkout, etc\&.
.if n \{\
.sp
.\}
.RS 4
.it 1 an-trap
.nr an-no-space-flag 1
.nr an-break-flag 1
.br
.ps +1
\fBNote\fR
.ps -1
.br
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\&.
.sp .5v
.RE
.RE
.PP
\fBcheckout\&.thresholdForParallelism\fR
.RS 4
When running parallel checkout with a small number of files, the cost of subprocess spawning and inter\-process communication might outweigh the parallelization gains\&. This setting allows you to define the minimum number of files for which parallel checkout should be attempted\&. The default is 100\&.
.RE
.SH "SEE ALSO"
.sp
\fBgit-switch\fR(1), \fBgit-restore\fR(1)
.SH "GIT"
.sp
Part of the \fBgit\fR(1) suite
