updated git doc
[TortoiseGit.git] / doc / source / en / TortoiseGit / git_doc / git-stash.xml
blob1f9a2f03e9c454733acdb9ed84393ee32be53c5c
1 <?xml version="1.0" encoding="UTF-8"?>\r
2 <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/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 [&lt;stash&gt;]\r
20 <emphasis>git stash</emphasis> drop [-q|--quiet] [&lt;stash&gt;]\r
21 <emphasis>git stash</emphasis> ( pop | apply ) [--index] [-q|--quiet] [&lt;stash&gt;]\r
22 <emphasis>git stash</emphasis> branch &lt;branchname&gt; [&lt;stash&gt;]\r
23 <emphasis>git stash</emphasis> [save [--patch] [-k|--[no-]keep-index] [-q|--quiet]\r
24              [-u|--include-untracked] [-a|--all] [&lt;message&gt;]]\r
25 <emphasis>git stash</emphasis> clear\r
26 <emphasis>git stash</emphasis> create</literallayout>\r
27 </blockquote>\r
28 </simplesect>\r
29 <simplesect id="_description">\r
30 <title>DESCRIPTION</title>\r
31 <simpara>Use <emphasis>git stash</emphasis> when you want to record the current state of the\r
32 working directory and the index, but want to go back to a clean\r
33 working directory.  The command saves your local modifications away\r
34 and reverts the working directory to match the <emphasis>HEAD</emphasis> commit.</simpara>\r
35 <simpara>The modifications stashed away by this command can be listed with\r
36 <emphasis>git stash list</emphasis>, inspected with <emphasis>git stash show</emphasis>, and restored\r
37 (potentially on top of a different commit) with <emphasis>git stash apply</emphasis>.\r
38 Calling <emphasis>git stash</emphasis> without any arguments is equivalent to <emphasis>git stash save</emphasis>.\r
39 A stash is by default listed as "WIP on <emphasis>branchname</emphasis> &#8230;", but\r
40 you can give a more descriptive message on the command line when\r
41 you create one.</simpara>\r
42 <simpara>The latest stash you created is stored in <emphasis>refs/stash</emphasis>; older\r
43 stashes are found in the reflog of this reference and can be named using\r
44 the usual reflog syntax (e.g. <emphasis>stash@{0}</emphasis> is the most recently\r
45 created stash, <emphasis>stash@{1}</emphasis> is the one before it, <emphasis>stash@{2.hours.ago}</emphasis>\r
46 is also possible).</simpara>\r
47 </simplesect>\r
48 <simplesect id="_options">\r
49 <title>OPTIONS</title>\r
50 <variablelist>\r
51 <varlistentry>\r
52 <term>\r
53 save [-p|--patch] [--[no-]keep-index] [-u|--include-untracked] [-a|--all] [-q|--quiet] [&lt;message&gt;]\r
54 </term>\r
55 <listitem>\r
56 <simpara>\r
57         Save your local modifications to a new <emphasis>stash</emphasis>, and run <emphasis>git reset\r
58         --hard</emphasis> to revert them.  The &lt;message&gt; part is optional and gives\r
59         the description along with the stashed state.  For quickly making\r
60         a snapshot, you can omit <emphasis>both</emphasis> "save" and &lt;message&gt;, but giving\r
61         only &lt;message&gt; does not trigger this action to prevent a misspelled\r
62         subcommand from making an unwanted stash.\r
63 </simpara>\r
64 <simpara>If the <emphasis>--keep-index</emphasis> option is used, all changes already added to the\r
65 index are left intact.</simpara>\r
66 <simpara>If the <emphasis>--include-untracked</emphasis> option is used, all untracked files are also\r
67 stashed and then cleaned up with <emphasis>git clean</emphasis>, leaving the working directory\r
68 in a very clean state. If the <emphasis>--all</emphasis> option is used instead then the\r
69 ignored files are stashed and cleaned in addition to the untracked files.</simpara>\r
70 <simpara>With <emphasis>--patch</emphasis>, you can interactively select hunks from the diff\r
71 between HEAD and the working tree to be stashed.  The stash entry is\r
72 constructed such that its index state is the same as the index state\r
73 of your repository, and its worktree contains only the changes you\r
74 selected interactively.  The selected changes are then rolled back\r
75 from your worktree. See the Interactive Mode section of\r
76 <xref linkend="git-add(1)" /> to learn how to operate the <emphasis>--patch</emphasis> mode.</simpara>\r
77 <simpara>The <emphasis>--patch</emphasis> option implies <emphasis>--keep-index</emphasis>.  You can use\r
78 <emphasis>--no-keep-index</emphasis> to override this.</simpara>\r
79 </listitem>\r
80 </varlistentry>\r
81 <varlistentry>\r
82 <term>\r
83 list [&lt;options&gt;]\r
84 </term>\r
85 <listitem>\r
86 <simpara>\r
87         List the stashes that you currently have.  Each <emphasis>stash</emphasis> is listed\r
88         with its name (e.g. <emphasis>stash@{0}</emphasis> is the latest stash, <emphasis>stash@{1}</emphasis> is\r
89         the one before, etc.), the name of the branch that was current when the\r
90         stash was made, and a short description of the commit the stash was\r
91         based on.\r
92 </simpara>\r
93 <screen>stash@{0}: WIP on submit: 6ebd0e2... Update git-stash documentation\r
94 stash@{1}: On master: 9cc0589... Add git-stash</screen>\r
95 <simpara>The command takes options applicable to the <emphasis>git log</emphasis>\r
96 command to control what is shown and how. See <xref linkend="git-log(1)" />.</simpara>\r
97 </listitem>\r
98 </varlistentry>\r
99 <varlistentry>\r
100 <term>\r
101 show [&lt;stash&gt;]\r
102 </term>\r
103 <listitem>\r
104 <simpara>\r
105         Show the changes recorded in the stash as a diff between the\r
106         stashed state and its original parent. When no <emphasis>&lt;stash&gt;</emphasis> is given,\r
107         shows the latest one. By default, the command shows the diffstat, but\r
108         it will accept any format known to <emphasis>git diff</emphasis> (e.g., <emphasis>git stash show\r
109         -p stash@{1}</emphasis> to view the second most recent stash in patch form).\r
110 </simpara>\r
111 </listitem>\r
112 </varlistentry>\r
113 <varlistentry>\r
114 <term>\r
115 pop [--index] [-q|--quiet] [&lt;stash&gt;]\r
116 </term>\r
117 <listitem>\r
118 <simpara>\r
119         Remove a single stashed state from the stash list and apply it\r
120         on top of the current working tree state, i.e., do the inverse\r
121         operation of <emphasis>git stash save</emphasis>. The working directory must\r
122         match the index.\r
123 </simpara>\r
124 <simpara>Applying the state can fail with conflicts; in this case, it is not\r
125 removed from the stash list. You need to resolve the conflicts by hand\r
126 and call <emphasis>git stash drop</emphasis> manually afterwards.</simpara>\r
127 <simpara>If the <emphasis>--index</emphasis> option is used, then tries to reinstate not only the working\r
128 tree's changes, but also the index's ones. However, this can fail, when you\r
129 have conflicts (which are stored in the index, where you therefore can no\r
130 longer apply the changes as they were originally).</simpara>\r
131 <simpara>When no <emphasis>&lt;stash&gt;</emphasis> is given, <emphasis>stash@{0}</emphasis> is assumed, otherwise <emphasis>&lt;stash&gt;</emphasis> must\r
132 be a reference of the form <emphasis>stash@{&lt;revision&gt;}</emphasis>.</simpara>\r
133 </listitem>\r
134 </varlistentry>\r
135 <varlistentry>\r
136 <term>\r
137 apply [--index] [-q|--quiet] [&lt;stash&gt;]\r
138 </term>\r
139 <listitem>\r
140 <simpara>\r
141         Like <emphasis>pop</emphasis>, but do not remove the state from the stash list. Unlike <emphasis>pop</emphasis>,\r
142         <emphasis>&lt;stash&gt;</emphasis> may be any commit that looks like a commit created by\r
143         <emphasis>stash save</emphasis> or <emphasis>stash create</emphasis>.\r
144 </simpara>\r
145 </listitem>\r
146 </varlistentry>\r
147 <varlistentry>\r
148 <term>\r
149 branch &lt;branchname&gt; [&lt;stash&gt;]\r
150 </term>\r
151 <listitem>\r
152 <simpara>\r
153         Creates and checks out a new branch named <emphasis>&lt;branchname&gt;</emphasis> starting from\r
154         the commit at which the <emphasis>&lt;stash&gt;</emphasis> was originally created, applies the\r
155         changes recorded in <emphasis>&lt;stash&gt;</emphasis> to the new working tree and index.\r
156         If that succeeds, and <emphasis>&lt;stash&gt;</emphasis> is a reference of the form\r
157         <emphasis>stash@{&lt;revision&gt;}</emphasis>, it then drops the <emphasis>&lt;stash&gt;</emphasis>. When no <emphasis>&lt;stash&gt;</emphasis>\r
158         is given, applies the latest one.\r
159 </simpara>\r
160 <simpara>This is useful if the branch on which you ran <emphasis>git stash save</emphasis> has\r
161 changed enough that <emphasis>git stash apply</emphasis> fails due to conflicts. Since\r
162 the stash is applied on top of the commit that was HEAD at the time\r
163 <emphasis>git stash</emphasis> was run, it restores the originally stashed state with\r
164 no conflicts.</simpara>\r
165 </listitem>\r
166 </varlistentry>\r
167 <varlistentry>\r
168 <term>\r
169 clear\r
170 </term>\r
171 <listitem>\r
172 <simpara>\r
173         Remove all the stashed states. Note that those states will then\r
174         be subject to pruning, and may be impossible to recover (see\r
175         <emphasis>Examples</emphasis> below for a possible strategy).\r
176 </simpara>\r
177 </listitem>\r
178 </varlistentry>\r
179 <varlistentry>\r
180 <term>\r
181 drop [-q|--quiet] [&lt;stash&gt;]\r
182 </term>\r
183 <listitem>\r
184 <simpara>\r
185         Remove a single stashed state from the stash list. When no <emphasis>&lt;stash&gt;</emphasis>\r
186         is given, it removes the latest one. i.e. <emphasis>stash@{0}</emphasis>, otherwise\r
187         <emphasis>&lt;stash&gt;</emphasis> must a valid stash log reference of the form\r
188         <emphasis>stash@{&lt;revision&gt;}</emphasis>.\r
189 </simpara>\r
190 </listitem>\r
191 </varlistentry>\r
192 <varlistentry>\r
193 <term>\r
194 create\r
195 </term>\r
196 <listitem>\r
197 <simpara>\r
198         Create a stash (which is a regular commit object) and return its\r
199         object name, without storing it anywhere in the ref namespace.\r
200 </simpara>\r
201 </listitem>\r
202 </varlistentry>\r
203 </variablelist>\r
204 </simplesect>\r
205 <simplesect id="_discussion">\r
206 <title>DISCUSSION</title>\r
207 <simpara>A stash is represented as a commit whose tree records the state of the\r
208 working directory, and its first parent is the commit at <emphasis>HEAD</emphasis> when\r
209 the stash was created.  The tree of the second parent records the\r
210 state of the index when the stash is made, and it is made a child of\r
211 the <emphasis>HEAD</emphasis> commit.  The ancestry graph looks like this:</simpara>\r
212 <literallayout class="monospaced">       .----W\r
213       /    /\r
214 -----H----I</literallayout>\r
215 <simpara>where <emphasis>H</emphasis> is the <emphasis>HEAD</emphasis> commit, <emphasis>I</emphasis> is a commit that records the state\r
216 of the index, and <emphasis>W</emphasis> is a commit that records the state of the working\r
217 tree.</simpara>\r
218 </simplesect>\r
219 <simplesect id="_examples">\r
220 <title>EXAMPLES</title>\r
221 <variablelist>\r
222 <varlistentry>\r
223 <term>\r
224 Pulling into a dirty tree\r
225 </term>\r
226 <listitem>\r
227 <simpara>\r
228 When you are in the middle of something, you learn that there are\r
229 upstream changes that are possibly relevant to what you are\r
230 doing.  When your local changes do not conflict with the changes in\r
231 the upstream, a simple <emphasis>git pull</emphasis> will let you move forward.\r
232 </simpara>\r
233 <simpara>However, there are cases in which your local changes do conflict with\r
234 the upstream changes, and <emphasis>git pull</emphasis> refuses to overwrite your\r
235 changes.  In such a case, you can stash your changes away,\r
236 perform a pull, and then unstash, like this:</simpara>\r
237 <screen>$ git pull\r
238  ...\r
239 file foobar not up to date, cannot merge.\r
240 $ git stash\r
241 $ git pull\r
242 $ git stash pop</screen>\r
243 </listitem>\r
244 </varlistentry>\r
245 <varlistentry>\r
246 <term>\r
247 Interrupted workflow\r
248 </term>\r
249 <listitem>\r
250 <simpara>\r
251 When you are in the middle of something, your boss comes in and\r
252 demands that you fix something immediately.  Traditionally, you would\r
253 make a commit to a temporary branch to store your changes away, and\r
254 return to your original branch to make the emergency fix, like this:\r
255 </simpara>\r
256 <screen># ... hack hack hack ...\r
257 $ git checkout -b my_wip\r
258 $ git commit -a -m "WIP"\r
259 $ git checkout master\r
260 $ edit emergency fix\r
261 $ git commit -a -m "Fix in a hurry"\r
262 $ git checkout my_wip\r
263 $ git reset --soft HEAD^\r
264 # ... continue hacking ...</screen>\r
265 <simpara>You can use <emphasis>git stash</emphasis> to simplify the above, like this:</simpara>\r
266 <screen># ... hack hack hack ...\r
267 $ git stash\r
268 $ edit emergency fix\r
269 $ git commit -a -m "Fix in a hurry"\r
270 $ git stash pop\r
271 # ... continue hacking ...</screen>\r
272 </listitem>\r
273 </varlistentry>\r
274 <varlistentry>\r
275 <term>\r
276 Testing partial commits\r
277 </term>\r
278 <listitem>\r
279 <simpara>\r
280 You can use <emphasis>git stash save --keep-index</emphasis> when you want to make two or\r
281 more commits out of the changes in the work tree, and you want to test\r
282 each change before committing:\r
283 </simpara>\r
284 <screen># ... hack hack hack ...\r
285 $ git add --patch foo            # add just first part to the index\r
286 $ git stash save --keep-index    # save all other changes to the stash\r
287 $ edit/build/test first part\r
288 $ git commit -m 'First part'     # commit fully tested change\r
289 $ git stash pop                  # prepare to work on all other changes\r
290 # ... repeat above five steps until one commit remains ...\r
291 $ edit/build/test remaining parts\r
292 $ git commit foo -m 'Remaining parts'</screen>\r
293 </listitem>\r
294 </varlistentry>\r
295 <varlistentry>\r
296 <term>\r
297 Recovering stashes that were cleared/dropped erroneously\r
298 </term>\r
299 <listitem>\r
300 <simpara>\r
301 If you mistakenly drop or clear stashes, they cannot be recovered\r
302 through the normal safety mechanisms.  However, you can try the\r
303 following incantation to get a list of stashes that are still in your\r
304 repository, but not reachable any more:\r
305 </simpara>\r
306 <screen>git fsck --unreachable |\r
307 grep commit | cut -d\  -f3 |\r
308 xargs git log --merges --no-walk --grep=WIP</screen>\r
309 </listitem>\r
310 </varlistentry>\r
311 </variablelist>\r
312 </simplesect>\r
313 <simplesect id="_see_also">\r
314 <title>SEE ALSO</title>\r
315 <simpara><xref linkend="git-checkout(1)" />,\r
316 <xref linkend="git-commit(1)" />,\r
317 <xref linkend="git-reflog(1)" />,\r
318 <xref linkend="git-reset(1)" /></simpara>\r
319 </simplesect>\r
320 <simplesect id="_git">\r
321 <title>GIT</title>\r
322 <simpara>Part of the <xref linkend="git(1)" /> suite</simpara>\r
323 </simplesect>\r
324 </article>\r