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, otherwise <stash>
97 must be a reference of the form stash@{<revision>}.
98
99 apply [--index] [-q|--quiet] [<stash>]
100 Like pop, but do not remove the state from the stash list. Unlike
101 pop, <stash> may be any commit that looks like a commit created by
102 stash save or stash create.
103
104 branch <branchname> [<stash>]
105 Creates and checks out a new branch named <branchname> starting
106 from the commit at which the <stash> was originally created,
107 applies the changes recorded in <stash> to the new working tree and
108 index. If that succeeds, and <stash> is a reference of the form
109 stash@{<revision>}, it then drops the <stash>. When no <stash> is
110 given, applies the latest one.
111
112 This is useful if the branch on which you ran git stash save has
113 changed enough that git stash apply fails due to conflicts. Since
114 the stash is applied on top of the commit that was HEAD at the time
115 git stash was run, it restores the originally stashed state with no
116 conflicts.
117
118 clear
119 Remove all the stashed states. Note that those states 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 stashed state from the stash list. When no <stash>
125 is given, it removes the latest one. i.e. stash@{0}, otherwise
126 <stash> must a valid stash log reference of the form
127 stash@{<revision>}.
128
129 create
130 Create a stash (which is a regular commit object) and return its
131 object name, without storing it anywhere in the ref namespace.
132
134 A stash is represented as a commit whose tree records the state of the
135 working directory, and its first parent is the commit at HEAD when the
136 stash was created. The tree of the second parent records the state of
137 the index when the stash is made, and it is made a child of the HEAD
138 commit. The ancestry graph looks like this:
139
140 .----W
141 / /
142 -----H----I
143
144 where H is the HEAD commit, I is a commit that records the state of the
145 index, and W is a commit that records the state of the working tree.
146
148 Pulling into a dirty tree
149 When you are in the middle of something, you learn that there are
150 upstream changes that are possibly relevant to what you are doing.
151 When your local changes do not conflict with the changes in the
152 upstream, a simple git pull will let you move forward.
153
154 However, there are cases in which your local changes do conflict
155 with the upstream changes, and git pull refuses to overwrite your
156 changes. In such a case, you can stash your changes away, perform a
157 pull, and then unstash, like this:
158
159 $ git pull
160 ...
161 file foobar not up to date, cannot merge.
162 $ git stash
163 $ git pull
164 $ git stash pop
165
166
167 Interrupted workflow
168 When you are in the middle of something, your boss comes in and
169 demands that you fix something immediately. Traditionally, you
170 would make a commit to a temporary branch to store your changes
171 away, and return to your original branch to make the emergency fix,
172 like this:
173
174 # ... hack hack hack ...
175 $ git checkout -b my_wip
176 $ git commit -a -m "WIP"
177 $ git checkout master
178 $ edit emergency fix
179 $ git commit -a -m "Fix in a hurry"
180 $ git checkout my_wip
181 $ git reset --soft HEAD^
182 # ... continue hacking ...
183
184 You can use git stash to simplify the above, like this:
185
186 # ... hack hack hack ...
187 $ git stash
188 $ edit emergency fix
189 $ git commit -a -m "Fix in a hurry"
190 $ git stash pop
191 # ... continue hacking ...
192
193
194 Testing partial commits
195 You can use git stash save --keep-index when you want to make two
196 or more commits out of the changes in the work tree, and you want
197 to test each change before committing:
198
199 # ... hack hack hack ...
200 $ git add --patch foo # add just first part to the index
201 $ git stash save --keep-index # save all other changes to the stash
202 $ edit/build/test first part
203 $ git commit -m 'First part' # commit fully tested change
204 $ git stash pop # prepare to work on all other changes
205 # ... repeat above five steps until one commit remains ...
206 $ edit/build/test remaining parts
207 $ git commit foo -m 'Remaining parts'
208
209
210 Recovering stashes that were cleared/dropped erroneously
211 If you mistakenly drop or clear stashes, they cannot be recovered
212 through the normal safety mechanisms. However, you can try the
213 following incantation to get a list of stashes that are still in
214 your repository, but not reachable any more:
215
216 git fsck --unreachable |
217 grep commit | cut -d\ -f3 |
218 xargs git log --merges --no-walk --grep=WIP
219
220
222 git-checkout(1), git-commit(1), git-reflog(1), git-reset(1)
223
225 Written by Nanako Shiraishi <nanako3@bluebottle.com[1]>
226
228 Part of the git(1) suite
229
231 1. nanako3@bluebottle.com
232 mailto:nanako3@bluebottle.com
233
234
235
236Git 1.7.4.4 04/11/2011 GIT-STASH(1)