interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / Documentation / git-stash.adoc

448 lines · 16616 bytes Raw Blame History
1git-stash(1)
2============
3
4NAME
5----
6git-stash - Stash the changes in a dirty working directory away
7
8SYNOPSIS
9--------
10[synopsis]
11git stash list [<log-options>]
12git stash show [-u | --include-untracked | --only-untracked] [<diff-options>] [<stash>]
13git stash drop [-q | --quiet] [<stash>]
14git stash pop [--index] [-q | --quiet] [<stash>]
15git stash apply [--index] [-q | --quiet] [--label-ours=<label>] [--label-theirs=<label>] [--label-base=<label>] [<stash>]
16git stash branch <branchname> [<stash>]
17git stash [push] [-p | --patch] [-S | --staged] [-k | --[no-]keep-index] [-q | --quiet]
18 [-u | --include-untracked] [-a | --all] [(-m | --message) <message>]
19 [--pathspec-from-file=<file> [--pathspec-file-nul]]
20 [--] [<pathspec>...]
21git stash clear
22git stash create [<message>]
23git stash store [(-m | --message) <message>] [-q | --quiet] <commit>
24git stash export (--print | --to-ref <ref>) [<stash>...]
25git stash import <commit>
26
27DESCRIPTION
28-----------
29
30Use `git stash` when you want to record the current state of the
31working directory and the index, but want to go back to a clean
32working directory. The command saves your local modifications away
33and reverts the working directory to match the `HEAD` commit.
34
35The modifications stashed away by this command can be listed with
36`git stash list`, inspected with `git stash show`, and restored
37(potentially on top of a different commit) with `git stash apply`.
38Calling `git stash` without any arguments is equivalent to `git stash push`.
39A stash is by default listed as "WIP on '<branchname>' ...", but
40you can give a more descriptive message on the command line when
41you create one.
42
43The latest stash you created is stored in `refs/stash`; older
44stashes are found in the reflog of this reference and can be named using
45the usual reflog syntax (e.g. `stash@{0}` is the most recently
46created stash, `stash@{1}` is the one before it, `stash@{2.hours.ago}`
47is also possible). Stashes may also be referenced by specifying just the
48stash index (e.g. the integer `<n>` is equivalent to `stash@{<n>}`).
49
50COMMANDS
51--------
52
53`push [-p | --patch] [-S | --staged] [-k | --[no-]keep-index] [-u | --include-untracked] [ -a | --all] [-q | --quiet] [(-m|--message) <message>] [--pathspec-from-file=<file> [--pathspec-file-nul]] [--] [<pathspec>...]`::
54
55 Save your local modifications to a new 'stash entry' and roll them
56 back to `HEAD` (in the working tree and in the index).
57 The _<message>_ part is optional and gives
58 the description along with the stashed state.
59+
60For quickly making a snapshot, you can omit "push". In this mode,
61pathspec elements are only allowed after a double hyphen `--`
62to prevent a misspelled subcommand from making an unwanted stash entry.
63
64`save [-p | --patch] [-S | --staged] [-k | --[no-]keep-index] [-u | --include-untracked] [-a | --all] [-q | --quiet] [<message>]`::
65
66 This option is deprecated in favour of 'git stash push'. It
67 differs from "stash push" in that it cannot take pathspec.
68 Instead, all non-option arguments are concatenated to form the stash
69 message.
70
71`list [<log-options>]`::
72
73 List the stash entries that you currently have. Each 'stash entry' is
74 listed with its name (e.g. `stash@{0}` is the latest entry, `stash@{1}` is
75 the one before, etc.), the name of the branch that was current when the
76 entry was made, and a short description of the commit the entry was
77 based on.
78+
79----------------------------------------------------------------
80stash@{0}: WIP on submit: 6ebd0e2... Update git-stash documentation
81stash@{1}: On master: 9cc0589... Add git-stash
82----------------------------------------------------------------
83+
84The command takes options applicable to the 'git log'
85command to control what is shown and how. See linkgit:git-log[1].
86
87`show [-u | --include-untracked | --only-untracked] [<diff-options>] [<stash>]`::
88
89 Show the changes recorded in the stash entry as a diff between the
90 stashed contents and the commit back when the stash entry was first
91 created.
92 By default, the command shows the diffstat, but it will accept any
93 format known to 'git diff' (e.g., `git stash show -p stash@{1}`
94 to view the second most recent entry in patch form).
95 If no _<diff-option>_ is provided, the default behavior will be given
96 by the `stash.showStat`, and `stash.showPatch` config variables. You
97 can also use `stash.showIncludeUntracked` to set whether
98 `--include-untracked` is enabled by default.
99
100`pop [--index] [-q | --quiet] [<stash>]`::
101
102 Remove a single stashed state from the stash list and apply it
103 on top of the current working tree state, i.e., do the inverse
104 operation of `git stash push`. The working directory must
105 match the index.
106+
107Applying the state can fail with conflicts; in this case, it is not
108removed from the stash list. You need to resolve the conflicts by hand
109and call `git stash drop` manually afterwards.
110
111`apply [--index] [-q | --quiet] [<stash>]`::
112
113 Like `pop`, but do not remove the state from the stash list. Unlike `pop`,
114 `<stash>` may be any commit that looks like a commit created by
115 `stash push` or `stash create`.
116
117`branch <branchname> [<stash>]`::
118
119 Creates and checks out a new branch named _<branchname>_ starting from
120 the commit at which the _<stash>_ was originally created, applies the
121 changes recorded in _<stash>_ to the new working tree and index.
122 If that succeeds, and _<stash>_ is a reference of the form
123 `stash@{<revision>}`, it then drops the _<stash>_.
124+
125This is useful if the branch on which you ran `git stash push` has
126changed enough that `git stash apply` fails due to conflicts. Since
127the stash entry is applied on top of the commit that was HEAD at the
128time `git stash` was run, it restores the originally stashed state
129with no conflicts.
130
131`clear`::
132 Remove all the stash entries. Note that those entries will then
133 be subject to pruning, and may be impossible to recover (see
134 <<EXAMPLES,'EXAMPLES'>> below for a possible strategy).
135
136`drop [-q | --quiet] [<stash>]`::
137 Remove a single stash entry from the list of stash entries.
138
139`create`::
140 Create a stash entry (which is a regular commit object) and
141 return its object name, without storing it anywhere in the ref
142 namespace.
143 This is intended to be useful for scripts. It is probably not
144 the command you want to use; see "push" above.
145
146`store`::
147
148 Store a given stash created via 'git stash create' (which is a
149 dangling merge commit) in the stash ref, updating the stash
150 reflog. This is intended to be useful for scripts. It is
151 probably not the command you want to use; see "push" above.
152
153`export ( --print | --to-ref <ref> ) [<stash>...]`::
154
155 Export the specified stashes, or all of them if none are specified, to
156 a chain of commits which can be transferred using the normal fetch and
157 push mechanisms, then imported using the `import` subcommand.
158
159`import <commit>`::
160 Import the specified stashes from the specified commit, which must have been
161 created by `export`, and add them to the list of stashes. To replace the
162 existing stashes, use `clear` first.
163
164OPTIONS
165-------
166`-a`::
167`--all`::
168 This option is only valid for `push` and `save` commands.
169+
170All ignored and untracked files are also stashed and then cleaned
171up with `git clean`.
172
173`-u`::
174`--include-untracked`::
175`--no-include-untracked`::
176 When used with the `push` and `save` commands,
177 all untracked files are also stashed and then cleaned up with
178 `git clean`.
179+
180When used with the `show` command, show the untracked files in the stash
181entry as part of the diff.
182
183`--only-untracked`::
184 This option is only valid for the `show` command.
185+
186Show only the untracked files in the stash entry as part of the diff.
187
188`--index`::
189 This option is only valid for `pop` and `apply` commands.
190+
191Tries to reinstate not only the working tree's changes, but also
192the index's ones. However, this can fail, when you have conflicts
193(which are stored in the index, where you therefore can no longer
194apply the changes as they were originally).
195
196`--label-ours=<label>`::
197`--label-theirs=<label>`::
198`--label-base=<label>`::
199 These options are only valid for the `apply` command.
200+
201Use the given labels in conflict markers instead of the default
202"Updated upstream", "Stashed changes", and "Stash base".
203`--label-base` only has an effect with merge.conflictStyle=diff3.
204
205`-k`::
206`--keep-index`::
207`--no-keep-index`::
208 This option is only valid for `push` and `save` commands.
209+
210All changes already added to the index are left intact.
211
212`-p`::
213`--patch`::
214 This option is only valid for `push` and `save` commands.
215+
216Interactively select hunks from the diff between HEAD and the
217working tree to be stashed. The stash entry is constructed such
218that its index state is the same as the index state of your
219repository, and its worktree contains only the changes you selected
220interactively. The selected changes are then rolled back from your
221worktree. See the ``Interactive Mode'' section of linkgit:git-add[1]
222to learn how to operate the `--patch` mode.
223+
224The `--patch` option implies `--keep-index`. You can use
225`--no-keep-index` to override this.
226
227include::diff-context-options.adoc[]
228
229`-S`::
230`--staged`::
231 This option is only valid for `push` and `save` commands.
232+
233Stash only the changes that are currently staged. This is similar to
234basic `git commit` except the state is committed to the stash instead
235of current branch.
236+
237The `--patch` option has priority over this one.
238
239`--pathspec-from-file=<file>`::
240 This option is only valid for `push` command.
241+
242Pathspec is passed in _<file>_ instead of commandline args. If
243_<file>_ is exactly `-` then standard input is used. Pathspec
244elements are separated by LF or CR/LF. Pathspec elements can be
245quoted as explained for the configuration variable `core.quotePath`
246(see linkgit:git-config[1]). See also `--pathspec-file-nul` and
247global `--literal-pathspecs`.
248
249`--pathspec-file-nul`::
250 This option is only valid for `push` command.
251+
252Only meaningful with `--pathspec-from-file`. Pathspec elements are
253separated with NUL character and all other characters are taken
254literally (including newlines and quotes).
255
256`-q`::
257`--quiet`::
258 This option is only valid for `apply`, `drop`, `pop`, `push`,
259 `save`, `store` commands.
260+
261Quiet, suppress feedback messages.
262
263`--print`::
264 This option is only valid for the `export` command.
265+
266Create the chain of commits representing the exported stashes without
267storing it anywhere in the ref namespace and print the object ID to
268standard output. This is designed for scripts.
269
270`--to-ref`::
271 This option is only valid for the `export` command.
272+
273Create the chain of commits representing the exported stashes and store
274it to the specified ref.
275
276`--`::
277 This option is only valid for `push` command.
278+
279Separates pathspec from options for disambiguation purposes.
280
281`<pathspec>...`::
282 This option is only valid for `push` command.
283+
284The new stash entry records the modified states only for the files
285that match the pathspec. The index entries and working tree files
286are then rolled back to the state in HEAD only for these files,
287too, leaving files that do not match the pathspec intact.
288+
289For more details, see the 'pathspec' entry in linkgit:gitglossary[7].
290
291_<stash>_::
292 This option is only valid for `apply`, `branch`, `drop`, `pop`,
293 `show`, and `export` commands.
294+
295A reference of the form `stash@{<revision>}`. When no _<stash>_ is
296given, the latest stash is assumed (that is, `stash@{0}`).
297
298DISCUSSION
299----------
300
301A stash entry is represented as a commit whose tree records the state
302of the working directory, and its first parent is the commit at `HEAD`
303when the entry was created. The tree of the second parent records the
304state of the index when the entry is made, and it is made a child of
305the `HEAD` commit. The ancestry graph looks like this:
306
307 .----W
308 / /
309 -----H----I
310
311where `H` is the `HEAD` commit, `I` is a commit that records the state
312of the index, and `W` is a commit that records the state of the working
313tree.
314
315
316[[EXAMPLES]]
317EXAMPLES
318--------
319
320Pulling into a dirty tree::
321
322When you are in the middle of something, you learn that there are
323upstream changes that are possibly relevant to what you are
324doing. When your local changes do not conflict with the changes in
325the upstream, a simple `git pull` will let you move forward.
326+
327However, there are cases in which your local changes do conflict with
328the upstream changes, and `git pull` refuses to overwrite your
329changes. In such a case, you can stash your changes away,
330perform a pull, and then unstash, like this:
331+
332----------------------------------------------------------------
333$ git pull
334 ...
335file foobar not up to date, cannot merge.
336$ git stash
337$ git pull
338$ git stash pop
339----------------------------------------------------------------
340
341Interrupted workflow::
342
343When you are in the middle of something, your boss comes in and
344demands that you fix something immediately. Traditionally, you would
345make a commit to a temporary branch to store your changes away, and
346return to your original branch to make the emergency fix, like this:
347+
348----------------------------------------------------------------
349# ... hack hack hack ...
350$ git switch -c my_wip
351$ git commit -a -m "WIP"
352$ git switch master
353$ edit emergency fix
354$ git commit -a -m "Fix in a hurry"
355$ git switch my_wip
356$ git reset --soft HEAD^
357# ... continue hacking ...
358----------------------------------------------------------------
359+
360You can use 'git stash' to simplify the above, like this:
361+
362----------------------------------------------------------------
363# ... hack hack hack ...
364$ git stash
365$ edit emergency fix
366$ git commit -a -m "Fix in a hurry"
367$ git stash pop
368# ... continue hacking ...
369----------------------------------------------------------------
370
371Testing partial commits::
372
373You can use `git stash push --keep-index` when you want to make two or
374more commits out of the changes in the work tree, and you want to test
375each change before committing:
376+
377----------------------------------------------------------------
378# ... hack hack hack ...
379$ git add --patch foo # add just first part to the index
380$ git stash push --keep-index # save all other changes to the stash
381$ edit/build/test first part
382$ git commit -m 'First part' # commit fully tested change
383$ git stash pop # prepare to work on all other changes
384# ... repeat above five steps until one commit remains ...
385$ edit/build/test remaining parts
386$ git commit foo -m 'Remaining parts'
387----------------------------------------------------------------
388
389Saving unrelated changes for future use::
390
391When you are in the middle of massive changes and you find some
392unrelated issue that you don't want to forget to fix, you can do the
393change(s), stage them, and use `git stash push --staged` to stash them
394out for future use. This is similar to committing the staged changes,
395only the commit ends-up being in the stash and not on the current branch.
396+
397----------------------------------------------------------------
398# ... hack hack hack ...
399$ git add --patch foo # add unrelated changes to the index
400$ git stash push --staged # save these changes to the stash
401# ... hack hack hack, finish current changes ...
402$ git commit -m 'Massive' # commit fully tested changes
403$ git switch fixup-branch # switch to another branch
404$ git stash pop # to finish work on the saved changes
405----------------------------------------------------------------
406
407Recovering stash entries that were cleared/dropped erroneously::
408
409If you mistakenly drop or clear stash entries, they cannot be recovered
410through the normal safety mechanisms. However, you can try the
411following incantation to get a list of stash entries that are still in
412your repository, but not reachable any more:
413+
414----------------------------------------------------------------
415git fsck --unreachable |
416grep commit | cut -d\ -f3 |
417xargs git log --merges --no-walk --grep=WIP
418----------------------------------------------------------------
419
420CONFIGURATION
421-------------
422
423include::includes/cmd-config-section-all.adoc[]
424
425:git-stash: 1
426include::config/stash.adoc[]
427
428EXIT STATUS
429-----------
430
431The `git stash` subcommands exit with status 0 on success. The
432subcommands that apply a stash entry, i.e. `apply`, `pop` and `branch`,
433exit with status 1 when applying the stash entry resulted in conflicts,
434in which case the stash entry is left in place, and with a non-zero
435status other than 1 when they fail for other reasons.
436
437
438SEE ALSO
439--------
440linkgit:git-checkout[1],
441linkgit:git-commit[1],
442linkgit:git-reflog[1],
443linkgit:git-reset[1],
444linkgit:git-switch[1]
445
446GIT
447---
448Part of the linkgit:git[1] suite