Home Explore Blog CI



git

6th chunk of `Documentation/git-stash.adoc`
0ed49154c1f622dfbb65672a4400e2db6f68eef53e79147f0000000100000bbb
 my_wip
$ git reset --soft HEAD^
# ... continue hacking ...
----------------------------------------------------------------
+
You can use 'git stash' to simplify the above, like this:
+
----------------------------------------------------------------
# ... hack hack hack ...
$ git stash
$ edit emergency fix
$ git commit -a -m "Fix in a hurry"
$ git stash pop
# ... continue hacking ...
----------------------------------------------------------------

Testing partial commits::

You can use `git stash push --keep-index` 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:
+
----------------------------------------------------------------
# ... hack hack hack ...
$ git add --patch foo            # add just first part to the index
$ git stash push --keep-index    # save all other changes to the stash
$ edit/build/test first part
$ git commit -m 'First part'     # commit fully tested change
$ git stash pop                  # prepare to work on all other changes
# ... repeat above five steps until one commit remains ...
$ edit/build/test remaining parts
$ git commit foo -m 'Remaining parts'
----------------------------------------------------------------

Saving unrelated changes for future use::

When you are in the middle of massive changes and you find some
unrelated issue that you don't want to forget to fix, you can do the
change(s), stage them, and use `git stash push --staged` 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.
+
----------------------------------------------------------------
# ... hack hack hack ...
$ git add --patch foo           # add unrelated changes to the index
$ git stash push --staged       # save these changes to the stash
# ... hack hack hack, finish current changes ...
$ git commit -m 'Massive'       # commit fully tested changes
$ git switch fixup-branch       # switch to another branch
$ git stash pop                 # to finish work on the saved changes
----------------------------------------------------------------

Recovering stash entries that were cleared/dropped erroneously::

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:
+
----------------------------------------------------------------
git fsck --unreachable |
grep commit | cut -d\  -f3 |
xargs git log --merges --no-walk --grep=WIP
----------------------------------------------------------------

CONFIGURATION
-------------

include::includes/cmd-config-section-all.adoc[]

include::config/stash.adoc[]


SEE ALSO
--------
linkgit:git-checkout[1],
linkgit:git-commit[1],
linkgit:git-reflog[1],
linkgit:git-reset[1],
linkgit:git-switch[1]

GIT
---
Part of the linkgit:git[1] suite

Title: Advanced Git Stash Usage and Configuration
Summary
Git stash can be used for testing partial commits, saving unrelated changes, and even recovering dropped stash entries, with various options and configurations available, including `git stash push --keep-index` and `git stash push --staged`, to help manage complex development workflows and tasks.