Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"git stash create x" - Where is it?

Tags:

git

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?

like image 758
Dan Rosenstark Avatar asked Jul 05 '11 21:07

Dan Rosenstark


People also ask

How do I find my git stash list?

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.

Where does git stash Save changes?

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.

What is git stash repository?

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.


3 Answers

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}
like image 195
Dan Rosenstark Avatar answered Oct 13 '22 22:10

Dan Rosenstark


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
like image 36
dwlz Avatar answered Oct 13 '22 21:10

dwlz


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

like image 25
sehe Avatar answered Oct 13 '22 21:10

sehe