3 .\" Author: [FIXME: author] [see http://www.docbook.org/tdg5/en/html/author]
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
7 .\" Source: Git 2.38.1.220.g9388e93f00
10 .TH "GIT\-STASH" "1" "10/27/2022" "Git 2\&.38\&.1\&.220\&.g9388e9" "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 | apply ) [\-\-index] [\-q|\-\-quiet] [<stash>]
39 \fIgit stash\fR branch <branchname> [<stash>]
40 \fIgit stash\fR [push [\-p|\-\-patch] [\-S|\-\-staged] [\-k|\-\-[no\-]keep\-index] [\-q|\-\-quiet]
41 [\-u|\-\-include\-untracked] [\-a|\-\-all] [\-m|\-\-message <message>]
42 [\-\-pathspec\-from\-file=<file> [\-\-pathspec\-file\-nul]]
43 [\-\-] [<pathspec>\&...]]
45 \fIgit stash\fR create [<message>]
46 \fIgit stash\fR store [\-m|\-\-message <message>] [\-q|\-\-quiet] <commit>
51 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\&.
53 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\&.
55 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)\&.
58 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>\&...]
60 Save your local modifications to a new
62 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\&.
64 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
66 which acts as alias for
68 and pathspec elements, which are allowed after a double hyphen
73 save [\-p|\-\-patch] [\-S|\-\-staged] [\-k|\-\-[no\-]keep\-index] [\-u|\-\-include\-untracked] [\-a|\-\-all] [\-q|\-\-quiet] [<message>]
75 This option is deprecated in favour of
76 \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\&.
81 List the stash entries that you currently have\&. Each
83 is listed with its name (e\&.g\&.
87 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\&.
93 stash@{0}: WIP on submit: 6ebd0e2\&.\&.\&. Update git\-stash documentation
94 stash@{1}: On master: 9cc0589\&.\&.\&. Add git\-stash
100 The command takes options applicable to the
102 command to control what is shown and how\&. See
106 show [\-u|\-\-include\-untracked|\-\-only\-untracked] [<diff\-options>] [<stash>]
108 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
111 \fBgit stash show \-p stash@{1}\fR
112 to view the second most recent entry in patch form)\&. If no
114 is provided, the default behavior will be given by the
115 \fBstash\&.showStat\fR, and
116 \fBstash\&.showPatch\fR
117 config variables\&. You can also use
118 \fBstash\&.showIncludeUntracked\fR
120 \fB\-\-include\-untracked\fR
121 is enabled by default\&.
124 pop [\-\-index] [\-q|\-\-quiet] [<stash>]
126 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
127 \fBgit stash push\fR\&. The working directory must match the index\&.
129 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
131 manually afterwards\&.
134 apply [\-\-index] [\-q|\-\-quiet] [<stash>]
137 \fBpop\fR, but do not remove the state from the stash list\&. Unlike
140 may be any commit that looks like a commit created by
143 \fBstash create\fR\&.
146 branch <branchname> [<stash>]
148 Creates and checks out a new branch named
150 starting from the commit at which the
152 was originally created, applies the changes recorded in
154 to the new working tree and index\&. If that succeeds, and
156 is a reference of the form
157 \fBstash@{<revision>}\fR, it then drops the
160 This is useful if the branch on which you ran
162 has changed enough that
163 \fBgit stash apply\fR
164 fails due to conflicts\&. Since the stash entry is applied on top of the commit that was HEAD at the time
166 was run, it restores the originally stashed state with no conflicts\&.
171 Remove all the stash entries\&. Note that those entries will then be subject to pruning, and may be impossible to recover (see
173 below for a possible strategy)\&.
176 drop [\-q|\-\-quiet] [<stash>]
178 Remove a single stash entry from the list of stash entries\&.
183 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\&.
188 Store a given stash created via
189 \fIgit stash create\fR
190 (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\&.
196 This option is only valid for
202 All ignored and untracked files are also stashed and then cleaned up with
206 \-u, \-\-include\-untracked, \-\-no\-include\-untracked
212 commands, all untracked files are also stashed and then cleaned up with
217 command, show the untracked files in the stash entry as part of the diff\&.
222 This option is only valid for the
226 Show only the untracked files in the stash entry as part of the diff\&.
231 This option is only valid for
237 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)\&.
240 \-k, \-\-keep\-index, \-\-no\-keep\-index
242 This option is only valid for
248 All changes already added to the index are left intact\&.
253 This option is only valid for
259 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 \(lqInteractive Mode\(rq section of
261 to learn how to operate the
268 \fB\-\-keep\-index\fR\&. You can use
269 \fB\-\-no\-keep\-index\fR
275 This option is only valid for
281 Stash only the changes that are currently staged\&. This is similar to basic
283 except the state is committed to the stash instead of current branch\&.
287 option has priority over this one\&.
290 \-\-pathspec\-from\-file=<file>
292 This option is only valid for
296 Pathspec is passed in
298 instead of commandline args\&. If
302 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
303 \fBcore\&.quotePath\fR
305 \fBgit-config\fR(1))\&. See also
306 \fB\-\-pathspec\-file\-nul\fR
308 \fB\-\-literal\-pathspecs\fR\&.
311 \-\-pathspec\-file\-nul
313 This option is only valid for
318 \fB\-\-pathspec\-from\-file\fR\&. Pathspec elements are separated with NUL character and all other characters are taken literally (including newlines and quotes)\&.
323 This option is only valid for
332 Quiet, suppress feedback messages\&.
337 This option is only valid for
341 Separates pathspec from options for disambiguation purposes\&.
346 This option is only valid for
350 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\&.
352 For more details, see the
355 \fBgitglossary\fR(7)\&.
360 This option is only valid for
368 A reference of the form
369 \fBstash@{<revision>}\fR\&. When no
371 is given, the latest stash is assumed (that is,
376 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:
390 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\&.
393 Pulling into a dirty tree
395 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
397 will let you move forward\&.
399 However, there are cases in which your local changes do conflict with the upstream changes, and
401 refuses to overwrite your changes\&. In such a case, you can stash your changes away, perform a pull, and then unstash, like this:
409 file foobar not up to date, cannot merge\&.
422 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:
428 # \&.\&.\&. hack hack hack \&.\&.\&.
429 $ git switch \-c my_wip
430 $ git commit \-a \-m "WIP"
433 $ git commit \-a \-m "Fix in a hurry"
435 $ git reset \-\-soft HEAD^
436 # \&.\&.\&. continue hacking \&.\&.\&.
444 to simplify the above, like this:
450 # \&.\&.\&. hack hack hack \&.\&.\&.
453 $ git commit \-a \-m "Fix in a hurry"
455 # \&.\&.\&. continue hacking \&.\&.\&.
463 Testing partial commits
466 \fBgit stash push \-\-keep\-index\fR
467 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:
473 # \&.\&.\&. hack hack hack \&.\&.\&.
474 $ git add \-\-patch foo # add just first part to the index
475 $ git stash push \-\-keep\-index # save all other changes to the stash
476 $ edit/build/test first part
477 $ git commit \-m \(aqFirst part\(aq # commit fully tested change
478 $ git stash pop # prepare to work on all other changes
479 # \&.\&.\&. repeat above five steps until one commit remains \&.\&.\&.
480 $ edit/build/test remaining parts
481 $ git commit foo \-m \(aqRemaining parts\(aq
489 Saving unrelated changes for future use
491 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
492 \fBgit stash push \-\-staged\fR
493 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\&.
499 # \&.\&.\&. hack hack hack \&.\&.\&.
500 $ git add \-\-patch foo # add unrelated changes to the index
501 $ git stash push \-\-staged # save these changes to the stash
502 # \&.\&.\&. hack hack hack, finish curent changes \&.\&.\&.
503 $ git commit \-m \(aqMassive\(aq # commit fully tested changes
504 $ git switch fixup\-branch # switch to another branch
505 $ 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
532 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:
534 stash\&.showIncludeUntracked
536 If this is set to true, the
538 command will show the untracked files of a stash entry\&. Defaults to false\&. See description of
541 \fBgit-stash\fR(1)\&.
546 If this is set to true, the
548 command without an option will show the stash entry in patch form\&. Defaults to false\&. See description of
551 \fBgit-stash\fR(1)\&.
556 If this is set to true, the
558 command without an option will show diffstat of the stash entry\&. Defaults to true\&. See description of
561 \fBgit-stash\fR(1)\&.
565 \fBgit-checkout\fR(1), \fBgit-commit\fR(1), \fBgit-reflog\fR(1), \fBgit-reset\fR(1), \fBgit-switch\fR(1)
568 Part of the \fBgit\fR(1) suite