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 [save [--patch] [-k|--[no-]keep-index] [-q|--quiet]
15 [-u|--include-untracked] [-a|--all] [<message>]]
16 git stash clear
17 git stash create
18
19
21 Use git stash when you want to record the current state of the working
22 directory and the index, but want to go back to a clean working
23 directory. The command saves your local modifications away and reverts
24 the working directory to match the HEAD commit.
25
26 The modifications stashed away by this command can be listed with git
27 stash list, inspected with git stash show, and restored (potentially on
28 top of a different commit) with git stash apply. Calling git stash
29 without any arguments is equivalent to git stash save. A stash is by
30 default listed as "WIP on branchname ...", but you can give a more
31 descriptive message on the command line when you create one.
32
33 The latest stash you created is stored in refs/stash; older stashes are
34 found in the reflog of this reference and can be named using the usual
35 reflog syntax (e.g. stash@{0} is the most recently created stash,
36 stash@{1} is the one before it, stash@{2.hours.ago} is also possible).
37
39 save [-p|--patch] [--[no-]keep-index] [-u|--include-untracked]
40 [-a|--all] [-q|--quiet] [<message>]
41 Save your local modifications to a new stash, and run git reset
42 --hard to revert them. The <message> part is optional and gives the
43 description along with the stashed state. For quickly making a
44 snapshot, you can omit both "save" and <message>, but giving only
45 <message> does not trigger this action to prevent a misspelled
46 subcommand from making an unwanted stash.
47
48 If the --keep-index option is used, all changes already added to
49 the index are left intact.
50
51 If the --include-untracked option is used, all untracked files are
52 also stashed and then cleaned up with git clean, leaving the
53 working directory in a very clean state. If the --all option is
54 used instead then the ignored files are stashed and cleaned in
55 addition to the untracked files.
56
57 With --patch, you can interactively select hunks from the diff
58 between HEAD and the working tree to be stashed. The stash entry is
59 constructed such that its index state is the same as the index
60 state of your repository, and its worktree contains only the
61 changes you selected interactively. The selected changes are then
62 rolled back from your worktree. See the “Interactive Mode” section
63 of git-add(1) to learn how to operate the --patch mode.
64
65 The --patch option implies --keep-index. You can use
66 --no-keep-index to override this.
67
68 list [<options>]
69 List the stashes that you currently have. Each stash is listed with
70 its name (e.g. stash@{0} is the latest stash, stash@{1} is the one
71 before, etc.), the name of the branch that was current when the
72 stash was made, and a short description of the commit the stash was
73 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 [<stash>]
82 Show the changes recorded in the stash as a diff between the
83 stashed state and its original parent. When no <stash> is given,
84 shows the latest one. By default, the command shows the diffstat,
85 but it will accept any format known to git diff (e.g., git stash
86 show -p stash@{1} to view the second most recent stash in patch
87 form).
88
89 pop [--index] [-q|--quiet] [<stash>]
90 Remove a single stashed state from the stash list and apply it on
91 top of the current working tree state, i.e., do the inverse
92 operation of git stash save. The working directory must match the
93 index.
94
95 Applying the state can fail with conflicts; in this case, it is not
96 removed from the stash list. You need to resolve the conflicts by
97 hand and call git stash drop manually afterwards.
98
99 If the --index option is used, then tries to reinstate not only the
100 working tree’s changes, but also the index’s ones. However, this
101 can fail, when you have conflicts (which are stored in the index,
102 where you therefore can no longer apply the changes as they were
103 originally).
104
105 When no <stash> is given, stash@{0} is assumed, otherwise <stash>
106 must be a reference of the form stash@{<revision>}.
107
108 apply [--index] [-q|--quiet] [<stash>]
109 Like pop, but do not remove the state from the stash list. Unlike
110 pop, <stash> may be any commit that looks like a commit created by
111 stash save or stash create.
112
113 branch <branchname> [<stash>]
114 Creates and checks out a new branch named <branchname> starting
115 from the commit at which the <stash> was originally created,
116 applies the changes recorded in <stash> to the new working tree and
117 index. If that succeeds, and <stash> is a reference of the form
118 stash@{<revision>}, it then drops the <stash>. When no <stash> is
119 given, applies the latest one.
120
121 This is useful if the branch on which you ran git stash save has
122 changed enough that git stash apply fails due to conflicts. Since
123 the stash is applied on top of the commit that was HEAD at the time
124 git stash was run, it restores the originally stashed state with no
125 conflicts.
126
127 clear
128 Remove all the stashed states. Note that those states will then be
129 subject to pruning, and may be impossible to recover (see Examples
130 below for a possible strategy).
131
132 drop [-q|--quiet] [<stash>]
133 Remove a single stashed state from the stash list. When no <stash>
134 is given, it removes the latest one. i.e. stash@{0}, otherwise
135 <stash> must be a valid stash log reference of the form
136 stash@{<revision>}.
137
138 create
139 Create a stash (which is a regular commit object) and return its
140 object name, without storing it anywhere in the ref namespace.
141
143 A stash is represented as a commit whose tree records the state of the
144 working directory, and its first parent is the commit at HEAD when the
145 stash was created. The tree of the second parent records the state of
146 the index when the stash is made, and it is made a child of the HEAD
147 commit. The ancestry graph looks like this:
148
149 .----W
150 / /
151 -----H----I
152
153 where H is the HEAD commit, I is a commit that records the state of the
154 index, and W is a commit that records the state of the working tree.
155
157 Pulling into a dirty tree
158 When you are in the middle of something, you learn that there are
159 upstream changes that are possibly relevant to what you are doing.
160 When your local changes do not conflict with the changes in the
161 upstream, a simple git pull will let you move forward.
162
163 However, there are cases in which your local changes do conflict
164 with the upstream changes, and git pull refuses to overwrite your
165 changes. In such a case, you can stash your changes away, perform a
166 pull, and then unstash, like this:
167
168 $ git pull
169 ...
170 file foobar not up to date, cannot merge.
171 $ git stash
172 $ git pull
173 $ git stash pop
174
175
176 Interrupted workflow
177 When you are in the middle of something, your boss comes in and
178 demands that you fix something immediately. Traditionally, you
179 would make a commit to a temporary branch to store your changes
180 away, and return to your original branch to make the emergency fix,
181 like this:
182
183 # ... hack hack hack ...
184 $ git checkout -b my_wip
185 $ git commit -a -m "WIP"
186 $ git checkout master
187 $ edit emergency fix
188 $ git commit -a -m "Fix in a hurry"
189 $ git checkout my_wip
190 $ git reset --soft HEAD^
191 # ... continue hacking ...
192
193 You can use git stash to simplify the above, like this:
194
195 # ... hack hack hack ...
196 $ git stash
197 $ edit emergency fix
198 $ git commit -a -m "Fix in a hurry"
199 $ git stash pop
200 # ... continue hacking ...
201
202
203 Testing partial commits
204 You can use git stash save --keep-index when you want to make two
205 or more commits out of the changes in the work tree, and you want
206 to test each change before committing:
207
208 # ... hack hack hack ...
209 $ git add --patch foo # add just first part to the index
210 $ git stash save --keep-index # save all other changes to the stash
211 $ edit/build/test first part
212 $ git commit -m 'First part' # commit fully tested change
213 $ git stash pop # prepare to work on all other changes
214 # ... repeat above five steps until one commit remains ...
215 $ edit/build/test remaining parts
216 $ git commit foo -m 'Remaining parts'
217
218
219 Recovering stashes that were cleared/dropped erroneously
220 If you mistakenly drop or clear stashes, they cannot be recovered
221 through the normal safety mechanisms. However, you can try the
222 following incantation to get a list of stashes that are still in
223 your repository, but not reachable any more:
224
225 git fsck --unreachable |
226 grep commit | cut -d\ -f3 |
227 xargs git log --merges --no-walk --grep=WIP
228
229
231 git-checkout(1), git-commit(1), git-reflog(1), git-reset(1)
232
234 Part of the git(1) suite
235
236
237
238Git 1.8.3.1 11/19/2018 GIT-STASH(1)