Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to undo an accidental git stash pop with the same stash message?

Tags:

git

I'm aware that git stashs are local only and the message will not be exposed to any remote party and the message is thus much less relevant than commit messages.

I don't seem to find a way to get the message which has been explicitly passed to git stash save or any other way to retrieve it since git stash pop is not recorded in git reflog.

I often write information into the stash message why I stashed and how far the stashed implementation is, so they're pretty valuable to me.

I'm aware the using branching has a lot of advantages over using git stash with almost no downsides. I'm in the transition of getting the habit of no longer using git stash in favor of branches, however I already lost messages and would like to clarify this once.

My git stash pop output looks like

On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   test

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (958d4b921e7f3e8faa9fd2ecb12af13250e1f739)
like image 411
Kalle Richter Avatar asked Dec 06 '25 04:12

Kalle Richter


1 Answers

First, git stash save is now called git stash push since Git 2.16+ (Q4 2017)

You can see the message tested in t/t3903-stash.sh, using git stash list (but that is before a git stash pop)

git stash is still a shell script, and you can see that, when it creates the stash, it actually creates a commit:

    # create the stash
    if test -z "$stash_msg"
    then
        stash_msg=$(printf 'WIP on %s' "$msg")
    else
        stash_msg=$(printf 'On %s: %s' "$branch" "$stash_msg")
    fi
    w_commit=$(printf '%s\n' "$stash_msg" |
    git commit-tree $w_tree -p $b_commit -p $i_commit $untracked_commit_option) ||
die "$(gettext "Cannot record working tree state")"

It would then stands to reason that a git log is able to get back that commit message.

like image 131
VonC Avatar answered Dec 07 '25 16:12

VonC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!