<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">

<article lang="en" id="git-checkout(1)">
<articleinfo>
    <title>git-checkout(1)</title>
        <indexterm>
                <primary>git-checkout(1)</primary>
        </indexterm>
</articleinfo>
<simplesect id="_name">
<title>NAME</title>
<simpara>git-checkout - Checkout a branch or paths to the working tree</simpara>
</simplesect>
<simplesect id="_synopsis">
<title>SYNOPSIS</title>
<blockquote>
<literallayout><emphasis>git checkout</emphasis> [-q] [-f] [--track | --no-track] [-b &lt;new_branch&gt; [-l]] [-m] [&lt;branch&gt;]
<emphasis>git checkout</emphasis> [-f|--ours|--theirs|-m|--conflict=&lt;style&gt;] [&lt;tree-ish&gt;] [--] &lt;paths&gt;&#8230;</literallayout>
</blockquote>
</simplesect>
<simplesect id="_description">
<title>DESCRIPTION</title>
<simpara>When &lt;paths&gt; are not given, this command switches branches by
updating the index and working tree to reflect the specified
branch, &lt;branch&gt;, and updating HEAD to be &lt;branch&gt; or, if
specified, &lt;new_branch&gt;.  Using -b will cause &lt;new_branch&gt; to
be created; in this case you can use the --track or --no-track
options, which will be passed to <literal>git branch</literal>.</simpara>
<simpara>As a convenience, --track will default to create a branch whose
name is constructed from the specified branch name by stripping
the first namespace level.</simpara>
<simpara>When &lt;paths&gt; are given, this command does <emphasis role="strong">not</emphasis> switch
branches.  It updates the named paths in the working tree from
the index file, or from a named &lt;tree-ish&gt; (most often a commit).  In
this case, the <literal>-b</literal> options is meaningless and giving
either of them results in an error.  &lt;tree-ish&gt; argument can be
used to specify a specific tree-ish (i.e. commit, tag or tree)
to update the index for the given paths before updating the
working tree.</simpara>
<simpara>The index may contain unmerged entries after a 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 -f will ignore these unmerged entries.  The contents from a
specific side of the merge can be checked out of the index by
using --ours or --theirs.  With -m, changes made to the working tree
file can be discarded to recreate the original conflicted merge result.</simpara>
</simplesect>
<simplesect id="_options">
<title>OPTIONS</title>
<variablelist>
<varlistentry>
<term>
-q
</term>
<listitem>
<simpara>
        Quiet, suppress feedback messages.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-f
</term>
<listitem>
<simpara>
        When switching branches, proceed even if the index or the
        working tree differs from HEAD.  This is used to throw away
        local changes.
</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>
</listitem>
</varlistentry>
<varlistentry>
<term>
-b
</term>
<listitem>
<simpara>
        Create a new branch named &lt;new_branch&gt; and start it at
        &lt;branch&gt;.  The new branch name must pass all checks defined
        by <xref linkend="git-check-ref-format(1)"/>.  Some of these checks
        may restrict the characters allowed in a branch name.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-t
</term>
<term>
--track
</term>
<listitem>
<simpara>
        When creating a new branch, set up configuration so that <emphasis>git-pull</emphasis>
        will automatically retrieve data from the start point, which must be
        a branch. Use this if you always pull from the same upstream branch
        into the new branch, and if you don&#8217;t want to use "git pull
        &lt;repository&gt; &lt;refspec&gt;" explicitly. This behavior is the default
        when the start point is a remote branch. Set the
        branch.autosetupmerge configuration variable to <literal>false</literal> if you want
        <emphasis>git-checkout</emphasis> and <emphasis>git-branch</emphasis> to always behave as if <emphasis>--no-track</emphasis> were
        given. Set it to <literal>always</literal> if you want this behavior when the
        start-point is either a local or remote branch.
</simpara>
<simpara>If no <emphasis>-b</emphasis> option was given, the name of the new branch will be
derived from the remote branch, by attempting to guess the name
of the branch on remote system.  If "remotes/" or "refs/remotes/"
are prefixed, it is stripped away, and then the part up to the
next slash (which would be the nickname of the remote) is removed.
This would tell us to use "hack" as the local branch when branching
off of "origin/hack" (or "remotes/origin/hack", or even
"refs/remotes/origin/hack").  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>
        Ignore the branch.autosetupmerge configuration variable.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-l
</term>
<listitem>
<simpara>
        Create the new branch&#8217;s reflog.  This activates recording of
        all changes made to the branch ref, enabling use of date
        based sha1 expressions such as "&lt;branchname&gt;@{yesterday}".
</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 <literal>git add</literal> (or <literal>git rm</literal> 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.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--conflict=&lt;style&gt;
</term>
<listitem>
<simpara>
        The same as --merge option above, but changes the way the
        conflicting hunks are presented, overriding the
        merge.conflictstyle configuration variable.  Possible values are
        "merge" (default) and "diff3" (in addition to what is shown by
        "merge" style, shows the original contents).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
&lt;new_branch&gt;
</term>
<listitem>
<simpara>
        Name for the new branch.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
&lt;branch&gt;
</term>
<listitem>
<simpara>
        Branch to checkout; may be any object ID that resolves to a
        commit.  Defaults to HEAD.
</simpara>
<simpara>When this parameter names a non-branch (but still a valid commit object),
your HEAD becomes <emphasis>detached</emphasis>.</simpara>
<simpara>As a special case, the "<literal>@{-N}</literal>" syntax for the N-th last branch
checks out the branch (instead of detaching).  You may also specify
"<literal>-</literal>" which is synonymous with "<literal>@{-1}</literal>".</simpara>
</listitem>
</varlistentry>
</variablelist>
</simplesect>
<simplesect id="_detached_head">
<title>Detached HEAD</title>
<simpara>It is sometimes useful to be able to <emphasis>checkout</emphasis> a commit that is
not at the tip of one of your branches.  The most obvious
example is to check out the commit at a tagged official release
point, like this:</simpara>
<literallayout>$ git checkout v2.6.18</literallayout>
<simpara>Earlier versions of git did not allow this and asked you to
create a temporary branch using <literal>-b</literal> option, but starting from
version 1.5.0, the above command <emphasis>detaches</emphasis> your HEAD from the
current branch and directly point at the commit named by the tag
(<literal>v2.6.18</literal> in the above example).</simpara>
<simpara>You can use usual git commands while in this state.  You can use
<literal>git reset --hard $othercommit</literal> to further move around, for
example.  You can make changes and create a new commit on top of
a detached HEAD.  You can even create a merge by using <literal>git
merge $othercommit</literal>.</simpara>
<simpara>The state you are in while your HEAD is detached is not recorded
by any branch (which is natural --- you are not on any branch).
What this means is that you can discard your temporary commits
and merges by switching back to an existing branch (e.g. <literal>git
checkout master</literal>), and a later <literal>git prune</literal> or <literal>git gc</literal> would
garbage-collect them.  If you did this by mistake, you can ask
the reflog for HEAD where you were, e.g.</simpara>
<literallayout>$ git log -g -2 HEAD</literallayout>
</simplesect>
<simplesect id="_examples">
<title>EXAMPLES</title>
<orderedlist numeration="arabic">
<listitem>
<simpara>
The following sequence checks out the <literal>master</literal> branch, reverts
the <literal>Makefile</literal> to two revisions back, deletes hello.c by
mistake, and gets it back from the index.
</simpara>
<literallayout>$ git checkout master             <co id="CO1-1"/>
$ git checkout master~2 Makefile  <co id="CO1-2"/>
$ rm -f hello.c
$ git checkout hello.c            <co id="CO1-3"/></literallayout>
<calloutlist>
<callout arearefs="CO1-1">
<simpara>
switch branch
</simpara>
</callout>
<callout arearefs="CO1-2">
<simpara>
take out a file out of other commit
</simpara>
</callout>
<callout arearefs="CO1-3">
<simpara>
restore hello.c from HEAD of current branch
</simpara>
<simpara>If you have an unfortunate branch that is named <literal>hello.c</literal>, this
step would be confused as an instruction to switch to that branch.
You should instead write:</simpara>
<literallayout>$ git checkout -- hello.c</literallayout>
</callout>
</calloutlist>
</listitem>
<listitem>
<simpara>
After working in a wrong branch, switching to the correct
branch would be done using:
</simpara>
<literallayout>$ git checkout mytopic</literallayout>
<simpara>However, your "wrong" branch and correct "mytopic" branch may
differ in files that you have locally modified, in which case,
the above checkout would fail like this:</simpara>
<literallayout>$ git checkout mytopic
fatal: Entry 'frotz' not uptodate. Cannot merge.</literallayout>
<simpara>You can give the <literal>-m</literal> flag to the command, which would try a
three-way merge:</simpara>
<literallayout>$ git checkout -m mytopic
Auto-merging frotz</literallayout>
<simpara>After this three-way merge, the local modifications are <emphasis>not</emphasis>
registered in your index file, so <literal>git diff</literal> would show you what
changes you made since the tip of the new branch.</simpara>
</listitem>
<listitem>
<simpara>
When a merge conflict happens during switching branches with
the <literal>-m</literal> option, you would see something like this:
</simpara>
<literallayout>$ git checkout -m mytopic
Auto-merging frotz
ERROR: Merge conflict in frotz
fatal: merge program failed</literallayout>
<simpara>At this point, <literal>git diff</literal> 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
<literal>git add</literal> as usual:</simpara>
<literallayout>$ edit frotz
$ git add frotz</literallayout>
</listitem>
</orderedlist>
</simplesect>
<simplesect id="_author">
<title>Author</title>
<simpara>Written by Linus Torvalds &lt;<ulink url="mailto:torvalds@osdl.org">torvalds@osdl.org</ulink>&gt;</simpara>
</simplesect>
<simplesect id="_documentation">
<title>Documentation</title>
<simpara>Documentation by Junio C Hamano and the git-list &lt;<ulink url="mailto:git@vger.kernel.org">git@vger.kernel.org</ulink>&gt;.</simpara>
</simplesect>
<simplesect id="_git">
<title>GIT</title>
<simpara>Part of the <xref linkend="git(1)"/> suite</simpara>
</simplesect>
</article>
