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 [<options>]
10 git stash show [<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 [<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 [<options>] [<stash>]
79 Show the changes recorded in the stash entry as a diff between the
80 stashed contents and the commit back when the stash entry was first
81 created. By default, the command shows the diffstat, but it will
82 accept any format known to git diff (e.g., git stash show -p
83 stash@{1} to view the second most recent entry in patch form). You
84 can use stash.showStat and/or stash.showPatch config variables to
85 change the default behavior.
86
87 pop [--index] [-q|--quiet] [<stash>]
88 Remove a single stashed state from the stash list and apply it on
89 top of the current working tree state, i.e., do the inverse
90 operation of git stash push. The working directory must match the
91 index.
92
93 Applying the state can fail with conflicts; in this case, it is not
94 removed from the stash list. You need to resolve the conflicts by
95 hand and call git stash drop manually afterwards.
96
97 apply [--index] [-q|--quiet] [<stash>]
98 Like pop, but do not remove the state from the stash list. Unlike
99 pop, <stash> may be any commit that looks like a commit created by
100 stash push or stash create.
101
102 branch <branchname> [<stash>]
103 Creates and checks out a new branch named <branchname> starting
104 from the commit at which the <stash> was originally created,
105 applies the changes recorded in <stash> to the new working tree and
106 index. If that succeeds, and <stash> is a reference of the form
107 stash@{<revision>}, it then drops the <stash>.
108
109 This is useful if the branch on which you ran git stash push has
110 changed enough that git stash apply fails due to conflicts. Since
111 the stash entry is applied on top of the commit that was HEAD at
112 the time git stash was run, it restores the originally stashed
113 state with no conflicts.
114
115 clear
116 Remove all the stash entries. Note that those entries will then be
117 subject to pruning, and may be impossible to recover (see Examples
118 below for a possible strategy).
119
120 drop [-q|--quiet] [<stash>]
121 Remove a single stash entry from the list of stash entries.
122
123 create
124 Create a stash entry (which is a regular commit object) and return
125 its object name, without storing it anywhere in the ref namespace.
126 This is intended to be useful for scripts. It is probably not the
127 command you want to use; see "push" above.
128
129 store
130 Store a given stash created via git stash create (which is a
131 dangling merge commit) in the stash ref, updating the stash reflog.
132 This is intended to be useful for scripts. It is probably not the
133 command you want to use; see "push" above.
134
136 -a, --all
137 This option is only valid for push and save commands.
138
139 All ignored and untracked files are also stashed and then cleaned
140 up with git clean.
141
142 -u, --include-untracked
143 This option is only valid for push and save commands.
144
145 All untracked files are also stashed and then cleaned up with git
146 clean.
147
148 --index
149 This option is only valid for pop and apply commands.
150
151 Tries to reinstate not only the working tree’s changes, but also
152 the index’s ones. However, this can fail, when you have conflicts
153 (which are stored in the index, where you therefore can no longer
154 apply the changes as they were originally).
155
156 -k, --keep-index, --no-keep-index
157 This option is only valid for push and save commands.
158
159 All changes already added to the index are left intact.
160
161 -p, --patch
162 This option is only valid for push and save commands.
163
164 Interactively select hunks from the diff between HEAD and the
165 working tree to be stashed. The stash entry is constructed such
166 that its index state is the same as the index state of your
167 repository, and its worktree contains only the changes you selected
168 interactively. The selected changes are then rolled back from your
169 worktree. See the “Interactive Mode” section of git-add(1) to learn
170 how to operate the --patch mode.
171
172 The --patch option implies --keep-index. You can use
173 --no-keep-index to override this.
174
175 --pathspec-from-file=<file>
176 This option is only valid for push command.
177
178 Pathspec is passed in <file> instead of commandline args. If <file>
179 is exactly - then standard input is used. Pathspec elements are
180 separated by LF or CR/LF. Pathspec elements can be quoted as
181 explained for the configuration variable core.quotePath (see git-
182 config(1)). See also --pathspec-file-nul and global
183 --literal-pathspecs.
184
185 --pathspec-file-nul
186 This option is only valid for push command.
187
188 Only meaningful with --pathspec-from-file. Pathspec elements are
189 separated with NUL character and all other characters are taken
190 literally (including newlines and quotes).
191
192 -q, --quiet
193 This option is only valid for apply, drop, pop, push, save, store
194 commands.
195
196 Quiet, suppress feedback messages.
197
198 --
199 This option is only valid for push command.
200
201 Separates pathspec from options for disambiguation purposes.
202
203 <pathspec>...
204 This option is only valid for push command.
205
206 The new stash entry records the modified states only for the files
207 that match the pathspec. The index entries and working tree files
208 are then rolled back to the state in HEAD only for these files,
209 too, leaving files that do not match the pathspec intact.
210
211 For more details, see the pathspec entry in gitglossary(7).
212
213 <stash>
214 This option is only valid for apply, branch, drop, pop, show
215 commands.
216
217 A reference of the form stash@{<revision>}. When no <stash> is
218 given, the latest stash is assumed (that is, stash@{0}).
219
221 A stash entry is represented as a commit whose tree records the state
222 of the working directory, and its first parent is the commit at HEAD
223 when the entry was created. The tree of the second parent records the
224 state of the index when the entry is made, and it is made a child of
225 the HEAD commit. The ancestry graph looks like this:
226
227 .----W
228 / /
229 -----H----I
230
231 where H is the HEAD commit, I is a commit that records the state of the
232 index, and W is a commit that records the state of the working tree.
233
235 Pulling into a dirty tree
236 When you are in the middle of something, you learn that there are
237 upstream changes that are possibly relevant to what you are doing.
238 When your local changes do not conflict with the changes in the
239 upstream, a simple git pull will let you move forward.
240
241 However, there are cases in which your local changes do conflict
242 with the upstream changes, and git pull refuses to overwrite your
243 changes. In such a case, you can stash your changes away, perform a
244 pull, and then unstash, like this:
245
246 $ git pull
247 ...
248 file foobar not up to date, cannot merge.
249 $ git stash
250 $ git pull
251 $ git stash pop
252
253 Interrupted workflow
254 When you are in the middle of something, your boss comes in and
255 demands that you fix something immediately. Traditionally, you
256 would make a commit to a temporary branch to store your changes
257 away, and return to your original branch to make the emergency fix,
258 like this:
259
260 # ... hack hack hack ...
261 $ git switch -c my_wip
262 $ git commit -a -m "WIP"
263 $ git switch master
264 $ edit emergency fix
265 $ git commit -a -m "Fix in a hurry"
266 $ git switch my_wip
267 $ git reset --soft HEAD^
268 # ... continue hacking ...
269
270 You can use git stash to simplify the above, like this:
271
272 # ... hack hack hack ...
273 $ git stash
274 $ edit emergency fix
275 $ git commit -a -m "Fix in a hurry"
276 $ git stash pop
277 # ... continue hacking ...
278
279 Testing partial commits
280 You can use git stash push --keep-index when you want to make two
281 or more commits out of the changes in the work tree, and you want
282 to test each change before committing:
283
284 # ... hack hack hack ...
285 $ git add --patch foo # add just first part to the index
286 $ git stash push --keep-index # save all other changes to the stash
287 $ edit/build/test first part
288 $ git commit -m 'First part' # commit fully tested change
289 $ git stash pop # prepare to work on all other changes
290 # ... repeat above five steps until one commit remains ...
291 $ edit/build/test remaining parts
292 $ git commit foo -m 'Remaining parts'
293
294 Recovering stash entries that were cleared/dropped erroneously
295 If you mistakenly drop or clear stash entries, they cannot be
296 recovered through the normal safety mechanisms. However, you can
297 try the following incantation to get a list of stash entries that
298 are still in your repository, but not reachable any more:
299
300 git fsck --unreachable |
301 grep commit | cut -d\ -f3 |
302 xargs git log --merges --no-walk --grep=WIP
303
305 git-checkout(1), git-commit(1), git-reflog(1), git-reset(1), git-
306 switch(1)
307
309 Part of the git(1) suite
310
311
312
313Git 2.26.2 2020-04-20 GIT-STASH(1)