1GIT-STASH(1) Git Manual GIT-STASH(1)
2
3
4
6 git-stash - Stash the changes in a dirty working directory away
7
9 git stash list [<log-options>]
10 git stash show [-u|--include-untracked|--only-untracked] [<diff-options>] [<stash>]
11 git stash drop [-q|--quiet] [<stash>]
12 git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
13 git stash branch <branchname> [<stash>]
14 git stash [push [-p|--patch] [-S|--staged] [-k|--[no-]keep-index] [-q|--quiet]
15 [-u|--include-untracked] [-a|--all] [-m|--message <message>]
16 [--pathspec-from-file=<file> [--pathspec-file-nul]]
17 [--] [<pathspec>...]]
18 git stash clear
19 git stash create [<message>]
20 git stash store [-m|--message <message>] [-q|--quiet] <commit>
21
23 Use git stash when you want to record the current state of the working
24 directory and the index, but want to go back to a clean working
25 directory. The command saves your local modifications away and reverts
26 the working directory to match the HEAD commit.
27
28 The modifications stashed away by this command can be listed with git
29 stash list, inspected with git stash show, and restored (potentially on
30 top of a different commit) with git stash apply. Calling git stash
31 without any arguments is equivalent to git stash push. A stash is by
32 default listed as "WIP on branchname ...", but you can give a more
33 descriptive message on the command line when you create one.
34
35 The latest stash you created is stored in refs/stash; older stashes are
36 found in the reflog of this reference and can be named using the usual
37 reflog syntax (e.g. stash@{0} is the most recently created stash,
38 stash@{1} is the one before it, stash@{2.hours.ago} is also possible).
39 Stashes may also be referenced by specifying just the stash index (e.g.
40 the integer n is equivalent to stash@{n}).
41
43 push [-p|--patch] [-S|--staged] [-k|--[no-]keep-index]
44 [-u|--include-untracked] [-a|--all] [-q|--quiet] [-m|--message
45 <message>] [--pathspec-from-file=<file> [--pathspec-file-nul]] [--]
46 [<pathspec>...]
47 Save your local modifications to a new stash entry and roll them
48 back to HEAD (in the working tree and in the index). The <message>
49 part is optional and gives the description along with the stashed
50 state.
51
52 For quickly making a snapshot, you can omit "push". In this mode,
53 non-option arguments are not allowed to prevent a misspelled
54 subcommand from making an unwanted stash entry. The two exceptions
55 to this are stash -p which acts as alias for stash push -p and
56 pathspec elements, which are allowed after a double hyphen -- for
57 disambiguation.
58
59 save [-p|--patch] [-S|--staged] [-k|--[no-]keep-index]
60 [-u|--include-untracked] [-a|--all] [-q|--quiet] [<message>]
61 This option is deprecated in favour of git stash push. It differs
62 from "stash push" in that it cannot take pathspec. Instead, all
63 non-option arguments are concatenated to form the stash message.
64
65 list [<log-options>]
66 List the stash entries that you currently have. Each stash entry is
67 listed with its name (e.g. stash@{0} is the latest entry,
68 stash@{1} is the one before, etc.), the name of the branch that was
69 current when the entry was made, and a short description of the
70 commit the entry was based on.
71
72 stash@{0}: WIP on submit: 6ebd0e2... Update git-stash documentation
73 stash@{1}: On master: 9cc0589... Add git-stash
74
75 The command takes options applicable to the git log command to
76 control what is shown and how. See git-log(1).
77
78 show [-u|--include-untracked|--only-untracked] [<diff-options>]
79 [<stash>]
80 Show the changes recorded in the stash entry as a diff between the
81 stashed contents and the commit back when the stash entry was first
82 created. By default, the command shows the diffstat, but it will
83 accept any format known to git diff (e.g., git stash show -p
84 stash@{1} to view the second most recent entry in patch form). If
85 no <diff-option> is provided, the default behavior will be given by
86 the stash.showStat, and stash.showPatch config variables. You can
87 also use stash.showIncludeUntracked to set whether
88 --include-untracked is enabled by default.
89
90 pop [--index] [-q|--quiet] [<stash>]
91 Remove a single stashed state from the stash list and apply it on
92 top of the current working tree state, i.e., do the inverse
93 operation of git stash push. The working directory must match the
94 index.
95
96 Applying the state can fail with conflicts; in this case, it is not
97 removed from the stash list. You need to resolve the conflicts by
98 hand and call git stash drop manually afterwards.
99
100 apply [--index] [-q|--quiet] [<stash>]
101 Like pop, but do not remove the state from the stash list. Unlike
102 pop, <stash> may be any commit that looks like a commit created by
103 stash push or stash create.
104
105 branch <branchname> [<stash>]
106 Creates and checks out a new branch named <branchname> starting
107 from the commit at which the <stash> was originally created,
108 applies the changes recorded in <stash> to the new working tree and
109 index. If that succeeds, and <stash> is a reference of the form
110 stash@{<revision>}, it then drops the <stash>.
111
112 This is useful if the branch on which you ran git stash push has
113 changed enough that git stash apply fails due to conflicts. Since
114 the stash entry is applied on top of the commit that was HEAD at
115 the time git stash was run, it restores the originally stashed
116 state with no conflicts.
117
118 clear
119 Remove all the stash entries. Note that those entries will then be
120 subject to pruning, and may be impossible to recover (see Examples
121 below for a possible strategy).
122
123 drop [-q|--quiet] [<stash>]
124 Remove a single stash entry from the list of stash entries.
125
126 create
127 Create a stash entry (which is a regular commit object) and return
128 its object name, without storing it anywhere in the ref namespace.
129 This is intended to be useful for scripts. It is probably not the
130 command you want to use; see "push" above.
131
132 store
133 Store a given stash created via git stash create (which is a
134 dangling merge commit) in the stash ref, updating the stash reflog.
135 This is intended to be useful for scripts. It is probably not the
136 command you want to use; see "push" above.
137
139 -a, --all
140 This option is only valid for push and save commands.
141
142 All ignored and untracked files are also stashed and then cleaned
143 up with git clean.
144
145 -u, --include-untracked, --no-include-untracked
146 When used with the push and save commands, all untracked files are
147 also stashed and then cleaned up with git clean.
148
149 When used with the show command, show the untracked files in the
150 stash entry as part of the diff.
151
152 --only-untracked
153 This option is only valid for the show command.
154
155 Show only the untracked files in the stash entry as part of the
156 diff.
157
158 --index
159 This option is only valid for pop and apply commands.
160
161 Tries to reinstate not only the working tree’s changes, but also
162 the index’s ones. However, this can fail, when you have conflicts
163 (which are stored in the index, where you therefore can no longer
164 apply the changes as they were originally).
165
166 -k, --keep-index, --no-keep-index
167 This option is only valid for push and save commands.
168
169 All changes already added to the index are left intact.
170
171 -p, --patch
172 This option is only valid for push and save commands.
173
174 Interactively select hunks from the diff between HEAD and the
175 working tree to be stashed. The stash entry is constructed such
176 that its index state is the same as the index state of your
177 repository, and its worktree contains only the changes you selected
178 interactively. The selected changes are then rolled back from your
179 worktree. See the “Interactive Mode” section of git-add(1) to learn
180 how to operate the --patch mode.
181
182 The --patch option implies --keep-index. You can use
183 --no-keep-index to override this.
184
185 -S, --staged
186 This option is only valid for push and save commands.
187
188 Stash only the changes that are currently staged. This is similar
189 to basic git commit except the state is committed to the stash
190 instead of current branch.
191
192 The --patch option has priority over this one.
193
194 --pathspec-from-file=<file>
195 This option is only valid for push command.
196
197 Pathspec is passed in <file> instead of commandline args. If <file>
198 is exactly - then standard input is used. Pathspec elements are
199 separated by LF or CR/LF. Pathspec elements can be quoted as
200 explained for the configuration variable core.quotePath (see git-
201 config(1)). See also --pathspec-file-nul and global
202 --literal-pathspecs.
203
204 --pathspec-file-nul
205 This option is only valid for push command.
206
207 Only meaningful with --pathspec-from-file. Pathspec elements are
208 separated with NUL character and all other characters are taken
209 literally (including newlines and quotes).
210
211 -q, --quiet
212 This option is only valid for apply, drop, pop, push, save, store
213 commands.
214
215 Quiet, suppress feedback messages.
216
217 --
218 This option is only valid for push command.
219
220 Separates pathspec from options for disambiguation purposes.
221
222 <pathspec>...
223 This option is only valid for push command.
224
225 The new stash entry records the modified states only for the files
226 that match the pathspec. The index entries and working tree files
227 are then rolled back to the state in HEAD only for these files,
228 too, leaving files that do not match the pathspec intact.
229
230 For more details, see the pathspec entry in gitglossary(7).
231
232 <stash>
233 This option is only valid for apply, branch, drop, pop, show
234 commands.
235
236 A reference of the form stash@{<revision>}. When no <stash> is
237 given, the latest stash is assumed (that is, stash@{0}).
238
240 A stash entry is represented as a commit whose tree records the state
241 of the working directory, and its first parent is the commit at HEAD
242 when the entry was created. The tree of the second parent records the
243 state of the index when the entry is made, and it is made a child of
244 the HEAD commit. The ancestry graph looks like this:
245
246 .----W
247 / /
248 -----H----I
249
250 where H is the HEAD commit, I is a commit that records the state of the
251 index, and W is a commit that records the state of the working tree.
252
254 Pulling into a dirty tree
255 When you are in the middle of something, you learn that there are
256 upstream changes that are possibly relevant to what you are doing.
257 When your local changes do not conflict with the changes in the
258 upstream, a simple git pull will let you move forward.
259
260 However, there are cases in which your local changes do conflict
261 with the upstream changes, and git pull refuses to overwrite your
262 changes. In such a case, you can stash your changes away, perform a
263 pull, and then unstash, like this:
264
265 $ git pull
266 ...
267 file foobar not up to date, cannot merge.
268 $ git stash
269 $ git pull
270 $ git stash pop
271
272 Interrupted workflow
273 When you are in the middle of something, your boss comes in and
274 demands that you fix something immediately. Traditionally, you
275 would make a commit to a temporary branch to store your changes
276 away, and return to your original branch to make the emergency fix,
277 like this:
278
279 # ... hack hack hack ...
280 $ git switch -c my_wip
281 $ git commit -a -m "WIP"
282 $ git switch master
283 $ edit emergency fix
284 $ git commit -a -m "Fix in a hurry"
285 $ git switch my_wip
286 $ git reset --soft HEAD^
287 # ... continue hacking ...
288
289 You can use git stash to simplify the above, like this:
290
291 # ... hack hack hack ...
292 $ git stash
293 $ edit emergency fix
294 $ git commit -a -m "Fix in a hurry"
295 $ git stash pop
296 # ... continue hacking ...
297
298 Testing partial commits
299 You can use git stash push --keep-index when you want to make two
300 or more commits out of the changes in the work tree, and you want
301 to test each change before committing:
302
303 # ... hack hack hack ...
304 $ git add --patch foo # add just first part to the index
305 $ git stash push --keep-index # save all other changes to the stash
306 $ edit/build/test first part
307 $ git commit -m 'First part' # commit fully tested change
308 $ git stash pop # prepare to work on all other changes
309 # ... repeat above five steps until one commit remains ...
310 $ edit/build/test remaining parts
311 $ git commit foo -m 'Remaining parts'
312
313 Saving unrelated changes for future use
314 When you are in the middle of massive changes and you find some
315 unrelated issue that you don’t want to forget to fix, you can do
316 the change(s), stage them, and use git stash push --staged to stash
317 them out for future use. This is similar to committing the staged
318 changes, only the commit ends-up being in the stash and not on the
319 current branch.
320
321 # ... hack hack hack ...
322 $ git add --patch foo # add unrelated changes to the index
323 $ git stash push --staged # save these changes to the stash
324 # ... hack hack hack, finish curent changes ...
325 $ git commit -m 'Massive' # commit fully tested changes
326 $ git switch fixup-branch # switch to another branch
327 $ git stash pop # to finish work on the saved changes
328
329 Recovering stash entries that were cleared/dropped erroneously
330 If you mistakenly drop or clear stash entries, they cannot be
331 recovered through the normal safety mechanisms. However, you can
332 try the following incantation to get a list of stash entries that
333 are still in your repository, but not reachable any more:
334
335 git fsck --unreachable |
336 grep commit | cut -d\ -f3 |
337 xargs git log --merges --no-walk --grep=WIP
338
340 git-checkout(1), git-commit(1), git-reflog(1), git-reset(1), git-
341 switch(1)
342
344 Part of the git(1) suite
345
346
347
348Git 2.36.1 2022-05-05 GIT-STASH(1)