If I create a commit with git stash create whatever
I get a hash of the commit back, but I can't find that commit hash with git reflog
.
git log stash
doesn't work either, not does git stash list
.
How can I list the commits I create using git stash create
?
Git Stash List. The Git stash list command will pull up a list of your repository's stashes. Git will display all of your stashes and a corresponding stash index. Now, if you wish to view the contents of a specific stash, you can run the Git stash show command followed by stash@ and the desired index.
Git stash stores the changes you made to the working directory locally (inside your project's . git directory; /. git/refs/stash , to be precise) and allows you to retrieve the changes when you need them.
The git stash command takes your uncommitted changes (both staged and unstaged), saves them away for later use, and then reverts them from your working copy.
If you use the script in this answer, you can then do git stash list
.
#!/bin/sh
#
# git-stash-push
# Push working tree onto the stash without modifying working tree.
# First argument (optional) is the stash message.
if [ -n "$1" ]; then
git update-ref -m "$1" refs/stash "$(git stash create \"$1\")"
else
HASH=`git stash create`
MESSAGE=`git --no-pager log -1 --pretty="tformat:%-s" "$HASH"`
git update-ref -m "$MESSAGE" refs/stash "$HASH"
fi
Then you may actually want to get that commit back at some point. To do this, you can list the stashes using git stash list
which gives you something like this (remember, these can be dumb commit messages):
stash@{0}: WTF? Nothing is working
stash@{1}: it's all working perfectlY!
stash@{2}: blah2
Then you can restore, say, blah2
by running:
git stash pop stash@{2}
or as @Eliot points out, you can use this to not destroy your stash:
git stash apply stash@{2}
While the answer in https://stackoverflow.com/a/6589093/39155 technically works, the solution is outdated as of Git 2.9.0 and there is a built-in way to store danglish stash refs (git stash store
).
git stash create
creates a dangling commit and will not store the ref anywhere. You'll need to follow it up with a git stash store
if you want to save it. From the git-stash
manpage:
Create a stash (which is a regular commit object) and return its object name, without storing it anywhere in the ref namespace
In order to store it in the ref namespace and associate it with the cache ref, you need to run git stash store <commit>
. E.g.
$ git stash create
09eb9a97ad632d0825be1ece361936d1d0bdb5c7
$ git stash store 09eb9a97ad632d0825be1ece361936d1d0bdb5c7
$ git stash list
stash@{0}: Created via "git stash store".
If you want to associate a name with the stash ref, just pass -m / --message
to git stash store
.
$ git stash store -m "my stash" 09eb9a97ad632d0825be1ece361936d1d0bdb5c7
$ git stash list
stash@{0}: my stash
Edit
thx for telling me about a new feature(?)
The man page spells it out:
Create a stash (which is a regular commit object) and return its object name, without storing it anywhere in the ref namespace.
It is not stored anywhere in the ref namespace. You'll have to keep track of it. If you lost it,
git fsck --unreachable
may be able to provide a hint. Beware of expiration, so don't do git gc --prune=...
just then
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With