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] [-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] [-k|--[no-]keep-index] [-u|--include-untracked]
44 [-a|--all] [-q|--quiet] [-m|--message <message>]
45 [--pathspec-from-file=<file> [--pathspec-file-nul]] [--] [<pathspec>...
46 ]
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] [-k|--[no-]keep-index] [-u|--include-untracked]
60 [-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 --pathspec-from-file=<file>
186 This option is only valid for push command.
187
188 Pathspec is passed in <file> instead of commandline args. If <file>
189 is exactly - then standard input is used. Pathspec elements are
190 separated by LF or CR/LF. Pathspec elements can be quoted as
191 explained for the configuration variable core.quotePath (see git-
192 config(1)). See also --pathspec-file-nul and global
193 --literal-pathspecs.
194
195 --pathspec-file-nul
196 This option is only valid for push command.
197
198 Only meaningful with --pathspec-from-file. Pathspec elements are
199 separated with NUL character and all other characters are taken
200 literally (including newlines and quotes).
201
202 -q, --quiet
203 This option is only valid for apply, drop, pop, push, save, store
204 commands.
205
206 Quiet, suppress feedback messages.
207
208 --
209 This option is only valid for push command.
210
211 Separates pathspec from options for disambiguation purposes.
212
213 <pathspec>...
214 This option is only valid for push command.
215
216 The new stash entry records the modified states only for the files
217 that match the pathspec. The index entries and working tree files
218 are then rolled back to the state in HEAD only for these files,
219 too, leaving files that do not match the pathspec intact.
220
221 For more details, see the pathspec entry in gitglossary(7).
222
223 <stash>
224 This option is only valid for apply, branch, drop, pop, show
225 commands.
226
227 A reference of the form stash@{<revision>}. When no <stash> is
228 given, the latest stash is assumed (that is, stash@{0}).
229
231 A stash entry is represented as a commit whose tree records the state
232 of the working directory, and its first parent is the commit at HEAD
233 when the entry was created. The tree of the second parent records the
234 state of the index when the entry is made, and it is made a child of
235 the HEAD commit. The ancestry graph looks like this:
236
237 .----W
238 / /
239 -----H----I
240
241 where H is the HEAD commit, I is a commit that records the state of the
242 index, and W is a commit that records the state of the working tree.
243
245 Pulling into a dirty tree
246 When you are in the middle of something, you learn that there are
247 upstream changes that are possibly relevant to what you are doing.
248 When your local changes do not conflict with the changes in the
249 upstream, a simple git pull will let you move forward.
250
251 However, there are cases in which your local changes do conflict
252 with the upstream changes, and git pull refuses to overwrite your
253 changes. In such a case, you can stash your changes away, perform a
254 pull, and then unstash, like this:
255
256 $ git pull
257 ...
258 file foobar not up to date, cannot merge.
259 $ git stash
260 $ git pull
261 $ git stash pop
262
263 Interrupted workflow
264 When you are in the middle of something, your boss comes in and
265 demands that you fix something immediately. Traditionally, you
266 would make a commit to a temporary branch to store your changes
267 away, and return to your original branch to make the emergency fix,
268 like this:
269
270 # ... hack hack hack ...
271 $ git switch -c my_wip
272 $ git commit -a -m "WIP"
273 $ git switch master
274 $ edit emergency fix
275 $ git commit -a -m "Fix in a hurry"
276 $ git switch my_wip
277 $ git reset --soft HEAD^
278 # ... continue hacking ...
279
280 You can use git stash to simplify the above, like this:
281
282 # ... hack hack hack ...
283 $ git stash
284 $ edit emergency fix
285 $ git commit -a -m "Fix in a hurry"
286 $ git stash pop
287 # ... continue hacking ...
288
289 Testing partial commits
290 You can use git stash push --keep-index when you want to make two
291 or more commits out of the changes in the work tree, and you want
292 to test each change before committing:
293
294 # ... hack hack hack ...
295 $ git add --patch foo # add just first part to the index
296 $ git stash push --keep-index # save all other changes to the stash
297 $ edit/build/test first part
298 $ git commit -m 'First part' # commit fully tested change
299 $ git stash pop # prepare to work on all other changes
300 # ... repeat above five steps until one commit remains ...
301 $ edit/build/test remaining parts
302 $ git commit foo -m 'Remaining parts'
303
304 Recovering stash entries that were cleared/dropped erroneously
305 If you mistakenly drop or clear stash entries, they cannot be
306 recovered through the normal safety mechanisms. However, you can
307 try the following incantation to get a list of stash entries that
308 are still in your repository, but not reachable any more:
309
310 git fsck --unreachable |
311 grep commit | cut -d\ -f3 |
312 xargs git log --merges --no-walk --grep=WIP
313
315 git-checkout(1), git-commit(1), git-reflog(1), git-reset(1), git-
316 switch(1)
317
319 Part of the git(1) suite
320
321
322
323Git 2.33.1 2021-10-12 GIT-STASH(1)