Git command to save a stash without modifying working tree?

You need to pass the message to update-ref, not stash create as stash create doesn't take a message (it doesn't update any ref, so it has no reflog entry to populate).

git update-ref -m "Stash message" refs/stash "$(git stash create)"

git stash store "$(git stash create)"

Will create stash entry similar to what you would get with git stash without actually touching and clearing your working directory and index.

If you check stash list or look at all commit graph (including stash) you'll see that it's similar result to what you would get with normal call to git stash. Just the message in stash list is different (normally it's something like "stash@{0}: WIP on master: 14e009e init commit", here we'll get "stash@{0}: Created via "git stash store"")

$ git status --short
M file.txt
A  file2.txt

$ git stash list

$ git stash store "$(git stash create)"

$ git stash list
stash@{0}: Created via "git stash store".

$ git stash show 'stash@{0}'
 file.txt  | 2 +-
 file2.txt | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)

$ git log --oneline --graph --all
*   85f937b (refs/stash) WIP on master: 14e009e init commit
|\
| * 26295a3 index on master: 14e009e init commit
|/
* 14e009e (HEAD -> master) init commit

$ git status
M file.txt
A  file2.txt

A bit more explanation:

A git stash entry is represented using normal commits with some defined structure. Basically it is a regular commit object that has 2 parents (or 3 if you use --include-untracked option) (more info 1,2).

git stash create creates this commits that represents stash entry and returns you the object name (SHA-1) of commit object (the one that has 2 or 3 parents). It is a dangling commit (you can verify it by calling git fsck after git stash create). You need to make refs/stash point to this dangling commit and you do it by git stash store (or by git update-ref like in other answers, because git stash store uses git update-ref to do its work).

It's good to look at actual source code of git stash push and see that it's basically calling git stash create and git stash store and then does some logic to clean files (which one depends on what options you used in git stash push).


Thanks to Charles' tip, I whipped up a bash script to do exactly what I wanted (I was running into issues implementing this as only an alias). It takes an optional stash message just like git stash save. If none is supplied it will use the default message generated by git stash.

#!/bin/sh
#
# git-stash-snap
# Save snapshot of working tree into 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 log --no-walk --pretty="tformat:%-s" "$HASH"`
        git update-ref -m "$MESSAGE" refs/stash "$HASH"
fi

Edit: As pointed out in a comment below, saving this script as git-stash-snap somewhere in your path is sufficient to be able to invoke it by typing git stash-snap.

The nice thing here is that even if you drop a stash made with this method, you will still be able to see the stash message using git log [commit-hash] of the dangling commit!

Edit: since git 2.6.0 you can add --create-reflog to update-ref and then git stash list will show this even if git stash was not used before.

Edit: Git has introduced a new stash subcommand called stash push so I have updated my recommendation for naming this script from git-stash-push to git-stash-snap.