Dan Rosenstark
Dan Rosenstark

Reputation: 69747

"git stash create x" - Where is it?

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?

Upvotes: 10

Views: 7262

Answers (3)

Dan Loewenherz
Dan Loewenherz

Reputation: 11226

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

Upvotes: 9

Dan Rosenstark
Dan Rosenstark

Reputation: 69747

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}

Upvotes: 4

sehe
sehe

Reputation: 392893

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

Upvotes: 3

Related Questions