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