]> git.ipfire.org Git - thirdparty/git.git/blame - Documentation/git-stash.txt
stash: introduce 'stash save --keep-index' option
[thirdparty/git.git] / Documentation / git-stash.txt
CommitLineData
09ccdb63
NS
1git-stash(1)
2============
3
4NAME
5----
6git-stash - Stash the changes in a dirty working directory away
7
8SYNOPSIS
9--------
10[verse]
b1889c36
JN
11'git stash' (list | show [<stash>] | apply [<stash>] | clear | drop [<stash>] | pop [<stash>])
12'git stash' [save [<message>]]
09ccdb63
NS
13
14DESCRIPTION
15-----------
16
b1889c36 17Use 'git stash' when you want to record the current state of the
09ccdb63
NS
18working directory and the index, but want to go back to a clean
19working directory. The command saves your local modifications away
20and reverts the working directory to match the `HEAD` commit.
21
22The modifications stashed away by this command can be listed with
483bc4f0
JN
23`git stash list`, inspected with `git stash show`, and restored
24(potentially on top of a different commit) with `git stash apply`.
25Calling `git stash` without any arguments is equivalent to `git stash save`.
26A stash is by default listed as "WIP on 'branchname' ...", but
ec96e0f6
NS
27you can give a more descriptive message on the command line when
28you create one.
09ccdb63
NS
29
30The latest stash you created is stored in `$GIT_DIR/refs/stash`; older
9488e875 31stashes are found in the reflog of this reference and can be named using
e2c6de1c
SH
32the usual reflog syntax (e.g. `stash@\{0}` is the most recently
33created stash, `stash@\{1}` is the one before it, `stash@\{2.hours.ago}`
9488e875 34is also possible).
09ccdb63
NS
35
36OPTIONS
37-------
38
7bedebca 39save [--keep-index] [<message>]::
09ccdb63 40
b1889c36 41 Save your local modifications to a new 'stash', and run `git reset
fcb10a96 42 --hard` to revert them. This is the default action when no
71bda8b9
JA
43 subcommand is given. The <message> part is optional and gives
44 the description along with the stashed state.
7bedebca
SG
45+
46If the `--keep-index` option is used, all changes already added to the
47index are left intact.
09ccdb63 48
fbd538c2 49list [<options>]::
09ccdb63
NS
50
51 List the stashes that you currently have. Each 'stash' is listed
36717575 52 with its name (e.g. `stash@\{0}` is the latest stash, `stash@\{1}` is
9488e875 53 the one before, etc.), the name of the branch that was current when the
09ccdb63
NS
54 stash was made, and a short description of the commit the stash was
55 based on.
56+
57----------------------------------------------------------------
ec96e0f6
NS
58stash@{0}: WIP on submit: 6ebd0e2... Update git-stash documentation
59stash@{1}: On master: 9cc0589... Add git-stash
09ccdb63 60----------------------------------------------------------------
fbd538c2 61+
483bc4f0
JN
62The command takes options applicable to the `git-log`
63command to control what is shown and how. See linkgit:git-log[1].
09ccdb63
NS
64
65show [<stash>]::
66
06ada152 67 Show the changes recorded in the stash as a diff between the
9488e875
JH
68 stashed state and its original parent. When no `<stash>` is given,
69 shows the latest one. By default, the command shows the diffstat, but
b1889c36 70 it will accept any format known to `git-diff` (e.g., `git stash show
e2c6de1c 71 -p stash@\{1}` to view the second most recent stash in patch form).
09ccdb63 72
0bdcac56 73apply [--index] [<stash>]::
09ccdb63 74
9488e875 75 Restore the changes recorded in the stash on top of the current
09ccdb63 76 working tree state. When no `<stash>` is given, applies the latest
9488e875
JH
77 one. The working directory must match the index.
78+
79This operation can fail with conflicts; you need to resolve them
80by hand in the working tree.
0bdcac56
MV
81+
82If the `--index` option is used, then tries to reinstate not only the working
83tree's changes, but also the index's ones. However, this can fail, when you
84have conflicts (which are stored in the index, where you therefore can no
85longer apply the changes as they were originally).
09ccdb63
NS
86
87clear::
9488e875
JH
88 Remove all the stashed states. Note that those states will then
89 be subject to pruning, and may be difficult or impossible to recover.
09ccdb63 90
e25d5f9c
BC
91drop [<stash>]::
92
93 Remove a single stashed state from the stash list. When no `<stash>`
94 is given, it removes the latest one. i.e. `stash@\{0}`
95
bd56ff54
BC
96pop [<stash>]::
97
98 Remove a single stashed state from the stash list and apply on top
99 of the current working tree state. When no `<stash>` is given,
100 `stash@\{0}` is assumed. See also `apply`.
101
09ccdb63
NS
102
103DISCUSSION
104----------
105
106A stash is represented as a commit whose tree records the state of the
107working directory, and its first parent is the commit at `HEAD` when
108the stash was created. The tree of the second parent records the
109state of the index when the stash is made, and it is made a child of
110the `HEAD` commit. The ancestry graph looks like this:
111
112 .----W
113 / /
114fd812 114 -----H----I
09ccdb63
NS
115
116where `H` is the `HEAD` commit, `I` is a commit that records the state
117of the index, and `W` is a commit that records the state of the working
118tree.
119
120
121EXAMPLES
122--------
123
124Pulling into a dirty tree::
125
126When you are in the middle of something, you learn that there are
9488e875
JH
127upstream changes that are possibly relevant to what you are
128doing. When your local changes do not conflict with the changes in
09ccdb63
NS
129the upstream, a simple `git pull` will let you move forward.
130+
131However, there are cases in which your local changes do conflict with
132the upstream changes, and `git pull` refuses to overwrite your
9488e875 133changes. In such a case, you can stash your changes away,
09ccdb63
NS
134perform a pull, and then unstash, like this:
135+
136----------------------------------------------------------------
137$ git pull
138...
139file foobar not up to date, cannot merge.
140$ git stash
141$ git pull
142$ git stash apply
143----------------------------------------------------------------
144
145Interrupted workflow::
146
147When you are in the middle of something, your boss comes in and
9488e875 148demands that you fix something immediately. Traditionally, you would
09ccdb63 149make a commit to a temporary branch to store your changes away, and
9488e875 150return to your original branch to make the emergency fix, like this:
09ccdb63
NS
151+
152----------------------------------------------------------------
153... hack hack hack ...
154$ git checkout -b my_wip
155$ git commit -a -m "WIP"
156$ git checkout master
157$ edit emergency fix
158$ git commit -a -m "Fix in a hurry"
159$ git checkout my_wip
160$ git reset --soft HEAD^
161... continue hacking ...
162----------------------------------------------------------------
163+
164You can use `git-stash` to simplify the above, like this:
165+
166----------------------------------------------------------------
167... hack hack hack ...
168$ git stash
169$ edit emergency fix
170$ git commit -a -m "Fix in a hurry"
171$ git stash apply
172... continue hacking ...
173----------------------------------------------------------------
174
7bedebca
SG
175Testing partial commits::
176
177You can use `git stash save --keep-index` when you want to make two or
178more commits out of the changes in the work tree, and you want to test
179each change before committing:
180+
181----------------------------------------------------------------
182... hack hack hack ...
183$ git add --patch foo
184$ git stash save --keep-index
185$ build && run tests
186$ git commit -m 'First part'
187$ git stash apply
188$ build && run tests
189$ git commit -a -m 'Second part'
190----------------------------------------------------------------
191
09ccdb63
NS
192SEE ALSO
193--------
5162e697
DM
194linkgit:git-checkout[1],
195linkgit:git-commit[1],
196linkgit:git-reflog[1],
197linkgit:git-reset[1]
09ccdb63
NS
198
199AUTHOR
200------
201Written by Nanako Shiraishi <nanako3@bluebottle.com>
202
203GIT
204---
9e1f0a85 205Part of the linkgit:git[1] suite