10

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?

Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421

3 Answers3

9

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
Community
  • 1
  • 1
Dan Loewenherz
  • 10,879
  • 7
  • 50
  • 81
  • 1
    Can you explain what "it will not store the ref anywhere" or "without storing it anywhere in the ref namespace" means? It means it won't get pushed to a remote repo? – Dan Rosenstark Jul 18 '16 at 19:26
  • 1
    It just means that while the object exists, and is in the local git database, there's no reference (e.g., tag, branch, etc.) pointing to it. – Dan Loewenherz Jul 28 '16 at 20:26
  • So if I merely do `git stash` there's no way to automatically push that to the remote repo without juggling the hashes, right? – Dan Rosenstark Jul 29 '16 at 14:42
  • Different command, but yep. `git stash` will only create a ref on your local checkout. AFAIK there is no way to push that to a remote without tagging or branching off of the ref. – Dan Loewenherz Jul 29 '16 at 15:42
  • from the manual: git stash create - 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" – marekmuratow Aug 16 '18 at 14:36
  • 1
    @dwlz: I have got a script that transfers the state of a worktree to another host, including uncommitted changes. I use `git stash create` to capture the state, push the commit using `git push --force origin :refs/anon/g`, fetch this special reference on the destination host and apply it using `git stash apply `. – hagello May 23 '22 at 17:57
4

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}
Community
  • 1
  • 1
Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
  • 4
    Also worth noting: `git stash pop` removes the stash from your stash list and applies the changes to your working copy, whereas `git stash apply` keeps the stash in the list and applies the changes. – Eliot Jul 12 '11 at 09:03
3

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

sehe
  • 374,641
  • 47
  • 450
  • 633
  • I do not mean `save`. I mean `create` because I do not want a `reset --hard` to be automatically run. – Dan Rosenstark Jul 05 '11 at 21:34
  • Ok. This is new to me. i don't _have_ git stash create. I just read the man page online, and it seems to me that it 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.` – sehe Jul 05 '11 at 21:36