<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sect2 SYSTEM "../../../dtd/dblite.dtd">

<sect2 lang="en" id="git-checkout(1)">
    <title>git-checkout(1)</title>
    <date>2024-07-02</date>
<revhistory><revision><date>2024-07-02</date></revision></revhistory>
<indexterm>
<primary>git-checkout(1)</primary>
</indexterm>
<simplesect id="git-checkout(1)__name">
<title>NAME</title>
<simpara>git-checkout - Switch branches or restore working tree files</simpara>
</simplesect>
<simplesect id="git-checkout(1)__synopsis">
<title>SYNOPSIS</title>
<blockquote>
<literallayout><emphasis>git checkout</emphasis> [-q] [-f] [-m] [&lt;branch&gt;]
<emphasis>git checkout</emphasis> [-q] [-f] [-m] --detach [&lt;branch&gt;]
<emphasis>git checkout</emphasis> [-q] [-f] [-m] [--detach] &lt;commit&gt;
<emphasis>git checkout</emphasis> [-q] [-f] [-m] [[-b|-B|--orphan] &lt;new-branch&gt;] [&lt;start-point&gt;]
<emphasis>git checkout</emphasis> [-f] &lt;tree-ish&gt; [--] &lt;pathspec&gt;&#8230;
<emphasis>git checkout</emphasis> [-f] &lt;tree-ish&gt; --pathspec-from-file=&lt;file&gt; [--pathspec-file-nul]
<emphasis>git checkout</emphasis> [-f|--ours|--theirs|-m|--conflict=&lt;style&gt;] [--] &lt;pathspec&gt;&#8230;
<emphasis>git checkout</emphasis> [-f|--ours|--theirs|-m|--conflict=&lt;style&gt;] --pathspec-from-file=&lt;file&gt; [--pathspec-file-nul]
<emphasis>git checkout</emphasis> (-p|--patch) [&lt;tree-ish&gt;] [--] [&lt;pathspec&gt;&#8230;]</literallayout>
</blockquote>
</simplesect>
<simplesect id="git-checkout(1)__description">
<title>DESCRIPTION</title>
<simpara>Updates files in the working tree to match the version in the index
or the specified tree.  If no pathspec was given, <emphasis>git checkout</emphasis> will
also update <emphasis>HEAD</emphasis> to set the specified branch as the current
branch.</simpara>
<variablelist>
<varlistentry>
<term>
<emphasis>git checkout</emphasis> [&lt;branch&gt;]
</term>
<listitem>
<simpara>
        To prepare for working on <emphasis>&lt;branch&gt;</emphasis>, switch to it by updating
        the index and the files in the working tree, and by pointing
        <emphasis>HEAD</emphasis> at the branch. Local modifications to the files in the
        working tree are kept, so that they can be committed to the
        <emphasis>&lt;branch&gt;</emphasis>.
</simpara>
<simpara>If <emphasis>&lt;branch&gt;</emphasis> is not found but there does exist a tracking branch in
exactly one remote (call it <emphasis>&lt;remote&gt;</emphasis>) with a matching name and
<emphasis>--no-guess</emphasis> is not specified, treat as equivalent to</simpara>
<screen>$ git checkout -b &lt;branch&gt; --track &lt;remote&gt;/&lt;branch&gt;</screen>
<simpara>You could omit <emphasis>&lt;branch&gt;</emphasis>, 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.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<emphasis>git checkout</emphasis> -b|-B &lt;new-branch&gt; [&lt;start-point&gt;]
</term>
<listitem>
<simpara>
        Specifying <emphasis>-b</emphasis> causes a new branch to be created as if
        <xref linkend="git-branch(1)" /> were called and then checked out.  In
        this case you can use the <emphasis>--track</emphasis> or <emphasis>--no-track</emphasis> options,
        which will be passed to <emphasis>git branch</emphasis>.  As a convenience,
        <emphasis>--track</emphasis> without <emphasis>-b</emphasis> implies branch creation; see the
        description of <emphasis>--track</emphasis> below.
</simpara>
<simpara>If <emphasis>-B</emphasis> is given, <emphasis>&lt;new-branch&gt;</emphasis> is created if it doesn't exist; otherwise, it
is reset. This is the transactional equivalent of</simpara>
<screen>$ git branch -f &lt;branch&gt; [&lt;start-point&gt;]
$ git checkout &lt;branch&gt;</screen>
<simpara>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).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<emphasis>git checkout</emphasis> --detach [&lt;branch&gt;]
</term>
<term>
<emphasis>git checkout</emphasis> [--detach] &lt;commit&gt;
</term>
<listitem>
<simpara>
        Prepare to work on top of <emphasis>&lt;commit&gt;</emphasis>, by detaching <emphasis>HEAD</emphasis> 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.
</simpara>
<simpara>When the <emphasis>&lt;commit&gt;</emphasis> argument is a branch name, the <emphasis>--detach</emphasis> option can
be used to detach <emphasis>HEAD</emphasis> at the tip of the branch (<emphasis>git checkout
&lt;branch&gt;</emphasis> would check out that branch without detaching <emphasis>HEAD</emphasis>).</simpara>
<simpara>Omitting <emphasis>&lt;branch&gt;</emphasis> detaches <emphasis>HEAD</emphasis> at the tip of the current branch.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<emphasis>git checkout</emphasis> [-f|--ours|--theirs|-m|--conflict=&lt;style&gt;] [&lt;tree-ish&gt;] [--] &lt;pathspec&gt;&#8230;
</term>
<term>
<emphasis>git checkout</emphasis> [-f|--ours|--theirs|-m|--conflict=&lt;style&gt;] [&lt;tree-ish&gt;] --pathspec-from-file=&lt;file&gt; [--pathspec-file-nul]
</term>
<listitem>
<simpara>
        Overwrite the contents of the files that match the pathspec.
        When the <emphasis>&lt;tree-ish&gt;</emphasis> (most often a commit) is not given,
        overwrite working tree with the contents in the index.
        When the <emphasis>&lt;tree-ish&gt;</emphasis> is given, overwrite both the index and
        the working tree with the contents at the <emphasis>&lt;tree-ish&gt;</emphasis>.
</simpara>
<simpara>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 <emphasis>-f</emphasis> will ignore these unmerged entries.  The contents from a
specific side of the merge can be checked out of the index by
using <emphasis>--ours</emphasis> or <emphasis>--theirs</emphasis>.  With <emphasis>-m</emphasis>, changes made to the working tree
file can be discarded to re-create the original conflicted merge result.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<emphasis>git checkout</emphasis> (-p|--patch) [&lt;tree-ish&gt;] [--] [&lt;pathspec&gt;&#8230;]
</term>
<listitem>
<simpara>
        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
        <emphasis>--patch</emphasis> option.
</simpara>
</listitem>
</varlistentry>
</variablelist>
</simplesect>
<simplesect id="git-checkout(1)__options">
<title>OPTIONS</title>
<variablelist>
<varlistentry>
<term>
-q
</term>
<term>
--quiet
</term>
<listitem>
<simpara>
        Quiet, suppress feedback messages.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--progress
</term>
<term>
--no-progress
</term>
<listitem>
<simpara>
        Progress status is reported on the standard error stream
        by default when it is attached to a terminal, unless <emphasis>--quiet</emphasis>
        is specified. This flag enables progress reporting even if not
        attached to a terminal, regardless of <emphasis>--quiet</emphasis>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-f
</term>
<term>
--force
</term>
<listitem>
<simpara>
        When switching branches, proceed even if the index or the
        working tree differs from <emphasis>HEAD</emphasis>, 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.
</simpara>
<simpara>When checking out paths from the index, do not fail upon unmerged
entries; instead, unmerged entries are ignored.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--ours
</term>
<term>
--theirs
</term>
<listitem>
<simpara>
        When checking out paths from the index, check out stage #2
        (<emphasis>ours</emphasis>) or #3 (<emphasis>theirs</emphasis>) for unmerged paths.
</simpara>
<simpara>Note that during <emphasis>git rebase</emphasis> and <emphasis>git pull --rebase</emphasis>, <emphasis>ours</emphasis> and
<emphasis>theirs</emphasis> may appear swapped; <emphasis>--ours</emphasis> gives the version from the
branch the changes are rebased onto, while <emphasis>--theirs</emphasis> gives the
version from the branch that holds your work that is being rebased.</simpara>
<simpara>This is because <emphasis>rebase</emphasis> 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 <emphasis>ours</emphasis> (i.e. "our shared canonical history"), while what you did
on your side branch as <emphasis>theirs</emphasis> (i.e. "one contributor's work on top
of it").</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-b &lt;new-branch&gt;
</term>
<listitem>
<simpara>
        Create a new branch named <emphasis>&lt;new-branch&gt;</emphasis>, start it at
        <emphasis>&lt;start-point&gt;</emphasis>, and check the resulting branch out;
        see <xref linkend="git-branch(1)" /> for details.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-B &lt;new-branch&gt;
</term>
<listitem>
<simpara>
        Creates the branch <emphasis>&lt;new-branch&gt;</emphasis>, start it at <emphasis>&lt;start-point&gt;</emphasis>;
        if it already exists, then reset it to <emphasis>&lt;start-point&gt;</emphasis>. And then
        check the resulting branch out.  This is equivalent to running
        "git branch" with "-f" followed by "git checkout" of that branch;
        see <xref linkend="git-branch(1)" /> for details.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-t
</term>
<term>
--track[=(direct|inherit)]
</term>
<listitem>
<simpara>
        When creating a new branch, set up "upstream" configuration. See
        "--track" in <xref linkend="git-branch(1)" /> for details.
</simpara>
<simpara>If no <emphasis>-b</emphasis> 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 <emphasis>hack</emphasis> as the local branch when branching
off of <emphasis>origin/hack</emphasis> (or <emphasis>remotes/origin/hack</emphasis>, or even
<emphasis>refs/remotes/origin/hack</emphasis>).  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 <emphasis>-b</emphasis> in such a case.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--no-track
</term>
<listitem>
<simpara>
        Do not set up "upstream" configuration, even if the
        <emphasis>branch.autoSetupMerge</emphasis> configuration variable is true.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--guess
</term>
<term>
--no-guess
</term>
<listitem>
<simpara>
        If <emphasis>&lt;branch&gt;</emphasis> is not found but there does exist a tracking
        branch in exactly one remote (call it <emphasis>&lt;remote&gt;</emphasis>) with a
        matching name, treat as equivalent to
</simpara>
<screen>$ git checkout -b &lt;branch&gt; --track &lt;remote&gt;/&lt;branch&gt;</screen>
<simpara>If the branch exists in multiple remotes and one of them is named by
the <emphasis>checkout.defaultRemote</emphasis> configuration variable, we'll use that
one for the purposes of disambiguation, even if the <emphasis>&lt;branch&gt;</emphasis> isn't
unique across all remotes. Set it to
e.g. <emphasis>checkout.defaultRemote=origin</emphasis> to always checkout remote
branches from there if <emphasis>&lt;branch&gt;</emphasis> is ambiguous but exists on the
<emphasis>origin</emphasis> remote. See also <emphasis>checkout.defaultRemote</emphasis> in
<xref linkend="git-config(1)" />.</simpara>
<simpara><emphasis>--guess</emphasis> is the default behavior. Use <emphasis>--no-guess</emphasis> to disable it.</simpara>
<simpara>The default behavior can be set via the <emphasis>checkout.guess</emphasis> configuration
variable.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-l
</term>
<listitem>
<simpara>
        Create the new branch's reflog; see <xref linkend="git-branch(1)" /> for
        details.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-d
</term>
<term>
--detach
</term>
<listitem>
<simpara>
        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 <emphasis>git checkout &lt;commit&gt;</emphasis> when
        <emphasis>&lt;commit&gt;</emphasis> is not a branch name.  See the "DETACHED HEAD" section
        below for details.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--orphan &lt;new-branch&gt;
</term>
<listitem>
<simpara>
        Create a new unborn branch, named <emphasis>&lt;new-branch&gt;</emphasis>, started from
        <emphasis>&lt;start-point&gt;</emphasis> 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.
</simpara>
<simpara>The index and the working tree are adjusted as if you had previously run
<emphasis>git checkout &lt;start-point&gt;</emphasis>.  This allows you to start a new history
that records a set of paths similar to <emphasis>&lt;start-point&gt;</emphasis> by easily running
<emphasis>git commit -a</emphasis> to make the root commit.</simpara>
<simpara>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.</simpara>
<simpara>If you want to start a disconnected history that records a set of paths
that is totally different from the one of <emphasis>&lt;start-point&gt;</emphasis>, then you should
clear the index and the working tree right after creating the orphan
branch by running <emphasis>git rm -rf .</emphasis> 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.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--ignore-skip-worktree-bits
</term>
<listitem>
<simpara>
        In sparse checkout mode, <emphasis>git checkout -- &lt;paths&gt;</emphasis> would
        update only entries matched by <emphasis>&lt;paths&gt;</emphasis> and sparse patterns
        in <emphasis>$GIT_DIR/info/sparse-checkout</emphasis>. This option ignores
        the sparse patterns and adds back any files in <emphasis>&lt;paths&gt;</emphasis>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-m
</term>
<term>
--merge
</term>
<listitem>
<simpara>
        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.
</simpara>
<simpara>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 <emphasis>git add</emphasis> (or <emphasis>git rm</emphasis> if the merge
should result in deletion of the path).</simpara>
<simpara>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.</simpara>
<simpara>When switching branches with <emphasis>--merge</emphasis>, staged changes may be lost.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--conflict=&lt;style&gt;
</term>
<listitem>
<simpara>
        The same as <emphasis>--merge</emphasis> option above, but changes the way the
        conflicting hunks are presented, overriding the
        <emphasis>merge.conflictStyle</emphasis> configuration variable.  Possible values are
        "merge" (default), "diff3", and "zdiff3".
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-p
</term>
<term>
--patch
</term>
<listitem>
<simpara>
        Interactively select hunks in the difference between the
        <emphasis>&lt;tree-ish&gt;</emphasis> (or the index, if unspecified) and the working
        tree.  The chosen hunks are then applied in reverse to the
        working tree (and if a <emphasis>&lt;tree-ish&gt;</emphasis> was specified, the index).
</simpara>
<simpara>This means that you can use <emphasis>git checkout -p</emphasis> to selectively discard
edits from your current working tree. See the Interactive Mode
section of <xref linkend="git-add(1)" /> to learn how to operate the <emphasis>--patch</emphasis> mode.</simpara>
<simpara>Note that this option uses the no overlay mode by default (see also
<emphasis>--overlay</emphasis>), and currently doesn't support overlay mode.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--ignore-other-worktrees
</term>
<listitem>
<simpara>
        <emphasis>git checkout</emphasis> 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.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--overwrite-ignore
</term>
<term>
--no-overwrite-ignore
</term>
<listitem>
<simpara>
        Silently overwrite ignored files when switching branches. This
        is the default behavior. Use <emphasis>--no-overwrite-ignore</emphasis> to abort
        the operation when the new branch contains ignored files.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--recurse-submodules
</term>
<term>
--no-recurse-submodules
</term>
<listitem>
<simpara>
        Using <emphasis>--recurse-submodules</emphasis> 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 <emphasis>-f</emphasis> is used. If nothing (or <emphasis>--no-recurse-submodules</emphasis>)
        is used, submodules working trees will not be updated.
        Just like <xref linkend="git-submodule(1)" />, this will detach <emphasis>HEAD</emphasis> of the
        submodule.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--overlay
</term>
<term>
--no-overlay
</term>
<listitem>
<simpara>
        In the default overlay mode, <emphasis>git checkout</emphasis> never
        removes files from the index or the working tree.  When
        specifying <emphasis>--no-overlay</emphasis>, files that appear in the index and
        working tree, but not in <emphasis>&lt;tree-ish&gt;</emphasis> are removed, to make them
        match <emphasis>&lt;tree-ish&gt;</emphasis> exactly.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--pathspec-from-file=&lt;file&gt;
</term>
<listitem>
<simpara>
        Pathspec is passed in <emphasis>&lt;file&gt;</emphasis> instead of commandline args. If
        <emphasis>&lt;file&gt;</emphasis> is exactly <emphasis>-</emphasis> 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 <emphasis>core.quotePath</emphasis>
        (see <xref linkend="git-config(1)" />). See also <emphasis>--pathspec-file-nul</emphasis> and
        global <emphasis>--literal-pathspecs</emphasis>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--pathspec-file-nul
</term>
<listitem>
<simpara>
        Only meaningful with <emphasis>--pathspec-from-file</emphasis>. Pathspec elements are
        separated with NUL character and all other characters are taken
        literally (including newlines and quotes).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
&lt;branch&gt;
</term>
<listitem>
<simpara>
        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 <emphasis>HEAD</emphasis> becomes "detached" and you are no longer on
        any branch (see below for details).
</simpara>
<simpara>You can use the <emphasis>@{-N}</emphasis> syntax to refer to the N-th last
branch/commit checked out using "git checkout" operation. You may
also specify <emphasis>-</emphasis> which is synonymous to <emphasis>@{-1}</emphasis>.</simpara>
<simpara>As a special case, you may use <emphasis>A...B</emphasis> as a shortcut for the
merge base of <emphasis>A</emphasis> and <emphasis>B</emphasis> if there is exactly one merge base. You can
leave out at most one of <emphasis>A</emphasis> and <emphasis>B</emphasis>, in which case it defaults to <emphasis>HEAD</emphasis>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
&lt;new-branch&gt;
</term>
<listitem>
<simpara>
        Name for the new branch.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
&lt;start-point&gt;
</term>
<listitem>
<simpara>
        The name of a commit at which to start the new branch; see
        <xref linkend="git-branch(1)" /> for details. Defaults to <emphasis>HEAD</emphasis>.
</simpara>
<simpara>As a special case, you may use <emphasis>"A...B"</emphasis> as a shortcut for the
merge base of <emphasis>A</emphasis> and <emphasis>B</emphasis> if there is exactly one merge base. You can
leave out at most one of <emphasis>A</emphasis> and <emphasis>B</emphasis>, in which case it defaults to <emphasis>HEAD</emphasis>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
&lt;tree-ish&gt;
</term>
<listitem>
<simpara>
        Tree to checkout from (when paths are given). If not specified,
        the index will be used.
</simpara>
<simpara>As a special case, you may use <emphasis>"A...B"</emphasis> as a shortcut for the
merge base of <emphasis>A</emphasis> and <emphasis>B</emphasis> if there is exactly one merge base. You can
leave out at most one of <emphasis>A</emphasis> and <emphasis>B</emphasis>, in which case it defaults to <emphasis>HEAD</emphasis>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--
</term>
<listitem>
<simpara>
        Do not interpret any more arguments as options.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
&lt;pathspec&gt;&#8230;
</term>
<listitem>
<simpara>
        Limits the paths affected by the operation.
</simpara>
<simpara>For more details, see the <emphasis>pathspec</emphasis> entry in <xref linkend="gitglossary(7)" />.</simpara>
</listitem>
</varlistentry>
</variablelist>
</simplesect>
<simplesect id="git-checkout(1)__detached_head">
<title>DETACHED HEAD</title>
<simpara><emphasis>HEAD</emphasis> normally refers to a named branch (e.g. <emphasis>master</emphasis>). Meanwhile, each
branch refers to a specific commit. Let's look at a repo with three
commits, one of them tagged, and with branch <emphasis>master</emphasis> checked out:</simpara>
<screen>           HEAD (refers to branch 'master')
            |
            v
a---b---c  branch 'master' (refers to commit 'c')
    ^
    |
  tag 'v2.0' (refers to commit 'b')</screen>
<simpara>When a commit is created in this state, the branch is updated to refer to
the new commit. Specifically, <emphasis>git commit</emphasis> creates a new commit <emphasis>d</emphasis>, whose
parent is commit <emphasis>c</emphasis>, and then updates branch <emphasis>master</emphasis> to refer to new
commit <emphasis>d</emphasis>. <emphasis>HEAD</emphasis> still refers to branch <emphasis>master</emphasis> and so indirectly now refers
to commit <emphasis>d</emphasis>:</simpara>
<screen>$ edit; git add; git commit

               HEAD (refers to branch 'master')
                |
                v
a---b---c---d  branch 'master' (refers to commit 'd')
    ^
    |
  tag 'v2.0' (refers to commit 'b')</screen>
<simpara>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's look at what happens when we
checkout commit <emphasis>b</emphasis> (here we show two ways this may be done):</simpara>
<screen>$ git checkout v2.0  # or
$ git checkout master^^

   HEAD (refers to commit 'b')
    |
    v
a---b---c---d  branch 'master' (refers to commit 'd')
    ^
    |
  tag 'v2.0' (refers to commit 'b')</screen>
<simpara>Notice that regardless of which checkout command we use, <emphasis>HEAD</emphasis> now refers
directly to commit <emphasis>b</emphasis>. This is known as being in detached <emphasis>HEAD</emphasis> state.
It means simply that <emphasis>HEAD</emphasis> refers to a specific commit, as opposed to
referring to a named branch. Let's see what happens when we create a commit:</simpara>
<screen>$ edit; git add; git commit

     HEAD (refers to commit 'e')
      |
      v
      e
     /
a---b---c---d  branch 'master' (refers to commit 'd')
    ^
    |
  tag 'v2.0' (refers to commit 'b')</screen>
<simpara>There is now a new commit <emphasis>e</emphasis>, but it is referenced only by <emphasis>HEAD</emphasis>. We can
of course add yet another commit in this state:</simpara>
<screen>$ edit; git add; git commit

         HEAD (refers to commit 'f')
          |
          v
      e---f
     /
a---b---c---d  branch 'master' (refers to commit 'd')
    ^
    |
  tag 'v2.0' (refers to commit 'b')</screen>
<simpara>In fact, we can perform all the normal Git operations. But, let's look
at what happens when we then checkout <emphasis>master</emphasis>:</simpara>
<screen>$ git checkout master

               HEAD (refers to branch 'master')
      e---f     |
     /          v
a---b---c---d  branch 'master' (refers to commit 'd')
    ^
    |
  tag 'v2.0' (refers to commit 'b')</screen>
<simpara>It is important to realize that at this point nothing refers to commit
<emphasis>f</emphasis>. Eventually commit <emphasis>f</emphasis> (and by extension commit <emphasis>e</emphasis>) 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 <emphasis>f</emphasis>,
any of these will create a reference to it:</simpara>
<screen>$ git checkout -b foo  # or "git switch -c foo"  <co id="git-checkout_CO1-1"/>
$ git branch foo                                 <co id="git-checkout_CO1-2"/>
$ git tag foo                                    <co id="git-checkout_CO1-3"/></screen>
<calloutlist>
<callout arearefs="git-checkout_CO1-1">
<para>
creates a new branch <emphasis>foo</emphasis>, which refers to commit <emphasis>f</emphasis>, and then
    updates <emphasis>HEAD</emphasis> to refer to branch <emphasis>foo</emphasis>. In other words, we'll no longer
    be in detached <emphasis>HEAD</emphasis> state after this command.
</para>
</callout>
<callout arearefs="git-checkout_CO1-2">
<para>
similarly creates a new branch <emphasis>foo</emphasis>, which refers to commit <emphasis>f</emphasis>,
    but leaves <emphasis>HEAD</emphasis> detached.
</para>
</callout>
<callout arearefs="git-checkout_CO1-3">
<para>
creates a new tag <emphasis>foo</emphasis>, which refers to commit <emphasis>f</emphasis>,
    leaving <emphasis>HEAD</emphasis> detached.
</para>
</callout>
</calloutlist>
<simpara>If we have moved away from commit <emphasis>f</emphasis>, 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 <emphasis>HEAD</emphasis> referred, we
can use either of these commands:</simpara>
<screen>$ git reflog -2 HEAD # or
$ git log -g -2 HEAD</screen>
</simplesect>
<simplesect id="git-checkout(1)__argument_disambiguation">
<title>ARGUMENT DISAMBIGUATION</title>
<simpara>When there is only one argument given and it is not <emphasis>--</emphasis> (e.g. <emphasis>git
checkout abc</emphasis>), and when the argument is both a valid <emphasis>&lt;tree-ish&gt;</emphasis>
(e.g. a branch <emphasis>abc</emphasis> exists) and a valid <emphasis>&lt;pathspec&gt;</emphasis> (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, <emphasis>git checkout abc</emphasis> takes "abc" as a <emphasis>&lt;tree-ish&gt;</emphasis>
in such a situation.  Use <emphasis>git checkout -- &lt;pathspec&gt;</emphasis> if you want
to checkout these paths out of the index.</simpara>
</simplesect>
<simplesect id="git-checkout(1)__examples">
<title>EXAMPLES</title>
<section id="git-checkout(1)__1_paths">
<title>1. Paths</title>
<simpara>The following sequence checks out the <emphasis>master</emphasis> branch, reverts
the <emphasis>Makefile</emphasis> to two revisions back, deletes <emphasis>hello.c</emphasis> by
mistake, and gets it back from the index.</simpara>
<screen>$ git checkout master             <co id="git-checkout_CO2-1"/>
$ git checkout master~2 Makefile  <co id="git-checkout_CO2-2"/>
$ rm -f hello.c
$ git checkout hello.c            <co id="git-checkout_CO2-3"/></screen>
<calloutlist>
<callout arearefs="git-checkout_CO2-1">
<para>
switch branch
</para>
</callout>
<callout arearefs="git-checkout_CO2-2">
<para>
take a file out of another commit
</para>
</callout>
<callout arearefs="git-checkout_CO2-3">
<para>
restore <emphasis>hello.c</emphasis> from the index
</para>
</callout>
</calloutlist>
<simpara>If you want to check out <emphasis>all</emphasis> C source files out of the index,
you can say</simpara>
<screen>$ git checkout -- '*.c'</screen>
<simpara>Note the quotes around <emphasis>*.c</emphasis>.  The file <emphasis>hello.c</emphasis> 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).</simpara>
<simpara>If you have an unfortunate branch that is named <emphasis>hello.c</emphasis>, this
step would be confused as an instruction to switch to that branch.
You should instead write:</simpara>
<screen>$ git checkout -- hello.c</screen>
</section>
<section id="git-checkout(1)__2_merge">
<title>2. Merge</title>
<simpara>After working in the wrong branch, switching to the correct
branch would be done using:</simpara>
<screen>$ git checkout mytopic</screen>
<simpara>However, your "wrong" branch and correct <emphasis>mytopic</emphasis> branch may
differ in files that you have modified locally, in which case
the above checkout would fail like this:</simpara>
<screen>$ git checkout mytopic
error: You have local changes to 'frotz'; not switching branches.</screen>
<simpara>You can give the <emphasis>-m</emphasis> flag to the command, which would try a
three-way merge:</simpara>
<screen>$ git checkout -m mytopic
Auto-merging frotz</screen>
<simpara>After this three-way merge, the local modifications are <emphasis>not</emphasis>
registered in your index file, so <emphasis>git diff</emphasis> would show you what
changes you made since the tip of the new branch.</simpara>
</section>
<section id="git-checkout(1)__3_merge_conflict">
<title>3. Merge conflict</title>
<simpara>When a merge conflict happens during switching branches with
the <emphasis>-m</emphasis> option, you would see something like this:</simpara>
<screen>$ git checkout -m mytopic
Auto-merging frotz
ERROR: Merge conflict in frotz
fatal: merge program failed</screen>
<simpara>At this point, <emphasis>git diff</emphasis> 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
<emphasis>git add</emphasis> as usual:</simpara>
<screen>$ edit frotz
$ git add frotz</screen>
</section>
</simplesect>
<simplesect id="git-checkout(1)__configuration">
<title>CONFIGURATION</title>
<simpara>Everything below this line in this section is selectively included
from the <xref linkend="git-config(1)" /> documentation. The content is the same
as what's found there:</simpara>
<variablelist>
<varlistentry>
<term>
checkout.defaultRemote
</term>
<listitem>
<simpara>
        When you run <emphasis>git checkout &lt;something&gt;</emphasis>
        or <emphasis>git switch &lt;something&gt;</emphasis> and only have one
        remote, it may implicitly fall back on checking out and
        tracking e.g. <emphasis>origin/&lt;something&gt;</emphasis>. This stops working as soon
        as you have more than one remote with a <emphasis>&lt;something&gt;</emphasis>
        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
        <emphasis>origin</emphasis>.
</simpara>
<simpara>Currently this is used by <xref linkend="git-switch(1)" /> and
<xref linkend="git-checkout(1)" /> when <emphasis>git checkout &lt;something&gt;</emphasis>
or <emphasis>git switch &lt;something&gt;</emphasis>
will checkout the <emphasis>&lt;something&gt;</emphasis> branch on another remote,
and by <xref linkend="git-worktree(1)" /> when <emphasis>git worktree add</emphasis> refers to a
remote branch. This setting might be used for other checkout-like
commands or functionality in the future.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
checkout.guess
</term>
<listitem>
<simpara>
        Provides the default value for the <emphasis>--guess</emphasis> or <emphasis>--no-guess</emphasis>
        option in <emphasis>git checkout</emphasis> and <emphasis>git switch</emphasis>. See
        <xref linkend="git-switch(1)" /> and <xref linkend="git-checkout(1)" />.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
checkout.workers
</term>
<listitem>
<simpara>
        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 <emphasis>checkout.thresholdForParallelism</emphasis> affect
        all commands that perform checkout. E.g. checkout, clone, reset,
        sparse-checkout, etc.
</simpara>
<simpara>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.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
checkout.thresholdForParallelism
</term>
<listitem>
<simpara>
        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.
</simpara>
</listitem>
</varlistentry>
</variablelist>
</simplesect>
<simplesect id="git-checkout(1)__see_also">
<title>SEE ALSO</title>
<simpara><xref linkend="git-switch(1)" />,
<xref linkend="git-restore(1)" /></simpara>
</simplesect>
<simplesect id="git-checkout(1)__git">
<title>GIT</title>
<simpara>Part of the <xref linkend="git(1)" /> suite</simpara>
</simplesect>
</sect2>
