3 .\" Author: [FIXME: author] [see http://www.docbook.org/tdg5/en/html/author]
4 .\" Generator: DocBook XSL Stylesheets v1.79.2 <http://docbook.sf.net/>
7 .\" Source: Git 2.46.2.628.g6258f68c3c
10 .TH "GIT\-STASH" "1" "2024-09-23" "Git 2\&.46\&.2\&.628\&.g6258f6" "Git Manual"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 git-stash \- Stash the changes in a dirty working directory away
35 \fIgit stash\fR list [<log\-options>]
36 \fIgit stash\fR show [\-u | \-\-include\-untracked | \-\-only\-untracked] [<diff\-options>] [<stash>]
37 \fIgit stash\fR drop [\-q | \-\-quiet] [<stash>]
38 \fIgit stash\fR pop [\-\-index] [\-q | \-\-quiet] [<stash>]
39 \fIgit stash\fR apply [\-\-index] [\-q | \-\-quiet] [<stash>]
40 \fIgit stash\fR branch <branchname> [<stash>]
41 \fIgit stash\fR [push [\-p | \-\-patch] [\-S | \-\-staged] [\-k | \-\-[no\-]keep\-index] [\-q | \-\-quiet]
42 [\-u | \-\-include\-untracked] [\-a | \-\-all] [(\-m | \-\-message) <message>]
43 [\-\-pathspec\-from\-file=<file> [\-\-pathspec\-file\-nul]]
44 [\-\-] [<pathspec>\&...\:]]
45 \fIgit stash\fR save [\-p | \-\-patch] [\-S | \-\-staged] [\-k | \-\-[no\-]keep\-index] [\-q | \-\-quiet]
46 [\-u | \-\-include\-untracked] [\-a | \-\-all] [<message>]
48 \fIgit stash\fR create [<message>]
49 \fIgit stash\fR store [(\-m | \-\-message) <message>] [\-q | \-\-quiet] <commit>
53 Use \fBgit stash\fR when you want to record the current state of the working directory and the index, but want to go back to a clean working directory\&. The command saves your local modifications away and reverts the working directory to match the \fBHEAD\fR commit\&.
55 The modifications stashed away by this command can be listed with \fBgit stash list\fR, inspected with \fBgit stash show\fR, and restored (potentially on top of a different commit) with \fBgit stash apply\fR\&. Calling \fBgit stash\fR without any arguments is equivalent to \fBgit stash push\fR\&. A stash is by default listed as "WIP on \fIbranchname\fR \&...\:", but you can give a more descriptive message on the command line when you create one\&.
57 The latest stash you created is stored in \fBrefs/stash\fR; older stashes are found in the reflog of this reference and can be named using the usual reflog syntax (e\&.g\&. \fBstash@{0}\fR is the most recently created stash, \fBstash@{1}\fR is the one before it, \fBstash@{2\&.hours\&.ago}\fR is also possible)\&. Stashes may also be referenced by specifying just the stash index (e\&.g\&. the integer \fBn\fR is equivalent to \fBstash@{n}\fR)\&.
60 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>\&...\:]
62 Save your local modifications to a new
64 and roll them back to HEAD (in the working tree and in the index)\&. The <message> part is optional and gives the description along with the stashed state\&.
66 For quickly making a snapshot, you can omit "push"\&. In this mode, non\-option arguments are not allowed to prevent a misspelled subcommand from making an unwanted stash entry\&. The two exceptions to this are
68 which acts as alias for
70 and pathspec elements, which are allowed after a double hyphen
75 save [\-p|\-\-patch] [\-S|\-\-staged] [\-k|\-\-[no\-]keep\-index] [\-u|\-\-include\-untracked] [\-a|\-\-all] [\-q|\-\-quiet] [<message>]
77 This option is deprecated in favour of
78 \fIgit stash push\fR\&. It differs from "stash push" in that it cannot take pathspec\&. Instead, all non\-option arguments are concatenated to form the stash message\&.
83 List the stash entries that you currently have\&. Each
85 is listed with its name (e\&.g\&.
89 is the one before, etc\&.), the name of the branch that was current when the entry was made, and a short description of the commit the entry was based on\&.
95 stash@{0}: WIP on submit: 6ebd0e2\&.\&.\&. Update git\-stash documentation
96 stash@{1}: On master: 9cc0589\&.\&.\&. Add git\-stash
102 The command takes options applicable to the
104 command to control what is shown and how\&. See
108 show [\-u|\-\-include\-untracked|\-\-only\-untracked] [<diff\-options>] [<stash>]
110 Show the changes recorded in the stash entry as a diff between the stashed contents and the commit back when the stash entry was first created\&. By default, the command shows the diffstat, but it will accept any format known to
113 \fBgit stash show \-p stash@{1}\fR
114 to view the second most recent entry in patch form)\&. If no
116 is provided, the default behavior will be given by the
117 \fBstash\&.showStat\fR, and
118 \fBstash\&.showPatch\fR
119 config variables\&. You can also use
120 \fBstash\&.showIncludeUntracked\fR
122 \fB\-\-include\-untracked\fR
123 is enabled by default\&.
126 pop [\-\-index] [\-q|\-\-quiet] [<stash>]
128 Remove a single stashed state from the stash list and apply it on top of the current working tree state, i\&.e\&., do the inverse operation of
129 \fBgit stash push\fR\&. The working directory must match the index\&.
131 Applying the state can fail with conflicts; in this case, it is not removed from the stash list\&. You need to resolve the conflicts by hand and call
133 manually afterwards\&.
136 apply [\-\-index] [\-q|\-\-quiet] [<stash>]
139 \fBpop\fR, but do not remove the state from the stash list\&. Unlike
142 may be any commit that looks like a commit created by
145 \fBstash create\fR\&.
148 branch <branchname> [<stash>]
150 Creates and checks out a new branch named
152 starting from the commit at which the
154 was originally created, applies the changes recorded in
156 to the new working tree and index\&. If that succeeds, and
158 is a reference of the form
159 \fBstash@{<revision>}\fR, it then drops the
162 This is useful if the branch on which you ran
164 has changed enough that
165 \fBgit stash apply\fR
166 fails due to conflicts\&. Since the stash entry is applied on top of the commit that was HEAD at the time
168 was run, it restores the originally stashed state with no conflicts\&.
173 Remove all the stash entries\&. Note that those entries will then be subject to pruning, and may be impossible to recover (see
175 below for a possible strategy)\&.
178 drop [\-q|\-\-quiet] [<stash>]
180 Remove a single stash entry from the list of stash entries\&.
185 Create a stash entry (which is a regular commit object) and return its object name, without storing it anywhere in the ref namespace\&. This is intended to be useful for scripts\&. It is probably not the command you want to use; see "push" above\&.
190 Store a given stash created via
191 \fIgit stash create\fR
192 (which is a dangling merge commit) in the stash ref, updating the stash reflog\&. This is intended to be useful for scripts\&. It is probably not the command you want to use; see "push" above\&.
198 This option is only valid for
204 All ignored and untracked files are also stashed and then cleaned up with
208 \-u, \-\-include\-untracked, \-\-no\-include\-untracked
214 commands, all untracked files are also stashed and then cleaned up with
219 command, show the untracked files in the stash entry as part of the diff\&.
224 This option is only valid for the
228 Show only the untracked files in the stash entry as part of the diff\&.
233 This option is only valid for
239 Tries to reinstate not only the working tree\(cqs changes, but also the index\(cqs ones\&. However, this can fail, when you have conflicts (which are stored in the index, where you therefore can no longer apply the changes as they were originally)\&.
242 \-k, \-\-keep\-index, \-\-no\-keep\-index
244 This option is only valid for
250 All changes already added to the index are left intact\&.
255 This option is only valid for
261 Interactively select hunks from the diff between HEAD and the working tree to be stashed\&. The stash entry is constructed such that its index state is the same as the index state of your repository, and its worktree contains only the changes you selected interactively\&. The selected changes are then rolled back from your worktree\&. See the
262 \(lqInteractive Mode\(rq
265 to learn how to operate the
272 \fB\-\-keep\-index\fR\&. You can use
273 \fB\-\-no\-keep\-index\fR
279 This option is only valid for
285 Stash only the changes that are currently staged\&. This is similar to basic
287 except the state is committed to the stash instead of current branch\&.
291 option has priority over this one\&.
294 \-\-pathspec\-from\-file=<file>
296 This option is only valid for
300 Pathspec is passed in
302 instead of commandline args\&. If
306 then standard input is used\&. Pathspec elements are separated by LF or CR/LF\&. Pathspec elements can be quoted as explained for the configuration variable
307 \fBcore\&.quotePath\fR
309 \fBgit-config\fR(1))\&. See also
310 \fB\-\-pathspec\-file\-nul\fR
312 \fB\-\-literal\-pathspecs\fR\&.
315 \-\-pathspec\-file\-nul
317 This option is only valid for
322 \fB\-\-pathspec\-from\-file\fR\&. Pathspec elements are separated with NUL character and all other characters are taken literally (including newlines and quotes)\&.
327 This option is only valid for
336 Quiet, suppress feedback messages\&.
341 This option is only valid for
345 Separates pathspec from options for disambiguation purposes\&.
350 This option is only valid for
354 The new stash entry records the modified states only for the files that match the pathspec\&. The index entries and working tree files are then rolled back to the state in HEAD only for these files, too, leaving files that do not match the pathspec intact\&.
356 For more details, see the
359 \fBgitglossary\fR(7)\&.
364 This option is only valid for
372 A reference of the form
373 \fBstash@{<revision>}\fR\&. When no
375 is given, the latest stash is assumed (that is,
380 A stash entry is represented as a commit whose tree records the state of the working directory, and its first parent is the commit at \fBHEAD\fR when the entry was created\&. The tree of the second parent records the state of the index when the entry is made, and it is made a child of the \fBHEAD\fR commit\&. The ancestry graph looks like this:
394 where \fBH\fR is the \fBHEAD\fR commit, \fBI\fR is a commit that records the state of the index, and \fBW\fR is a commit that records the state of the working tree\&.
397 Pulling into a dirty tree
399 When you are in the middle of something, you learn that there are upstream changes that are possibly relevant to what you are doing\&. When your local changes do not conflict with the changes in the upstream, a simple
401 will let you move forward\&.
403 However, there are cases in which your local changes do conflict with the upstream changes, and
405 refuses to overwrite your changes\&. In such a case, you can stash your changes away, perform a pull, and then unstash, like this:
413 file foobar not up to date, cannot merge\&.
425 When you are in the middle of something, your boss comes in and demands that you fix something immediately\&. Traditionally, you would make a commit to a temporary branch to store your changes away, and return to your original branch to make the emergency fix, like this:
431 # \&.\&.\&. hack hack hack \&.\&.\&.
432 $ git switch \-c my_wip
433 $ git commit \-a \-m "WIP"
436 $ git commit \-a \-m "Fix in a hurry"
438 $ git reset \-\-soft HEAD^
439 # \&.\&.\&. continue hacking \&.\&.\&.
447 to simplify the above, like this:
453 # \&.\&.\&. hack hack hack \&.\&.\&.
456 $ git commit \-a \-m "Fix in a hurry"
458 # \&.\&.\&. continue hacking \&.\&.\&.
465 Testing partial commits
468 \fBgit stash push \-\-keep\-index\fR
469 when you want to make two or more commits out of the changes in the work tree, and you want to test each change before committing:
475 # \&.\&.\&. hack hack hack \&.\&.\&.
476 $ git add \-\-patch foo # add just first part to the index
477 $ git stash push \-\-keep\-index # save all other changes to the stash
478 $ edit/build/test first part
479 $ git commit \-m \*(AqFirst part\*(Aq # commit fully tested change
480 $ git stash pop # prepare to work on all other changes
481 # \&.\&.\&. repeat above five steps until one commit remains \&.\&.\&.
482 $ edit/build/test remaining parts
483 $ git commit foo \-m \*(AqRemaining parts\*(Aq
490 Saving unrelated changes for future use
492 When you are in the middle of massive changes and you find some unrelated issue that you don\(cqt want to forget to fix, you can do the change(s), stage them, and use
493 \fBgit stash push \-\-staged\fR
494 to stash them out for future use\&. This is similar to committing the staged changes, only the commit ends\-up being in the stash and not on the current branch\&.
500 # \&.\&.\&. hack hack hack \&.\&.\&.
501 $ git add \-\-patch foo # add unrelated changes to the index
502 $ git stash push \-\-staged # save these changes to the stash
503 # \&.\&.\&. hack hack hack, finish current changes \&.\&.\&.
504 $ git commit \-m \*(AqMassive\*(Aq # commit fully tested changes
505 $ git switch fixup\-branch # switch to another branch
506 $ git stash pop # to finish work on the saved changes
513 Recovering stash entries that were cleared/dropped erroneously
515 If you mistakenly drop or clear stash entries, they cannot be recovered through the normal safety mechanisms\&. However, you can try the following incantation to get a list of stash entries that are still in your repository, but not reachable any more:
521 git fsck \-\-unreachable |
522 grep commit | cut \-d\e \-f3 |
523 xargs git log \-\-merges \-\-no\-walk \-\-grep=WIP
531 Everything below this line in this section is selectively included from the \fBgit-config\fR(1) documentation\&. The content is the same as what\(cqs found there:
533 stash\&.showIncludeUntracked
535 If this is set to true, the
537 command will show the untracked files of a stash entry\&. Defaults to false\&. See the description of the
540 \fBgit-stash\fR(1)\&.
545 If this is set to true, the
547 command without an option will show the stash entry in patch form\&. Defaults to false\&. See the description of the
550 \fBgit-stash\fR(1)\&.
555 If this is set to true, the
557 command without an option will show a diffstat of the stash entry\&. Defaults to true\&. See the description of the
560 \fBgit-stash\fR(1)\&.
564 \fBgit-checkout\fR(1), \fBgit-commit\fR(1), \fBgit-reflog\fR(1), \fBgit-reset\fR(1), \fBgit-switch\fR(1)
567 Part of the \fBgit\fR(1) suite