【问题标题】:How to undo an accidental git stash pop with the same stash message?如何使用相同的存储消息撤消意外的 git stash pop?
【发布时间】:2018-10-02 17:51:17
【问题描述】:

我知道git 存储只是本地的,消息不会暴露给任何远程方,因此消息的相关性远低于提交消息。

由于git stash pop 没有记录在git reflog 中,我似乎没有找到一种方法来获取已明确传递给git stash save 的消息或任何其他方式来检索它。

我经常在 stash 消息中写入信息,我为什么要隐藏以及隐藏的实现有多远,所以它们对我来说非常有价值。

我知道使用分支比使用git stash 有很多优势,几乎没有缺点。我正在养成不再使用git stash 来支持分支的习惯,但是我已经丢失了消息并想澄清一下。

我的git stash pop 输出看起来像

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)

【问题讨论】:

    标签: git


    【解决方案1】:

    首先,git stash save is now called git stash push 从 Git 2.16+(2017 年第四季度)开始

    你可以看到在t/t3903-stash.sh中测试的消息,使用git stash list(但那是之前git stash pop

    git stash 仍然是 shell script,你可以看到,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")"
    

    因此,git log 能够取回该提交消息是理所当然的。

    【讨论】:

    • 更准确地说,一个 stash 至少是两个,有时是三个,提交;但是名称stash 专门指向这个$w_commit 一个,它是具有存储名称的那个。所以git log -1 stash 在删除之前或git log -1 &lt;hash&gt; 之后(前提是提交还没有被垃圾收集),会得到你的消息。
    • @torek 同意了,现在我仔细看看源代码 ;)
    猜你喜欢
    • 2015-02-10
    • 2014-04-08
    • 1970-01-01
    • 1970-01-01
    • 2017-11-25
    • 2013-02-23
    • 1970-01-01
    • 2012-12-25
    相关资源
    最近更新 更多