blob: 711ffe17a7574511de6c20cb85b6cdd5a4fff5a5 [file] [log] [blame]
Junio C Hamano1d90cb02007-07-03 07:05:311git-stash(1)
2============
3
4NAME
5----
6git-stash - Stash the changes in a dirty working directory away
7
8SYNOPSIS
9--------
10[verse]
Junio C Hamano9e96a1a2008-08-16 10:23:1811'git stash' list [<options>]
Junio C Hamano2c14c8d2009-07-02 03:17:0012'git stash' show [<stash>]
13'git stash' drop [-q|--quiet] [<stash>]
14'git stash' ( pop | apply ) [--index] [-q|--quiet] [<stash>]
Junio C Hamano46a38aa2008-07-14 03:13:2715'git stash' branch <branchname> [<stash>]
Junio C Hamano15567bc2011-07-23 00:51:5916'git stash' [save [--patch] [-k|--[no-]keep-index] [-q|--quiet]
17 [-u|--include-untracked] [-a|--all] [<message>]]
Junio C Hamano46a38aa2008-07-14 03:13:2718'git stash' clear
Junio C Hamano9e96a1a2008-08-16 10:23:1819'git stash' create
Junio C Hamano1d90cb02007-07-03 07:05:3120
21DESCRIPTION
22-----------
23
Junio C Hamano1aa40d22010-01-21 17:46:4324Use `git stash` when you want to record the current state of the
Junio C Hamano1d90cb02007-07-03 07:05:3125working directory and the index, but want to go back to a clean
26working directory. The command saves your local modifications away
27and reverts the working directory to match the `HEAD` commit.
28
29The modifications stashed away by this command can be listed with
Junio C Hamanofce7c7e2008-07-02 03:06:3830`git stash list`, inspected with `git stash show`, and restored
31(potentially on top of a different commit) with `git stash apply`.
32Calling `git stash` without any arguments is equivalent to `git stash save`.
33A stash is by default listed as "WIP on 'branchname' ...", but
Junio C Hamanobb6e0782007-07-19 02:14:5034you can give a more descriptive message on the command line when
35you create one.
Junio C Hamano1d90cb02007-07-03 07:05:3136
Junio C Hamano4fdccb22010-02-19 09:58:1437The latest stash you created is stored in `refs/stash`; older
Junio C Hamano1d90cb02007-07-03 07:05:3138stashes are found in the reflog of this reference and can be named using
Junio C Hamanob76a6862012-05-02 22:02:4639the usual reflog syntax (e.g. `stash@{0}` is the most recently
40created stash, `stash@{1}` is the one before it, `stash@{2.hours.ago}`
Junio C Hamano1d90cb02007-07-03 07:05:3141is also possible).
42
43OPTIONS
44-------
45
Junio C Hamano15567bc2011-07-23 00:51:5946save [-p|--patch] [--[no-]keep-index] [-u|--include-untracked] [-a|--all] [-q|--quiet] [<message>]::
Junio C Hamano1d90cb02007-07-03 07:05:3147
Junio C Hamanofce7c7e2008-07-02 03:06:3848Save your local modifications to a new 'stash', and run `git reset
Junio C Hamanoc28068f2009-09-08 00:55:2349--hard` to revert them. The <message> part is optional and gives
50the description along with the stashed state. For quickly making
51a snapshot, you can omit _both_ "save" and <message>, but giving
52only <message> does not trigger this action to prevent a misspelled
53subcommand from making an unwanted stash.
Junio C Hamano46a38aa2008-07-14 03:13:2754+
55If the `--keep-index` option is used, all changes already added to the
56index are left intact.
Junio C Hamanoc28068f2009-09-08 00:55:2357+
Junio C Hamano15567bc2011-07-23 00:51:5958If the `--include-untracked` option is used, all untracked files are also
59stashed and then cleaned up with `git clean`, leaving the working directory
60in a very clean state. If the `--all` option is used instead then the
61ignored files are stashed and cleaned in addition to the untracked files.
62+
Junio C Hamano9eb58352011-05-11 20:09:2463With `--patch`, you can interactively select hunks from the diff
Junio C Hamanoc28068f2009-09-08 00:55:2364between HEAD and the working tree to be stashed. The stash entry is
65constructed such that its index state is the same as the index state
66of your repository, and its worktree contains only the changes you
67selected interactively. The selected changes are then rolled back
Junio C Hamano9eb58352011-05-11 20:09:2468from your worktree. See the ``Interactive Mode'' section of
Junio C Hamanob76a6862012-05-02 22:02:4669linkgit:git-add[1] to learn how to operate the `--patch` mode.
Junio C Hamanoc28068f2009-09-08 00:55:2370+
71The `--patch` option implies `--keep-index`. You can use
72`--no-keep-index` to override this.
Junio C Hamano1d90cb02007-07-03 07:05:3173
Junio C Hamano83d46a22008-02-21 02:29:4274list [<options>]::
Junio C Hamano1d90cb02007-07-03 07:05:3175
76List the stashes that you currently have. Each 'stash' is listed
Junio C Hamanob76a6862012-05-02 22:02:4677with its name (e.g. `stash@{0}` is the latest stash, `stash@{1}` is
Junio C Hamano1d90cb02007-07-03 07:05:3178the one before, etc.), the name of the branch that was current when the
79stash was made, and a short description of the commit the stash was
80based on.
81+
82----------------------------------------------------------------
Junio C Hamanobb6e0782007-07-19 02:14:5083stash@{0}: WIP on submit: 6ebd0e2... Update git-stash documentation
84stash@{1}: On master: 9cc0589... Add git-stash
Junio C Hamano1d90cb02007-07-03 07:05:3185----------------------------------------------------------------
Junio C Hamano83d46a22008-02-21 02:29:4286+
Junio C Hamano1aa40d22010-01-21 17:46:4387The command takes options applicable to the 'git log'
Junio C Hamanoc21ab052009-10-31 04:03:5588command to control what is shown and how. See linkgit:git-log[1].
Junio C Hamano1d90cb02007-07-03 07:05:3189
90show [<stash>]::
91
Junio C Hamano764a6672007-10-23 01:23:3192Show the changes recorded in the stash as a diff between the
Junio C Hamano1d90cb02007-07-03 07:05:3193stashed state and its original parent. When no `<stash>` is given,
94shows the latest one. By default, the command shows the diffstat, but
Junio C Hamano1aa40d22010-01-21 17:46:4395it will accept any format known to 'git diff' (e.g., `git stash show
Junio C Hamanob76a6862012-05-02 22:02:4696-p stash@{1}` to view the second most recent stash in patch form).
Junio C Hamano1d90cb02007-07-03 07:05:3197
Junio C Hamano2c14c8d2009-07-02 03:17:0098pop [--index] [-q|--quiet] [<stash>]::
Junio C Hamano7d3275e2009-05-31 21:53:3099
100Remove a single stashed state from the stash list and apply it
101on top of the current working tree state, i.e., do the inverse
102operation of `git stash save`. The working directory must
103match the index.
104+
105Applying the state can fail with conflicts; in this case, it is not
106removed from the stash list. You need to resolve the conflicts by hand
107and call `git stash drop` manually afterwards.
108+
Junio C Hamano58992c92007-10-01 16:23:18109If the `--index` option is used, then tries to reinstate not only the working
110tree's changes, but also the index's ones. However, this can fail, when you
111have conflicts (which are stored in the index, where you therefore can no
112longer apply the changes as they were originally).
Junio C Hamanof068d542009-06-09 15:46:41113+
Junio C Hamanob76a6862012-05-02 22:02:46114When no `<stash>` is given, `stash@{0}` is assumed, otherwise `<stash>` must
115be a reference of the form `stash@{<revision>}`.
Junio C Hamanof068d542009-06-09 15:46:41116
Junio C Hamano2c14c8d2009-07-02 03:17:00117apply [--index] [-q|--quiet] [<stash>]::
Junio C Hamanof068d542009-06-09 15:46:41118
Junio C Hamanof0f41ba2010-09-04 16:23:06119Like `pop`, but do not remove the state from the stash list. Unlike `pop`,
120`<stash>` may be any commit that looks like a commit created by
121`stash save` or `stash create`.
Junio C Hamano1d90cb02007-07-03 07:05:31122
Junio C Hamano46a38aa2008-07-14 03:13:27123branch <branchname> [<stash>]::
124
125Creates and checks out a new branch named `<branchname>` starting from
126the commit at which the `<stash>` was originally created, applies the
Junio C Hamanof0f41ba2010-09-04 16:23:06127changes recorded in `<stash>` to the new working tree and index.
128If that succeeds, and `<stash>` is a reference of the form
129`stash@{<revision>}`, it then drops the `<stash>`. When no `<stash>`
Junio C Hamano46a38aa2008-07-14 03:13:27130is given, applies the latest one.
131+
132This is useful if the branch on which you ran `git stash save` has
133changed enough that `git stash apply` fails due to conflicts. Since
134the stash is applied on top of the commit that was HEAD at the time
135`git stash` was run, it restores the originally stashed state with
136no conflicts.
137
Junio C Hamano1d90cb02007-07-03 07:05:31138clear::
139Remove all the stashed states. Note that those states will then
Junio C Hamano9f883862009-08-11 06:23:52140be subject to pruning, and may be impossible to recover (see
141'Examples' below for a possible strategy).
Junio C Hamano1d90cb02007-07-03 07:05:31142
Junio C Hamano2c14c8d2009-07-02 03:17:00143drop [-q|--quiet] [<stash>]::
Junio C Hamano86bcccc2008-03-08 09:33:55144
145Remove a single stashed state from the stash list. When no `<stash>`
Junio C Hamanob76a6862012-05-02 22:02:46146is given, it removes the latest one. i.e. `stash@{0}`, otherwise
Junio C Hamano2d7c5de2012-12-10 22:17:38147`<stash>` must be a valid stash log reference of the form
Junio C Hamanob76a6862012-05-02 22:02:46148`stash@{<revision>}`.
Junio C Hamano86bcccc2008-03-08 09:33:55149
Junio C Hamano9e96a1a2008-08-16 10:23:18150create::
151
152Create a stash (which is a regular commit object) and return its
153object name, without storing it anywhere in the ref namespace.
154
Junio C Hamano1d90cb02007-07-03 07:05:31155
156DISCUSSION
157----------
158
159A stash is represented as a commit whose tree records the state of the
160working directory, and its first parent is the commit at `HEAD` when
161the stash was created. The tree of the second parent records the
162state of the index when the stash is made, and it is made a child of
163the `HEAD` commit. The ancestry graph looks like this:
164
165 .----W
166 / /
Junio C Hamano28d8ede2007-07-05 05:51:38167 -----H----I
Junio C Hamano1d90cb02007-07-03 07:05:31168
169where `H` is the `HEAD` commit, `I` is a commit that records the state
170of the index, and `W` is a commit that records the state of the working
171tree.
172
173
174EXAMPLES
175--------
176
177Pulling into a dirty tree::
178
179When you are in the middle of something, you learn that there are
180upstream changes that are possibly relevant to what you are
181doing. When your local changes do not conflict with the changes in
182the upstream, a simple `git pull` will let you move forward.
183+
184However, there are cases in which your local changes do conflict with
185the upstream changes, and `git pull` refuses to overwrite your
186changes. In such a case, you can stash your changes away,
187perform a pull, and then unstash, like this:
188+
189----------------------------------------------------------------
190$ git pull
Junio C Hamanoa2f46622008-09-03 05:20:31191 ...
Junio C Hamano1d90cb02007-07-03 07:05:31192file foobar not up to date, cannot merge.
193$ git stash
194$ git pull
Junio C Hamano7d3275e2009-05-31 21:53:30195$ git stash pop
Junio C Hamano1d90cb02007-07-03 07:05:31196----------------------------------------------------------------
197
198Interrupted workflow::
199
200When you are in the middle of something, your boss comes in and
201demands that you fix something immediately. Traditionally, you would
202make a commit to a temporary branch to store your changes away, and
203return to your original branch to make the emergency fix, like this:
204+
205----------------------------------------------------------------
Junio C Hamanoa2f46622008-09-03 05:20:31206# ... hack hack hack ...
Junio C Hamano1d90cb02007-07-03 07:05:31207$ git checkout -b my_wip
208$ git commit -a -m "WIP"
209$ git checkout master
210$ edit emergency fix
211$ git commit -a -m "Fix in a hurry"
212$ git checkout my_wip
213$ git reset --soft HEAD^
Junio C Hamanoa2f46622008-09-03 05:20:31214# ... continue hacking ...
Junio C Hamano1d90cb02007-07-03 07:05:31215----------------------------------------------------------------
216+
Junio C Hamano1aa40d22010-01-21 17:46:43217You can use 'git stash' to simplify the above, like this:
Junio C Hamano1d90cb02007-07-03 07:05:31218+
219----------------------------------------------------------------
Junio C Hamanoa2f46622008-09-03 05:20:31220# ... hack hack hack ...
Junio C Hamano1d90cb02007-07-03 07:05:31221$ git stash
222$ edit emergency fix
223$ git commit -a -m "Fix in a hurry"
Junio C Hamano7d3275e2009-05-31 21:53:30224$ git stash pop
Junio C Hamanoa2f46622008-09-03 05:20:31225# ... continue hacking ...
Junio C Hamano1d90cb02007-07-03 07:05:31226----------------------------------------------------------------
227
Junio C Hamano46a38aa2008-07-14 03:13:27228Testing partial commits::
229
230You can use `git stash save --keep-index` when you want to make two or
231more commits out of the changes in the work tree, and you want to test
232each change before committing:
233+
234----------------------------------------------------------------
Junio C Hamanoa2f46622008-09-03 05:20:31235# ... hack hack hack ...
Junio C Hamano46a38aa2008-07-14 03:13:27236$ git add --patch foo # add just first part to the index
237$ git stash save --keep-index # save all other changes to the stash
238$ edit/build/test first part
Junio C Hamanoa2f46622008-09-03 05:20:31239$ git commit -m 'First part' # commit fully tested change
Junio C Hamano46a38aa2008-07-14 03:13:27240$ git stash pop # prepare to work on all other changes
Junio C Hamanoa2f46622008-09-03 05:20:31241# ... repeat above five steps until one commit remains ...
Junio C Hamano46a38aa2008-07-14 03:13:27242$ edit/build/test remaining parts
243$ git commit foo -m 'Remaining parts'
244----------------------------------------------------------------
245
Junio C Hamano9f883862009-08-11 06:23:52246Recovering stashes that were cleared/dropped erroneously::
247
248If you mistakenly drop or clear stashes, they cannot be recovered
249through the normal safety mechanisms. However, you can try the
250following incantation to get a list of stashes that are still in your
251repository, but not reachable any more:
252+
253----------------------------------------------------------------
254git fsck --unreachable |
255grep commit | cut -d\ -f3 |
256xargs git log --merges --no-walk --grep=WIP
257----------------------------------------------------------------
258
259
Junio C Hamano1d90cb02007-07-03 07:05:31260SEE ALSO
261--------
Junio C Hamano35738e82008-01-07 07:55:46262linkgit:git-checkout[1],
263linkgit:git-commit[1],
264linkgit:git-reflog[1],
265linkgit:git-reset[1]
Junio C Hamano1d90cb02007-07-03 07:05:31266
Junio C Hamano1d90cb02007-07-03 07:05:31267GIT
268---
Junio C Hamanof7c042d2008-06-06 22:50:53269Part of the linkgit:git[1] suite