blob: 31f1beb65baf213908e93d0e90c379d9fb0e7d84 [file] [log] [blame]
しらいしななこ09ccdb62007-07-01 14:26:08 +09001git-stash(1)
2============
3
4NAME
5----
6git-stash - Stash the changes in a dirty working directory away
7
8SYNOPSIS
9--------
10[verse]
Stephan Beyera5ab00c2008-08-16 05:27:31 +020011'git stash' list [<options>]
Paul-Sebastian Ungureanubef55dc2019-02-25 23:16:14 +000012'git stash' show [<options>] [<stash>]
Stephen Boydfcdd0e92009-06-17 18:07:37 -070013'git stash' drop [-q|--quiet] [<stash>]
14'git stash' ( pop | apply ) [--index] [-q|--quiet] [<stash>]
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +053015'git stash' branch <branchname> [<stash>]
Thomas Gummerer1ada5022017-02-28 20:33:39 +000016'git stash' [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
Thomas Gummerer0a790f02018-03-26 22:11:47 +010017 [-u|--include-untracked] [-a|--all] [-m|--message <message>]
Alexandr Miloslavskiy8a987582020-02-17 17:25:22 +000018 [--pathspec-from-file=<file> [--pathspec-file-nul]]
Thomas Gummerer1ada5022017-02-28 20:33:39 +000019 [--] [<pathspec>...]]
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +053020'git stash' clear
Ramkumar Ramachandra2be43512013-06-15 18:43:22 +053021'git stash' create [<message>]
Ramkumar Ramachandrabd514ca2013-06-15 18:43:25 +053022'git stash' store [-m|--message <message>] [-q|--quiet] <commit>
しらいしななこ09ccdb62007-07-01 14:26:08 +090023
24DESCRIPTION
25-----------
26
Thomas Rastca768282010-01-07 17:49:12 +010027Use `git stash` when you want to record the current state of the
しらいしななこ09ccdb62007-07-01 14:26:08 +090028working directory and the index, but want to go back to a clean
29working directory. The command saves your local modifications away
30and reverts the working directory to match the `HEAD` commit.
31
32The modifications stashed away by this command can be listed with
Jonathan Nieder483bc4f2008-06-30 13:56:34 -050033`git stash list`, inspected with `git stash show`, and restored
34(potentially on top of a different commit) with `git stash apply`.
Thomas Gummererdb377452017-10-22 18:04:07 +010035Calling `git stash` without any arguments is equivalent to `git stash push`.
Jonathan Nieder483bc4f2008-06-30 13:56:34 -050036A stash is by default listed as "WIP on 'branchname' ...", but
しらいしななこec96e0f2007-07-17 17:15:42 +090037you can give a more descriptive message on the command line when
38you create one.
しらいしななこ09ccdb62007-07-01 14:26:08 +090039
Jeff Kingcc1b8d82010-02-17 20:16:20 -050040The latest stash you created is stored in `refs/stash`; older
Junio C Hamano9488e872007-07-01 15:29:01 -070041stashes are found in the reflog of this reference and can be named using
Jeff King6cf378f2012-04-26 04:51:57 -040042the usual reflog syntax (e.g. `stash@{0}` is the most recently
43created stash, `stash@{1}` is the one before it, `stash@{2.hours.ago}`
Aaron M Watsona56c8f52016-10-24 19:40:13 -040044is also possible). Stashes may also be referenced by specifying just the
45stash index (e.g. the integer `n` is equivalent to `stash@{n}`).
しらいしななこ09ccdb62007-07-01 14:26:08 +090046
Alexandr Miloslavskiy0093abc2020-02-17 17:25:18 +000047COMMANDS
48--------
しらいしななこ09ccdb62007-07-01 14:26:08 +090049
Alexandr Miloslavskiy8a987582020-02-17 17:25:22 +000050push [-p|--patch] [-k|--[no-]keep-index] [-u|--include-untracked] [-a|--all] [-q|--quiet] [-m|--message <message>] [--pathspec-from-file=<file> [--pathspec-file-nul]] [--] [<pathspec>...]::
しらいしななこ09ccdb62007-07-01 14:26:08 +090051
Liam Beguine01db912017-06-17 18:30:50 -040052 Save your local modifications to a new 'stash entry' and roll them
Thomas Gummerer20a7e062017-02-12 21:54:14 +000053 back to HEAD (in the working tree and in the index).
54 The <message> part is optional and gives
Thomas Gummerer9e140902017-02-28 20:33:40 +000055 the description along with the stashed state.
56+
57For quickly making a snapshot, you can omit "push". In this mode,
58non-option arguments are not allowed to prevent a misspelled
Liam Beguine01db912017-06-17 18:30:50 -040059subcommand from making an unwanted stash entry. The two exceptions to this
Alexandr Miloslavskiy3f3d8062020-02-17 17:25:20 +000060are `stash -p` which acts as alias for `stash push -p` and pathspec elements,
Thomas Gummerer9e140902017-02-28 20:33:40 +000061which are allowed after a double hyphen `--` for disambiguation.
しらいしななこ09ccdb62007-07-01 14:26:08 +090062
Thomas Gummererfd2ebf12017-10-22 18:04:08 +010063save [-p|--patch] [-k|--[no-]keep-index] [-u|--include-untracked] [-a|--all] [-q|--quiet] [<message>]::
64
65 This option is deprecated in favour of 'git stash push'. It
Alexandr Miloslavskiy3f3d8062020-02-17 17:25:20 +000066 differs from "stash push" in that it cannot take pathspec.
Johannes Schindelin57d8f4b2019-10-10 03:06:41 -070067 Instead, all non-option arguments are concatenated to form the stash
68 message.
Thomas Gummererfd2ebf12017-10-22 18:04:08 +010069
Miklos Vajnafbd538c2008-02-20 12:31:35 +010070list [<options>]::
しらいしななこ09ccdb62007-07-01 14:26:08 +090071
Liam Beguine01db912017-06-17 18:30:50 -040072 List the stash entries that you currently have. Each 'stash entry' is
73 listed with its name (e.g. `stash@{0}` is the latest entry, `stash@{1}` is
Junio C Hamano9488e872007-07-01 15:29:01 -070074 the one before, etc.), the name of the branch that was current when the
Liam Beguine01db912017-06-17 18:30:50 -040075 entry was made, and a short description of the commit the entry was
しらいしななこ09ccdb62007-07-01 14:26:08 +090076 based on.
77+
78----------------------------------------------------------------
しらいしななこec96e0f2007-07-17 17:15:42 +090079stash@{0}: WIP on submit: 6ebd0e2... Update git-stash documentation
80stash@{1}: On master: 9cc0589... Add git-stash
しらいしななこ09ccdb62007-07-01 14:26:08 +090081----------------------------------------------------------------
Miklos Vajnafbd538c2008-02-20 12:31:35 +010082+
Thomas Rast0b444cd2010-01-10 00:33:00 +010083The command takes options applicable to the 'git log'
Thomas Rastb7b10382009-10-19 17:48:12 +020084command to control what is shown and how. See linkgit:git-log[1].
しらいしななこ09ccdb62007-07-01 14:26:08 +090085
Paul-Sebastian Ungureanubef55dc2019-02-25 23:16:14 +000086show [<options>] [<stash>]::
しらいしななこ09ccdb62007-07-01 14:26:08 +090087
Liam Beguine01db912017-06-17 18:30:50 -040088 Show the changes recorded in the stash entry as a diff between the
89 stashed contents and the commit back when the stash entry was first
Alexandr Miloslavskiy0093abc2020-02-17 17:25:18 +000090 created.
Liam Beguine01db912017-06-17 18:30:50 -040091 By default, the command shows the diffstat, but it will accept any
92 format known to 'git diff' (e.g., `git stash show -p stash@{1}`
93 to view the second most recent entry in patch form).
Namhyung Kim3086c062015-08-30 00:25:57 +090094 You can use stash.showStat and/or stash.showPatch config variables
95 to change the default behavior.
しらいしななこ09ccdb62007-07-01 14:26:08 +090096
Stephen Boydfcdd0e92009-06-17 18:07:37 -070097pop [--index] [-q|--quiet] [<stash>]::
Thomas Rastd1836632009-05-28 11:40:15 +020098
99 Remove a single stashed state from the stash list and apply it
100 on top of the current working tree state, i.e., do the inverse
Thomas Gummererdb377452017-10-22 18:04:07 +0100101 operation of `git stash push`. The working directory must
Thomas Rastd1836632009-05-28 11:40:15 +0200102 match the index.
103+
104Applying the state can fail with conflicts; in this case, it is not
105removed from the stash list. You need to resolve the conflicts by hand
106and call `git stash drop` manually afterwards.
SZEDER Gáborf39d6ee2009-06-09 00:57:06 +0200107
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700108apply [--index] [-q|--quiet] [<stash>]::
SZEDER Gáborf39d6ee2009-06-09 00:57:06 +0200109
Jon Seymourb0c6bf42010-08-21 14:09:04 +1000110 Like `pop`, but do not remove the state from the stash list. Unlike `pop`,
111 `<stash>` may be any commit that looks like a commit created by
Thomas Gummererdb377452017-10-22 18:04:07 +0100112 `stash push` or `stash create`.
しらいしななこ09ccdb62007-07-01 14:26:08 +0900113
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530114branch <branchname> [<stash>]::
115
116 Creates and checks out a new branch named `<branchname>` starting from
117 the commit at which the `<stash>` was originally created, applies the
Jon Seymourb0c6bf42010-08-21 14:09:04 +1000118 changes recorded in `<stash>` to the new working tree and index.
119 If that succeeds, and `<stash>` is a reference of the form
Alexandr Miloslavskiy0093abc2020-02-17 17:25:18 +0000120 `stash@{<revision>}`, it then drops the `<stash>`.
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530121+
Thomas Gummererdb377452017-10-22 18:04:07 +0100122This is useful if the branch on which you ran `git stash push` has
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530123changed enough that `git stash apply` fails due to conflicts. Since
Liam Beguine01db912017-06-17 18:30:50 -0400124the stash entry is applied on top of the commit that was HEAD at the
125time `git stash` was run, it restores the originally stashed state
126with no conflicts.
Abhijit Menon-Sen656b5032008-07-03 11:46:05 +0530127
しらいしななこ09ccdb62007-07-01 14:26:08 +0900128clear::
Liam Beguine01db912017-06-17 18:30:50 -0400129 Remove all the stash entries. Note that those entries will then
Thomas Rastf5f1e162009-08-09 02:47:36 +0200130 be subject to pruning, and may be impossible to recover (see
131 'Examples' below for a possible strategy).
しらいしななこ09ccdb62007-07-01 14:26:08 +0900132
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700133drop [-q|--quiet] [<stash>]::
Brandon Caseye25d5f92008-02-22 13:04:54 -0600134
Liam Beguine01db912017-06-17 18:30:50 -0400135 Remove a single stash entry from the list of stash entries.
Brandon Caseye25d5f92008-02-22 13:04:54 -0600136
Stephan Beyera5ab00c2008-08-16 05:27:31 +0200137create::
138
Liam Beguine01db912017-06-17 18:30:50 -0400139 Create a stash entry (which is a regular commit object) and
140 return its object name, without storing it anywhere in the ref
141 namespace.
Ramkumar Ramachandra2be43512013-06-15 18:43:22 +0530142 This is intended to be useful for scripts. It is probably not
Phil Hord0d5f8442017-11-21 15:05:23 -0800143 the command you want to use; see "push" above.
Stephan Beyera5ab00c2008-08-16 05:27:31 +0200144
Ramkumar Ramachandrabd514ca2013-06-15 18:43:25 +0530145store::
146
147 Store a given stash created via 'git stash create' (which is a
148 dangling merge commit) in the stash ref, updating the stash
149 reflog. This is intended to be useful for scripts. It is
Phil Hord0d5f8442017-11-21 15:05:23 -0800150 probably not the command you want to use; see "push" above.
しらいしななこ09ccdb62007-07-01 14:26:08 +0900151
Alexandr Miloslavskiy0093abc2020-02-17 17:25:18 +0000152OPTIONS
153-------
154-a::
155--all::
156 This option is only valid for `push` and `save` commands.
157+
158All ignored and untracked files are also stashed and then cleaned
159up with `git clean`.
Alexandr Miloslavskiy2b7460d2020-02-17 17:25:17 +0000160
Alexandr Miloslavskiy0093abc2020-02-17 17:25:18 +0000161-u::
162--include-untracked::
163 This option is only valid for `push` and `save` commands.
164+
165All untracked files are also stashed and then cleaned up with
166`git clean`.
Alexandr Miloslavskiy2b7460d2020-02-17 17:25:17 +0000167
Alexandr Miloslavskiy0093abc2020-02-17 17:25:18 +0000168--index::
169 This option is only valid for `pop` and `apply` commands.
170+
171Tries to reinstate not only the working tree's changes, but also
172the index's ones. However, this can fail, when you have conflicts
173(which are stored in the index, where you therefore can no longer
174apply the changes as they were originally).
Alexandr Miloslavskiy2b7460d2020-02-17 17:25:17 +0000175
Alexandr Miloslavskiy0093abc2020-02-17 17:25:18 +0000176-k::
177--keep-index::
178--no-keep-index::
179 This option is only valid for `push` and `save` commands.
180+
181All changes already added to the index are left intact.
Alexandr Miloslavskiy2b7460d2020-02-17 17:25:17 +0000182
Alexandr Miloslavskiy0093abc2020-02-17 17:25:18 +0000183-p::
184--patch::
185 This option is only valid for `push` and `save` commands.
186+
187Interactively select hunks from the diff between HEAD and the
188working tree to be stashed. The stash entry is constructed such
189that its index state is the same as the index state of your
190repository, and its worktree contains only the changes you selected
191interactively. The selected changes are then rolled back from your
192worktree. See the ``Interactive Mode'' section of linkgit:git-add[1]
193to learn how to operate the `--patch` mode.
Alexandr Miloslavskiy2b7460d2020-02-17 17:25:17 +0000194+
195The `--patch` option implies `--keep-index`. You can use
196`--no-keep-index` to override this.
197
Alexandr Miloslavskiy8a987582020-02-17 17:25:22 +0000198--pathspec-from-file=<file>::
199 This option is only valid for `push` command.
200+
201Pathspec is passed in `<file>` instead of commandline args. If
202`<file>` is exactly `-` then standard input is used. Pathspec
203elements are separated by LF or CR/LF. Pathspec elements can be
204quoted as explained for the configuration variable `core.quotePath`
205(see linkgit:git-config[1]). See also `--pathspec-file-nul` and
206global `--literal-pathspecs`.
207
208--pathspec-file-nul::
209 This option is only valid for `push` command.
210+
211Only meaningful with `--pathspec-from-file`. Pathspec elements are
212separated with NUL character and all other characters are taken
213literally (including newlines and quotes).
214
Alexandr Miloslavskiyb2290912020-02-17 17:25:19 +0000215-q::
216--quiet::
217 This option is only valid for `apply`, `drop`, `pop`, `push`,
218 `save`, `store` commands.
219+
220Quiet, suppress feedback messages.
221
222\--::
223 This option is only valid for `push` command.
224+
225Separates pathspec from options for disambiguation purposes.
226
Alexandr Miloslavskiy0093abc2020-02-17 17:25:18 +0000227<pathspec>...::
228 This option is only valid for `push` command.
229+
230The new stash entry records the modified states only for the files
231that match the pathspec. The index entries and working tree files
232are then rolled back to the state in HEAD only for these files,
233too, leaving files that do not match the pathspec intact.
Alexandr Miloslavskiy3f3d8062020-02-17 17:25:20 +0000234+
235For more details, see the 'pathspec' entry in linkgit:gitglossary[7].
Alexandr Miloslavskiy2b7460d2020-02-17 17:25:17 +0000236
Alexandr Miloslavskiy0093abc2020-02-17 17:25:18 +0000237<stash>::
238 This option is only valid for `apply`, `branch`, `drop`, `pop`,
239 `show` commands.
240+
241A reference of the form `stash@{<revision>}`. When no `<stash>` is
242given, the latest stash is assumed (that is, `stash@{0}`).
Alexandr Miloslavskiy2b7460d2020-02-17 17:25:17 +0000243
しらいしななこ09ccdb62007-07-01 14:26:08 +0900244DISCUSSION
245----------
246
Liam Beguine01db912017-06-17 18:30:50 -0400247A stash entry is represented as a commit whose tree records the state
248of the working directory, and its first parent is the commit at `HEAD`
249when the entry was created. The tree of the second parent records the
250state of the index when the entry is made, and it is made a child of
しらいしななこ09ccdb62007-07-01 14:26:08 +0900251the `HEAD` commit. The ancestry graph looks like this:
252
253 .----W
254 / /
Junio C Hamano114fd812007-07-04 22:09:20 -0700255 -----H----I
しらいしななこ09ccdb62007-07-01 14:26:08 +0900256
257where `H` is the `HEAD` commit, `I` is a commit that records the state
258of the index, and `W` is a commit that records the state of the working
259tree.
260
261
262EXAMPLES
263--------
264
265Pulling into a dirty tree::
266
267When you are in the middle of something, you learn that there are
Junio C Hamano9488e872007-07-01 15:29:01 -0700268upstream changes that are possibly relevant to what you are
269doing. When your local changes do not conflict with the changes in
しらいしななこ09ccdb62007-07-01 14:26:08 +0900270the upstream, a simple `git pull` will let you move forward.
271+
272However, there are cases in which your local changes do conflict with
273the upstream changes, and `git pull` refuses to overwrite your
Junio C Hamano9488e872007-07-01 15:29:01 -0700274changes. In such a case, you can stash your changes away,
しらいしななこ09ccdb62007-07-01 14:26:08 +0900275perform a pull, and then unstash, like this:
276+
277----------------------------------------------------------------
278$ git pull
SZEDER Gábor9da6f0f2008-09-02 03:35:24 +0200279 ...
しらいしななこ09ccdb62007-07-01 14:26:08 +0900280file foobar not up to date, cannot merge.
281$ git stash
282$ git pull
Thomas Rastd1836632009-05-28 11:40:15 +0200283$ git stash pop
しらいしななこ09ccdb62007-07-01 14:26:08 +0900284----------------------------------------------------------------
285
286Interrupted workflow::
287
288When you are in the middle of something, your boss comes in and
Junio C Hamano9488e872007-07-01 15:29:01 -0700289demands that you fix something immediately. Traditionally, you would
しらいしななこ09ccdb62007-07-01 14:26:08 +0900290make a commit to a temporary branch to store your changes away, and
Junio C Hamano9488e872007-07-01 15:29:01 -0700291return to your original branch to make the emergency fix, like this:
しらいしななこ09ccdb62007-07-01 14:26:08 +0900292+
293----------------------------------------------------------------
SZEDER Gábor9da6f0f2008-09-02 03:35:24 +0200294# ... hack hack hack ...
Nguyễn Thái Ngọc Duy328c6cb2019-03-29 17:39:19 +0700295$ git switch -c my_wip
しらいしななこ09ccdb62007-07-01 14:26:08 +0900296$ git commit -a -m "WIP"
Nguyễn Thái Ngọc Duy328c6cb2019-03-29 17:39:19 +0700297$ git switch master
しらいしななこ09ccdb62007-07-01 14:26:08 +0900298$ edit emergency fix
299$ git commit -a -m "Fix in a hurry"
Nguyễn Thái Ngọc Duy328c6cb2019-03-29 17:39:19 +0700300$ git switch my_wip
しらいしななこ09ccdb62007-07-01 14:26:08 +0900301$ git reset --soft HEAD^
SZEDER Gábor9da6f0f2008-09-02 03:35:24 +0200302# ... continue hacking ...
しらいしななこ09ccdb62007-07-01 14:26:08 +0900303----------------------------------------------------------------
304+
Thomas Rast0b444cd2010-01-10 00:33:00 +0100305You can use 'git stash' to simplify the above, like this:
しらいしななこ09ccdb62007-07-01 14:26:08 +0900306+
307----------------------------------------------------------------
SZEDER Gábor9da6f0f2008-09-02 03:35:24 +0200308# ... hack hack hack ...
しらいしななこ09ccdb62007-07-01 14:26:08 +0900309$ git stash
310$ edit emergency fix
311$ git commit -a -m "Fix in a hurry"
Thomas Rastd1836632009-05-28 11:40:15 +0200312$ git stash pop
SZEDER Gábor9da6f0f2008-09-02 03:35:24 +0200313# ... continue hacking ...
しらいしななこ09ccdb62007-07-01 14:26:08 +0900314----------------------------------------------------------------
315
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200316Testing partial commits::
317
Thomas Gummererdb377452017-10-22 18:04:07 +0100318You can use `git stash push --keep-index` when you want to make two or
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200319more commits out of the changes in the work tree, and you want to test
320each change before committing:
321+
322----------------------------------------------------------------
SZEDER Gábor9da6f0f2008-09-02 03:35:24 +0200323# ... hack hack hack ...
Eric Raiblecaf18992008-07-08 00:40:56 -0700324$ git add --patch foo # add just first part to the index
Thomas Gummererdb377452017-10-22 18:04:07 +0100325$ git stash push --keep-index # save all other changes to the stash
Eric Raiblecaf18992008-07-08 00:40:56 -0700326$ edit/build/test first part
SZEDER Gáborf733c702008-09-02 03:45:01 +0200327$ git commit -m 'First part' # commit fully tested change
Eric Raiblecaf18992008-07-08 00:40:56 -0700328$ git stash pop # prepare to work on all other changes
SZEDER Gábor9da6f0f2008-09-02 03:35:24 +0200329# ... repeat above five steps until one commit remains ...
Eric Raiblecaf18992008-07-08 00:40:56 -0700330$ edit/build/test remaining parts
331$ git commit foo -m 'Remaining parts'
SZEDER Gábor7bedebc2008-06-27 16:37:15 +0200332----------------------------------------------------------------
333
Liam Beguine01db912017-06-17 18:30:50 -0400334Recovering stash entries that were cleared/dropped erroneously::
Thomas Rastf5f1e162009-08-09 02:47:36 +0200335
Liam Beguine01db912017-06-17 18:30:50 -0400336If you mistakenly drop or clear stash entries, they cannot be recovered
Thomas Rastf5f1e162009-08-09 02:47:36 +0200337through the normal safety mechanisms. However, you can try the
Liam Beguine01db912017-06-17 18:30:50 -0400338following incantation to get a list of stash entries that are still in
339your repository, but not reachable any more:
Thomas Rastf5f1e162009-08-09 02:47:36 +0200340+
341----------------------------------------------------------------
342git fsck --unreachable |
343grep commit | cut -d\ -f3 |
344xargs git log --merges --no-walk --grep=WIP
345----------------------------------------------------------------
346
347
しらいしななこ09ccdb62007-07-01 14:26:08 +0900348SEE ALSO
349--------
Dan McGee5162e692007-12-29 00:20:38 -0600350linkgit:git-checkout[1],
351linkgit:git-commit[1],
352linkgit:git-reflog[1],
Nguyễn Thái Ngọc Duy328c6cb2019-03-29 17:39:19 +0700353linkgit:git-reset[1],
354linkgit:git-switch[1]
しらいしななこ09ccdb62007-07-01 14:26:08 +0900355
しらいしななこ09ccdb62007-07-01 14:26:08 +0900356GIT
357---
Christian Couder9e1f0a82008-06-06 09:07:32 +0200358Part of the linkgit:git[1] suite