<?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-stash(1)">
<articleinfo>
    <title>git-stash(1)</title>
        <indexterm>
                <primary>git-stash(1)</primary>
        </indexterm>
</articleinfo>
<simplesect id="_name">
<title>NAME</title>
<simpara>git-stash - Stash the changes in a dirty working directory away</simpara>
</simplesect>
<simplesect id="_synopsis">
<title>SYNOPSIS</title>
<blockquote>
<literallayout><emphasis>git stash</emphasis> list [&lt;options&gt;]
<emphasis>git stash</emphasis> (show | drop | pop ) [&lt;stash&gt;]
<emphasis>git stash</emphasis> apply [--index] [&lt;stash&gt;]
<emphasis>git stash</emphasis> branch &lt;branchname&gt; [&lt;stash&gt;]
<emphasis>git stash</emphasis> [save [--keep-index] [&lt;message&gt;]]
<emphasis>git stash</emphasis> clear
<emphasis>git stash</emphasis> create</literallayout>
</blockquote>
</simplesect>
<simplesect id="_description">
<title>DESCRIPTION</title>
<simpara>Use <emphasis>git stash</emphasis> when you want to record the current state of the
working directory and the index, but want to go back to a clean
working directory.  The command saves your local modifications away
and reverts the working directory to match the <literal>HEAD</literal> commit.</simpara>
<simpara>The modifications stashed away by this command can be listed with
<literal>git stash list</literal>, inspected with <literal>git stash show</literal>, and restored
(potentially on top of a different commit) with <literal>git stash apply</literal>.
Calling <literal>git stash</literal> without any arguments is equivalent to <literal>git stash save</literal>.
A stash is by default listed as "WIP on <emphasis>branchname</emphasis> &#8230;", but
you can give a more descriptive message on the command line when
you create one.</simpara>
<simpara>The latest stash you created is stored in <literal>$GIT_DIR/refs/stash</literal>; older
stashes are found in the reflog of this reference and can be named using
the usual reflog syntax (e.g. <literal>stash@{0}</literal> is the most recently
created stash, <literal>stash@{1}</literal> is the one before it, <literal>stash@{2.hours.ago}</literal>
is also possible).</simpara>
</simplesect>
<simplesect id="_options">
<title>OPTIONS</title>
<variablelist>
<varlistentry>
<term>
save [--keep-index] [&lt;message&gt;]
</term>
<listitem>
<simpara>
        Save your local modifications to a new <emphasis>stash</emphasis>, and run <literal>git reset
        --hard</literal> to revert them.  This is the default action when no
        subcommand is given. The &lt;message&gt; part is optional and gives
        the description along with the stashed state.
</simpara>
<simpara>If the <literal>--keep-index</literal> option is used, all changes already added to the
index are left intact.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
list [&lt;options&gt;]
</term>
<listitem>
<simpara>
        List the stashes that you currently have.  Each <emphasis>stash</emphasis> is listed
        with its name (e.g. <literal>stash@{0}</literal> is the latest stash, <literal>stash@{1}</literal> is
        the one before, etc.), the name of the branch that was current when the
        stash was made, and a short description of the commit the stash was
        based on.
</simpara>
<literallayout>stash@{0}: WIP on submit: 6ebd0e2... Update git-stash documentation
stash@{1}: On master: 9cc0589... Add git-stash</literallayout>
<simpara>The command takes options applicable to the <emphasis>git-log</emphasis>
command to control what is shown and how. See <xref linkend="git-log(1)"/>.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
show [&lt;stash&gt;]
</term>
<listitem>
<simpara>
        Show the changes recorded in the stash as a diff between the
        stashed state and its original parent. When no <literal>&lt;stash&gt;</literal> is given,
        shows the latest one. By default, the command shows the diffstat, but
        it will accept any format known to <emphasis>git-diff</emphasis> (e.g., <literal>git stash show
        -p stash@{1}</literal> to view the second most recent stash in patch form).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
apply [--index] [&lt;stash&gt;]
</term>
<listitem>
<simpara>
        Restore the changes recorded in the stash on top of the current
        working tree state.  When no <literal>&lt;stash&gt;</literal> is given, applies the latest
        one.  The working directory must match the index.
</simpara>
<simpara>This operation can fail with conflicts; you need to resolve them
by hand in the working tree.</simpara>
<simpara>If the <literal>--index</literal> option is used, then tries to reinstate not only the working
tree&#8217;s changes, but also the index&#8217;s ones. However, this can fail, when you
have conflicts (which are stored in the index, where you therefore can no
longer apply the changes as they were originally).</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
branch &lt;branchname&gt; [&lt;stash&gt;]
</term>
<listitem>
<simpara>
        Creates and checks out a new branch named <literal>&lt;branchname&gt;</literal> starting from
        the commit at which the <literal>&lt;stash&gt;</literal> was originally created, applies the
        changes recorded in <literal>&lt;stash&gt;</literal> to the new working tree and index, then
        drops the <literal>&lt;stash&gt;</literal> if that completes successfully. When no <literal>&lt;stash&gt;</literal>
        is given, applies the latest one.
</simpara>
<simpara>This is useful if the branch on which you ran <literal>git stash save</literal> has
changed enough that <literal>git stash apply</literal> fails due to conflicts. Since
the stash is applied on top of the commit that was HEAD at the time
<literal>git stash</literal> was run, it restores the originally stashed state with
no conflicts.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
clear
</term>
<listitem>
<simpara>
        Remove all the stashed states. Note that those states will then
        be subject to pruning, and may be difficult or impossible to recover.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
drop [&lt;stash&gt;]
</term>
<listitem>
<simpara>
        Remove a single stashed state from the stash list. When no <literal>&lt;stash&gt;</literal>
        is given, it removes the latest one. i.e. <literal>stash@{0}</literal>
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
pop [&lt;stash&gt;]
</term>
<listitem>
<simpara>
        Remove a single stashed state from the stash list and apply on top
        of the current working tree state. When no <literal>&lt;stash&gt;</literal> is given,
        <literal>stash@{0}</literal> is assumed. See also <literal>apply</literal>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
create
</term>
<listitem>
<simpara>
        Create a stash (which is a regular commit object) and return its
        object name, without storing it anywhere in the ref namespace.
</simpara>
</listitem>
</varlistentry>
</variablelist>
</simplesect>
<simplesect id="_discussion">
<title>DISCUSSION</title>
<simpara>A stash is represented as a commit whose tree records the state of the
working directory, and its first parent is the commit at <literal>HEAD</literal> when
the stash was created.  The tree of the second parent records the
state of the index when the stash is made, and it is made a child of
the <literal>HEAD</literal> commit.  The ancestry graph looks like this:</simpara>
<literallayout class="monospaced">       .----W
      /    /
-----H----I</literallayout>
<simpara>where <literal>H</literal> is the <literal>HEAD</literal> commit, <literal>I</literal> is a commit that records the state
of the index, and <literal>W</literal> is a commit that records the state of the working
tree.</simpara>
</simplesect>
<simplesect id="_examples">
<title>EXAMPLES</title>
<variablelist>
<varlistentry>
<term>
Pulling into a dirty tree
</term>
<listitem>
<simpara>
When you are in the middle of something, you learn that there are
upstream changes that are possibly relevant to what you are
doing.  When your local changes do not conflict with the changes in
the upstream, a simple <literal>git pull</literal> will let you move forward.
</simpara>
<simpara>However, there are cases in which your local changes do conflict with
the upstream changes, and <literal>git pull</literal> refuses to overwrite your
changes.  In such a case, you can stash your changes away,
perform a pull, and then unstash, like this:</simpara>
<literallayout>$ git pull
 ...
file foobar not up to date, cannot merge.
$ git stash
$ git pull
$ git stash apply</literallayout>
</listitem>
</varlistentry>
<varlistentry>
<term>
Interrupted workflow
</term>
<listitem>
<simpara>
When you are in the middle of something, your boss comes in and
demands that you fix something immediately.  Traditionally, you would
make a commit to a temporary branch to store your changes away, and
return to your original branch to make the emergency fix, like this:
</simpara>
<literallayout># ... hack hack hack ...
$ git checkout -b my_wip
$ git commit -a -m "WIP"
$ git checkout master
$ edit emergency fix
$ git commit -a -m "Fix in a hurry"
$ git checkout my_wip
$ git reset --soft HEAD^
# ... continue hacking ...</literallayout>
<simpara>You can use <emphasis>git-stash</emphasis> to simplify the above, like this:</simpara>
<literallayout># ... hack hack hack ...
$ git stash
$ edit emergency fix
$ git commit -a -m "Fix in a hurry"
$ git stash apply
# ... continue hacking ...</literallayout>
</listitem>
</varlistentry>
<varlistentry>
<term>
Testing partial commits
</term>
<listitem>
<simpara>
You can use <literal>git stash save --keep-index</literal> when you want to make two or
more commits out of the changes in the work tree, and you want to test
each change before committing:
</simpara>
<literallayout># ... hack hack hack ...
$ git add --patch foo            # add just first part to the index
$ git stash save --keep-index    # save all other changes to the stash
$ edit/build/test first part
$ git commit -m 'First part'     # commit fully tested change
$ git stash pop                  # prepare to work on all other changes
# ... repeat above five steps until one commit remains ...
$ edit/build/test remaining parts
$ git commit foo -m 'Remaining parts'</literallayout>
</listitem>
</varlistentry>
</variablelist>
</simplesect>
<simplesect id="_see_also">
<title>SEE ALSO</title>
<simpara><xref linkend="git-checkout(1)"/>,
<xref linkend="git-commit(1)"/>,
<xref linkend="git-reflog(1)"/>,
<xref linkend="git-reset(1)"/></simpara>
</simplesect>
<simplesect id="_author">
<title>AUTHOR</title>
<simpara>Written by Nanako Shiraishi &lt;<ulink url="mailto:nanako3@bluebottle.com">nanako3@bluebottle.com</ulink>&gt;</simpara>
</simplesect>
<simplesect id="_git">
<title>GIT</title>
<simpara>Part of the <xref linkend="git(1)"/> suite</simpara>
</simplesect>
</article>
