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.45.2.648.g1e1586e4ed
10 .TH "GIT\-STASH" "1" "2024\-06\-24" "Git 2\&.45\&.2\&.648\&.g1e1586" "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>
54 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\&.
56 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\&.
58 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)\&.
61 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>\&...]
63 Save your local modifications to a new
65 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\&.
67 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
69 which acts as alias for
71 and pathspec elements, which are allowed after a double hyphen
76 save [\-p|\-\-patch] [\-S|\-\-staged] [\-k|\-\-[no\-]keep\-index] [\-u|\-\-include\-untracked] [\-a|\-\-all] [\-q|\-\-quiet] [<message>]
78 This option is deprecated in favour of
79 \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\&.
84 List the stash entries that you currently have\&. Each
86 is listed with its name (e\&.g\&.
90 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\&.
96 stash@{0}: WIP on submit: 6ebd0e2\&.\&.\&. Update git\-stash documentation
97 stash@{1}: On master: 9cc0589\&.\&.\&. Add git\-stash
103 The command takes options applicable to the
105 command to control what is shown and how\&. See
109 show [\-u|\-\-include\-untracked|\-\-only\-untracked] [<diff\-options>] [<stash>]
111 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
114 \fBgit stash show \-p stash@{1}\fR
115 to view the second most recent entry in patch form)\&. If no
117 is provided, the default behavior will be given by the
118 \fBstash\&.showStat\fR, and
119 \fBstash\&.showPatch\fR
120 config variables\&. You can also use
121 \fBstash\&.showIncludeUntracked\fR
123 \fB\-\-include\-untracked\fR
124 is enabled by default\&.
127 pop [\-\-index] [\-q|\-\-quiet] [<stash>]
129 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
130 \fBgit stash push\fR\&. The working directory must match the index\&.
132 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
134 manually afterwards\&.
137 apply [\-\-index] [\-q|\-\-quiet] [<stash>]
140 \fBpop\fR, but do not remove the state from the stash list\&. Unlike
143 may be any commit that looks like a commit created by
146 \fBstash create\fR\&.
149 branch <branchname> [<stash>]
151 Creates and checks out a new branch named
153 starting from the commit at which the
155 was originally created, applies the changes recorded in
157 to the new working tree and index\&. If that succeeds, and
159 is a reference of the form
160 \fBstash@{<revision>}\fR, it then drops the
163 This is useful if the branch on which you ran
165 has changed enough that
166 \fBgit stash apply\fR
167 fails due to conflicts\&. Since the stash entry is applied on top of the commit that was HEAD at the time
169 was run, it restores the originally stashed state with no conflicts\&.
174 Remove all the stash entries\&. Note that those entries will then be subject to pruning, and may be impossible to recover (see
176 below for a possible strategy)\&.
179 drop [\-q|\-\-quiet] [<stash>]
181 Remove a single stash entry from the list of stash entries\&.
186 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\&.
191 Store a given stash created via
192 \fIgit stash create\fR
193 (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\&.
199 This option is only valid for
205 All ignored and untracked files are also stashed and then cleaned up with
209 \-u, \-\-include\-untracked, \-\-no\-include\-untracked
215 commands, all untracked files are also stashed and then cleaned up with
220 command, show the untracked files in the stash entry as part of the diff\&.
225 This option is only valid for the
229 Show only the untracked files in the stash entry as part of the diff\&.
234 This option is only valid for
240 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)\&.
243 \-k, \-\-keep\-index, \-\-no\-keep\-index
245 This option is only valid for
251 All changes already added to the index are left intact\&.
256 This option is only valid for
262 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
264 to learn how to operate the
271 \fB\-\-keep\-index\fR\&. You can use
272 \fB\-\-no\-keep\-index\fR
278 This option is only valid for
284 Stash only the changes that are currently staged\&. This is similar to basic
286 except the state is committed to the stash instead of current branch\&.
290 option has priority over this one\&.
293 \-\-pathspec\-from\-file=<file>
295 This option is only valid for
299 Pathspec is passed in
301 instead of commandline args\&. If
305 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
306 \fBcore\&.quotePath\fR
308 \fBgit-config\fR(1))\&. See also
309 \fB\-\-pathspec\-file\-nul\fR
311 \fB\-\-literal\-pathspecs\fR\&.
314 \-\-pathspec\-file\-nul
316 This option is only valid for
321 \fB\-\-pathspec\-from\-file\fR\&. Pathspec elements are separated with NUL character and all other characters are taken literally (including newlines and quotes)\&.
326 This option is only valid for
335 Quiet, suppress feedback messages\&.
340 This option is only valid for
344 Separates pathspec from options for disambiguation purposes\&.
349 This option is only valid for
353 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\&.
355 For more details, see the
358 \fBgitglossary\fR(7)\&.
363 This option is only valid for
371 A reference of the form
372 \fBstash@{<revision>}\fR\&. When no
374 is given, the latest stash is assumed (that is,
379 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:
393 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\&.
396 Pulling into a dirty tree
398 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
400 will let you move forward\&.
402 However, there are cases in which your local changes do conflict with the upstream changes, and
404 refuses to overwrite your changes\&. In such a case, you can stash your changes away, perform a pull, and then unstash, like this:
412 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 \&.\&.\&.
466 Testing partial commits
469 \fBgit stash push \-\-keep\-index\fR
470 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:
476 # \&.\&.\&. hack hack hack \&.\&.\&.
477 $ git add \-\-patch foo # add just first part to the index
478 $ git stash push \-\-keep\-index # save all other changes to the stash
479 $ edit/build/test first part
480 $ git commit \-m \*(AqFirst part\*(Aq # commit fully tested change
481 $ git stash pop # prepare to work on all other changes
482 # \&.\&.\&. repeat above five steps until one commit remains \&.\&.\&.
483 $ edit/build/test remaining parts
484 $ git commit foo \-m \*(AqRemaining parts\*(Aq
492 Saving unrelated changes for future use
494 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
495 \fBgit stash push \-\-staged\fR
496 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\&.
502 # \&.\&.\&. hack hack hack \&.\&.\&.
503 $ git add \-\-patch foo # add unrelated changes to the index
504 $ git stash push \-\-staged # save these changes to the stash
505 # \&.\&.\&. hack hack hack, finish current changes \&.\&.\&.
506 $ git commit \-m \*(AqMassive\*(Aq # commit fully tested changes
507 $ git switch fixup\-branch # switch to another branch
508 $ git stash pop # to finish work on the saved changes
516 Recovering stash entries that were cleared/dropped erroneously
518 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:
524 git fsck \-\-unreachable |
525 grep commit | cut \-d\e \-f3 |
526 xargs git log \-\-merges \-\-no\-walk \-\-grep=WIP
535 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:
537 stash\&.showIncludeUntracked
539 If this is set to true, the
541 command will show the untracked files of a stash entry\&. Defaults to false\&. See the description of the
544 \fBgit-stash\fR(1)\&.
549 If this is set to true, the
551 command without an option will show the stash entry in patch form\&. Defaults to false\&. See the description of the
554 \fBgit-stash\fR(1)\&.
559 If this is set to true, the
561 command without an option will show a diffstat of the stash entry\&. Defaults to true\&. See the description of the
564 \fBgit-stash\fR(1)\&.
568 \fBgit-checkout\fR(1), \fBgit-commit\fR(1), \fBgit-reflog\fR(1), \fBgit-reset\fR(1), \fBgit-switch\fR(1)
571 Part of the \fBgit\fR(1) suite