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