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 [<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>...]]
17 git stash clear
18 git stash create [<message>]
19 git stash store [-m|--message <message>] [-q|--quiet] <commit>
20
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>] [--] [<pathspec>...]
45 Save your local modifications to a new stash entry and roll them
46 back to HEAD (in the working tree and in the index). The <message>
47 part is optional and gives the description along with the stashed
48 state.
49
50 For quickly making a snapshot, you can omit "push". In this mode,
51 non-option arguments are not allowed to prevent a misspelled
52 subcommand from making an unwanted stash entry. The two exceptions
53 to this are stash -p which acts as alias for stash push -p and
54 pathspecs, which are allowed after a double hyphen -- for
55 disambiguation.
56
57 When pathspec is given to git stash push, the new stash entry
58 records the modified states only for the files that match the
59 pathspec. The index entries and working tree files are then rolled
60 back to the state in HEAD only for these files, too, leaving files
61 that do not match the pathspec intact.
62
63 If the --keep-index option is used, all changes already added to
64 the index are left intact.
65
66 If the --include-untracked option is used, all untracked files are
67 also stashed and then cleaned up with git clean, leaving the
68 working directory in a very clean state. If the --all option is
69 used instead then the ignored files are stashed and cleaned in
70 addition to the untracked files.
71
72 With --patch, you can interactively select hunks from the diff
73 between HEAD and the working tree to be stashed. The stash entry is
74 constructed such that its index state is the same as the index
75 state of your repository, and its worktree contains only the
76 changes you selected interactively. The selected changes are then
77 rolled back from your worktree. See the “Interactive Mode” section
78 of git-add(1) to learn how to operate the --patch mode.
79
80 The --patch option implies --keep-index. You can use
81 --no-keep-index to override this.
82
83 save [-p|--patch] [-k|--[no-]keep-index] [-u|--include-untracked]
84 [-a|--all] [-q|--quiet] [<message>]
85 This option is deprecated in favour of git stash push. It differs
86 from "stash push" in that it cannot take pathspecs, and any
87 non-option arguments form the message.
88
89 list [<options>]
90 List the stash entries that you currently have. Each stash entry is
91 listed with its name (e.g. stash@{0} is the latest entry,
92 stash@{1} is the one before, etc.), the name of the branch that was
93 current when the entry was made, and a short description of the
94 commit the entry was based on.
95
96 stash@{0}: WIP on submit: 6ebd0e2... Update git-stash documentation
97 stash@{1}: On master: 9cc0589... Add git-stash
98
99 The command takes options applicable to the git log command to
100 control what is shown and how. See git-log(1).
101
102 show [<stash>]
103 Show the changes recorded in the stash entry as a diff between the
104 stashed contents and the commit back when the stash entry was first
105 created. When no <stash> is given, it shows the latest one. By
106 default, the command shows the diffstat, but it will accept any
107 format known to git diff (e.g., git stash show -p stash@{1} to view
108 the second most recent entry in patch form). You can use
109 stash.showStat and/or stash.showPatch config variables to change
110 the default behavior.
111
112 pop [--index] [-q|--quiet] [<stash>]
113 Remove a single stashed state from the stash list and apply it on
114 top of the current working tree state, i.e., do the inverse
115 operation of git stash push. The working directory must match the
116 index.
117
118 Applying the state can fail with conflicts; in this case, it is not
119 removed from the stash list. You need to resolve the conflicts by
120 hand and call git stash drop manually afterwards.
121
122 If the --index option is used, then tries to reinstate not only the
123 working tree’s changes, but also the index’s ones. However, this
124 can fail, when you have conflicts (which are stored in the index,
125 where you therefore can no longer apply the changes as they were
126 originally).
127
128 When no <stash> is given, stash@{0} is assumed, otherwise <stash>
129 must be a reference of the form stash@{<revision>}.
130
131 apply [--index] [-q|--quiet] [<stash>]
132 Like pop, but do not remove the state from the stash list. Unlike
133 pop, <stash> may be any commit that looks like a commit created by
134 stash push or stash create.
135
136 branch <branchname> [<stash>]
137 Creates and checks out a new branch named <branchname> starting
138 from the commit at which the <stash> was originally created,
139 applies the changes recorded in <stash> to the new working tree and
140 index. If that succeeds, and <stash> is a reference of the form
141 stash@{<revision>}, it then drops the <stash>. When no <stash> is
142 given, applies the latest one.
143
144 This is useful if the branch on which you ran git stash push has
145 changed enough that git stash apply fails due to conflicts. Since
146 the stash entry is applied on top of the commit that was HEAD at
147 the time git stash was run, it restores the originally stashed
148 state with no conflicts.
149
150 clear
151 Remove all the stash entries. Note that those entries will then be
152 subject to pruning, and may be impossible to recover (see Examples
153 below for a possible strategy).
154
155 drop [-q|--quiet] [<stash>]
156 Remove a single stash entry from the list of stash entries. When no
157 <stash> is given, it removes the latest one. i.e. stash@{0},
158 otherwise <stash> must be a valid stash log reference of the form
159 stash@{<revision>}.
160
161 create
162 Create a stash entry (which is a regular commit object) and return
163 its object name, without storing it anywhere in the ref namespace.
164 This is intended to be useful for scripts. It is probably not the
165 command you want to use; see "push" above.
166
167 store
168 Store a given stash created via git stash create (which is a
169 dangling merge commit) in the stash ref, updating the stash reflog.
170 This is intended to be useful for scripts. It is probably not the
171 command you want to use; see "push" above.
172
174 A stash entry is represented as a commit whose tree records the state
175 of the working directory, and its first parent is the commit at HEAD
176 when the entry was created. The tree of the second parent records the
177 state of the index when the entry is made, and it is made a child of
178 the HEAD commit. The ancestry graph looks like this:
179
180 .----W
181 / /
182 -----H----I
183
184 where H is the HEAD commit, I is a commit that records the state of the
185 index, and W is a commit that records the state of the working tree.
186
188 Pulling into a dirty tree
189 When you are in the middle of something, you learn that there are
190 upstream changes that are possibly relevant to what you are doing.
191 When your local changes do not conflict with the changes in the
192 upstream, a simple git pull will let you move forward.
193
194 However, there are cases in which your local changes do conflict
195 with the upstream changes, and git pull refuses to overwrite your
196 changes. In such a case, you can stash your changes away, perform a
197 pull, and then unstash, like this:
198
199 $ git pull
200 ...
201 file foobar not up to date, cannot merge.
202 $ git stash
203 $ git pull
204 $ git stash pop
205
206
207 Interrupted workflow
208 When you are in the middle of something, your boss comes in and
209 demands that you fix something immediately. Traditionally, you
210 would make a commit to a temporary branch to store your changes
211 away, and return to your original branch to make the emergency fix,
212 like this:
213
214 # ... hack hack hack ...
215 $ git checkout -b my_wip
216 $ git commit -a -m "WIP"
217 $ git checkout master
218 $ edit emergency fix
219 $ git commit -a -m "Fix in a hurry"
220 $ git checkout my_wip
221 $ git reset --soft HEAD^
222 # ... continue hacking ...
223
224 You can use git stash to simplify the above, like this:
225
226 # ... hack hack hack ...
227 $ git stash
228 $ edit emergency fix
229 $ git commit -a -m "Fix in a hurry"
230 $ git stash pop
231 # ... continue hacking ...
232
233
234 Testing partial commits
235 You can use git stash push --keep-index when you want to make two
236 or more commits out of the changes in the work tree, and you want
237 to test each change before committing:
238
239 # ... hack hack hack ...
240 $ git add --patch foo # add just first part to the index
241 $ git stash push --keep-index # save all other changes to the stash
242 $ edit/build/test first part
243 $ git commit -m 'First part' # commit fully tested change
244 $ git stash pop # prepare to work on all other changes
245 # ... repeat above five steps until one commit remains ...
246 $ edit/build/test remaining parts
247 $ git commit foo -m 'Remaining parts'
248
249
250 Recovering stash entries that were cleared/dropped erroneously
251 If you mistakenly drop or clear stash entries, they cannot be
252 recovered through the normal safety mechanisms. However, you can
253 try the following incantation to get a list of stash entries that
254 are still in your repository, but not reachable any more:
255
256 git fsck --unreachable |
257 grep commit | cut -d\ -f3 |
258 xargs git log --merges --no-walk --grep=WIP
259
260
262 git-checkout(1), git-commit(1), git-reflog(1), git-reset(1)
263
265 Part of the git(1) suite
266
267
268
269Git 2.18.1 05/14/2019 GIT-STASH(1)