OSDN Git Service

Add Git official document to help
[tortoisegit/TortoiseGitJp.git] / doc / source / en / TortoiseGit / git_doc / git-stash.html.xml
1 <?xml version="1.0" encoding="UTF-8"?>\r
2 <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">\r
3 \r
4 <article lang="en" id="git-stash(1)">\r
5 <articleinfo>\r
6     <title>git-stash(1)</title>\r
7         <indexterm>\r
8                 <primary>git-stash(1)</primary>\r
9         </indexterm>\r
10 </articleinfo>\r
11 <simplesect id="_name">\r
12 <title>NAME</title>\r
13 <simpara>git-stash - Stash the changes in a dirty working directory away</simpara>\r
14 </simplesect>\r
15 <simplesect id="_synopsis">\r
16 <title>SYNOPSIS</title>\r
17 <blockquote>\r
18 <literallayout><emphasis>git stash</emphasis> list [&lt;options&gt;]\r
19 <emphasis>git stash</emphasis> (show | drop | pop ) [&lt;stash&gt;]\r
20 <emphasis>git stash</emphasis> apply [--index] [&lt;stash&gt;]\r
21 <emphasis>git stash</emphasis> branch &lt;branchname&gt; [&lt;stash&gt;]\r
22 <emphasis>git stash</emphasis> [save [--keep-index] [&lt;message&gt;]]\r
23 <emphasis>git stash</emphasis> clear\r
24 <emphasis>git stash</emphasis> create</literallayout>\r
25 </blockquote>\r
26 </simplesect>\r
27 <simplesect id="_description">\r
28 <title>DESCRIPTION</title>\r
29 <simpara>Use <emphasis>git stash</emphasis> when you want to record the current state of the\r
30 working directory and the index, but want to go back to a clean\r
31 working directory.  The command saves your local modifications away\r
32 and reverts the working directory to match the <literal>HEAD</literal> commit.</simpara>\r
33 <simpara>The modifications stashed away by this command can be listed with\r
34 <literal>git stash list</literal>, inspected with <literal>git stash show</literal>, and restored\r
35 (potentially on top of a different commit) with <literal>git stash apply</literal>.\r
36 Calling <literal>git stash</literal> without any arguments is equivalent to <literal>git stash save</literal>.\r
37 A stash is by default listed as "WIP on <emphasis>branchname</emphasis> &#8230;", but\r
38 you can give a more descriptive message on the command line when\r
39 you create one.</simpara>\r
40 <simpara>The latest stash you created is stored in <literal>$GIT_DIR/refs/stash</literal>; older\r
41 stashes are found in the reflog of this reference and can be named using\r
42 the usual reflog syntax (e.g. <literal>stash@{0}</literal> is the most recently\r
43 created stash, <literal>stash@{1}</literal> is the one before it, <literal>stash@{2.hours.ago}</literal>\r
44 is also possible).</simpara>\r
45 </simplesect>\r
46 <simplesect id="_options">\r
47 <title>OPTIONS</title>\r
48 <variablelist>\r
49 <varlistentry>\r
50 <term>\r
51 save [--keep-index] [&lt;message&gt;]\r
52 </term>\r
53 <listitem>\r
54 <simpara>\r
55         Save your local modifications to a new <emphasis>stash</emphasis>, and run <literal>git reset\r
56         --hard</literal> to revert them.  This is the default action when no\r
57         subcommand is given. The &lt;message&gt; part is optional and gives\r
58         the description along with the stashed state.\r
59 </simpara>\r
60 <simpara>If the <literal>--keep-index</literal> option is used, all changes already added to the\r
61 index are left intact.</simpara>\r
62 </listitem>\r
63 </varlistentry>\r
64 <varlistentry>\r
65 <term>\r
66 list [&lt;options&gt;]\r
67 </term>\r
68 <listitem>\r
69 <simpara>\r
70         List the stashes that you currently have.  Each <emphasis>stash</emphasis> is listed\r
71         with its name (e.g. <literal>stash@{0}</literal> is the latest stash, <literal>stash@{1}</literal> is\r
72         the one before, etc.), the name of the branch that was current when the\r
73         stash was made, and a short description of the commit the stash was\r
74         based on.\r
75 </simpara>\r
76 <literallayout>stash@{0}: WIP on submit: 6ebd0e2... Update git-stash documentation\r
77 stash@{1}: On master: 9cc0589... Add git-stash</literallayout>\r
78 <simpara>The command takes options applicable to the <emphasis>git-log</emphasis>\r
79 command to control what is shown and how. See <xref linkend="git-log(1)"/>.</simpara>\r
80 </listitem>\r
81 </varlistentry>\r
82 <varlistentry>\r
83 <term>\r
84 show [&lt;stash&gt;]\r
85 </term>\r
86 <listitem>\r
87 <simpara>\r
88         Show the changes recorded in the stash as a diff between the\r
89         stashed state and its original parent. When no <literal>&lt;stash&gt;</literal> is given,\r
90         shows the latest one. By default, the command shows the diffstat, but\r
91         it will accept any format known to <emphasis>git-diff</emphasis> (e.g., <literal>git stash show\r
92         -p stash@{1}</literal> to view the second most recent stash in patch form).\r
93 </simpara>\r
94 </listitem>\r
95 </varlistentry>\r
96 <varlistentry>\r
97 <term>\r
98 apply [--index] [&lt;stash&gt;]\r
99 </term>\r
100 <listitem>\r
101 <simpara>\r
102         Restore the changes recorded in the stash on top of the current\r
103         working tree state.  When no <literal>&lt;stash&gt;</literal> is given, applies the latest\r
104         one.  The working directory must match the index.\r
105 </simpara>\r
106 <simpara>This operation can fail with conflicts; you need to resolve them\r
107 by hand in the working tree.</simpara>\r
108 <simpara>If the <literal>--index</literal> option is used, then tries to reinstate not only the working\r
109 tree&#8217;s changes, but also the index&#8217;s ones. However, this can fail, when you\r
110 have conflicts (which are stored in the index, where you therefore can no\r
111 longer apply the changes as they were originally).</simpara>\r
112 </listitem>\r
113 </varlistentry>\r
114 <varlistentry>\r
115 <term>\r
116 branch &lt;branchname&gt; [&lt;stash&gt;]\r
117 </term>\r
118 <listitem>\r
119 <simpara>\r
120         Creates and checks out a new branch named <literal>&lt;branchname&gt;</literal> starting from\r
121         the commit at which the <literal>&lt;stash&gt;</literal> was originally created, applies the\r
122         changes recorded in <literal>&lt;stash&gt;</literal> to the new working tree and index, then\r
123         drops the <literal>&lt;stash&gt;</literal> if that completes successfully. When no <literal>&lt;stash&gt;</literal>\r
124         is given, applies the latest one.\r
125 </simpara>\r
126 <simpara>This is useful if the branch on which you ran <literal>git stash save</literal> has\r
127 changed enough that <literal>git stash apply</literal> fails due to conflicts. Since\r
128 the stash is applied on top of the commit that was HEAD at the time\r
129 <literal>git stash</literal> was run, it restores the originally stashed state with\r
130 no conflicts.</simpara>\r
131 </listitem>\r
132 </varlistentry>\r
133 <varlistentry>\r
134 <term>\r
135 clear\r
136 </term>\r
137 <listitem>\r
138 <simpara>\r
139         Remove all the stashed states. Note that those states will then\r
140         be subject to pruning, and may be difficult or impossible to recover.\r
141 </simpara>\r
142 </listitem>\r
143 </varlistentry>\r
144 <varlistentry>\r
145 <term>\r
146 drop [&lt;stash&gt;]\r
147 </term>\r
148 <listitem>\r
149 <simpara>\r
150         Remove a single stashed state from the stash list. When no <literal>&lt;stash&gt;</literal>\r
151         is given, it removes the latest one. i.e. <literal>stash@{0}</literal>\r
152 </simpara>\r
153 </listitem>\r
154 </varlistentry>\r
155 <varlistentry>\r
156 <term>\r
157 pop [&lt;stash&gt;]\r
158 </term>\r
159 <listitem>\r
160 <simpara>\r
161         Remove a single stashed state from the stash list and apply on top\r
162         of the current working tree state. When no <literal>&lt;stash&gt;</literal> is given,\r
163         <literal>stash@{0}</literal> is assumed. See also <literal>apply</literal>.\r
164 </simpara>\r
165 </listitem>\r
166 </varlistentry>\r
167 <varlistentry>\r
168 <term>\r
169 create\r
170 </term>\r
171 <listitem>\r
172 <simpara>\r
173         Create a stash (which is a regular commit object) and return its\r
174         object name, without storing it anywhere in the ref namespace.\r
175 </simpara>\r
176 </listitem>\r
177 </varlistentry>\r
178 </variablelist>\r
179 </simplesect>\r
180 <simplesect id="_discussion">\r
181 <title>DISCUSSION</title>\r
182 <simpara>A stash is represented as a commit whose tree records the state of the\r
183 working directory, and its first parent is the commit at <literal>HEAD</literal> when\r
184 the stash was created.  The tree of the second parent records the\r
185 state of the index when the stash is made, and it is made a child of\r
186 the <literal>HEAD</literal> commit.  The ancestry graph looks like this:</simpara>\r
187 <literallayout class="monospaced">       .----W\r
188       /    /\r
189 -----H----I</literallayout>\r
190 <simpara>where <literal>H</literal> is the <literal>HEAD</literal> commit, <literal>I</literal> is a commit that records the state\r
191 of the index, and <literal>W</literal> is a commit that records the state of the working\r
192 tree.</simpara>\r
193 </simplesect>\r
194 <simplesect id="_examples">\r
195 <title>EXAMPLES</title>\r
196 <variablelist>\r
197 <varlistentry>\r
198 <term>\r
199 Pulling into a dirty tree\r
200 </term>\r
201 <listitem>\r
202 <simpara>\r
203 When you are in the middle of something, you learn that there are\r
204 upstream changes that are possibly relevant to what you are\r
205 doing.  When your local changes do not conflict with the changes in\r
206 the upstream, a simple <literal>git pull</literal> will let you move forward.\r
207 </simpara>\r
208 <simpara>However, there are cases in which your local changes do conflict with\r
209 the upstream changes, and <literal>git pull</literal> refuses to overwrite your\r
210 changes.  In such a case, you can stash your changes away,\r
211 perform a pull, and then unstash, like this:</simpara>\r
212 <literallayout>$ git pull\r
213  ...\r
214 file foobar not up to date, cannot merge.\r
215 $ git stash\r
216 $ git pull\r
217 $ git stash apply</literallayout>\r
218 </listitem>\r
219 </varlistentry>\r
220 <varlistentry>\r
221 <term>\r
222 Interrupted workflow\r
223 </term>\r
224 <listitem>\r
225 <simpara>\r
226 When you are in the middle of something, your boss comes in and\r
227 demands that you fix something immediately.  Traditionally, you would\r
228 make a commit to a temporary branch to store your changes away, and\r
229 return to your original branch to make the emergency fix, like this:\r
230 </simpara>\r
231 <literallayout># ... hack hack hack ...\r
232 $ git checkout -b my_wip\r
233 $ git commit -a -m "WIP"\r
234 $ git checkout master\r
235 $ edit emergency fix\r
236 $ git commit -a -m "Fix in a hurry"\r
237 $ git checkout my_wip\r
238 $ git reset --soft HEAD^\r
239 # ... continue hacking ...</literallayout>\r
240 <simpara>You can use <emphasis>git-stash</emphasis> to simplify the above, like this:</simpara>\r
241 <literallayout># ... hack hack hack ...\r
242 $ git stash\r
243 $ edit emergency fix\r
244 $ git commit -a -m "Fix in a hurry"\r
245 $ git stash apply\r
246 # ... continue hacking ...</literallayout>\r
247 </listitem>\r
248 </varlistentry>\r
249 <varlistentry>\r
250 <term>\r
251 Testing partial commits\r
252 </term>\r
253 <listitem>\r
254 <simpara>\r
255 You can use <literal>git stash save --keep-index</literal> when you want to make two or\r
256 more commits out of the changes in the work tree, and you want to test\r
257 each change before committing:\r
258 </simpara>\r
259 <literallayout># ... hack hack hack ...\r
260 $ git add --patch foo            # add just first part to the index\r
261 $ git stash save --keep-index    # save all other changes to the stash\r
262 $ edit/build/test first part\r
263 $ git commit -m 'First part'     # commit fully tested change\r
264 $ git stash pop                  # prepare to work on all other changes\r
265 # ... repeat above five steps until one commit remains ...\r
266 $ edit/build/test remaining parts\r
267 $ git commit foo -m 'Remaining parts'</literallayout>\r
268 </listitem>\r
269 </varlistentry>\r
270 </variablelist>\r
271 </simplesect>\r
272 <simplesect id="_see_also">\r
273 <title>SEE ALSO</title>\r
274 <simpara><xref linkend="git-checkout(1)"/>,\r
275 <xref linkend="git-commit(1)"/>,\r
276 <xref linkend="git-reflog(1)"/>,\r
277 <xref linkend="git-reset(1)"/></simpara>\r
278 </simplesect>\r
279 <simplesect id="_author">\r
280 <title>AUTHOR</title>\r
281 <simpara>Written by Nanako Shiraishi &lt;<ulink url="mailto:nanako3@bluebottle.com">nanako3@bluebottle.com</ulink>&gt;</simpara>\r
282 </simplesect>\r
283 <simplesect id="_git">\r
284 <title>GIT</title>\r
285 <simpara>Part of the <xref linkend="git(1)"/> suite</simpara>\r
286 </simplesect>\r
287 </article>\r