如何撤消 git stash clear

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32517870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 11:22:31  来源:igfitidea点击:

How to undo git stash clear

git

提问by Ashish Banker

I execute git stash save "ABC" and the and by mistake I did git stash clear . How can I retrieve the data that was in stash ABC . Help please

我执行 git stash save "ABC" 并且错误地执行了 git stash clear 。如何检索存储在 ABC 中的数据。请帮忙

回答by mamuso

As it may be found in the documentation of git stash, you may be lucky if this works:

正如它可能在git stash 的文档中找到的那样,如果它有效,你可能很幸运:

Recovering stashes that were cleared/dropped erroneously

If you mistakenly drop or clear stashes, they cannot be recovered through the normal safety mechanisms. However, you can try the following incantation to get a list of stashes that are still in your repository, but not reachable any more:

git fsck --unreachable | grep commit | cut -d\ -f3 | xargs git log --merges --no-walk --grep=WIP

恢复被错误清除/丢弃的藏匿处

如果您错误地丢弃或清除了藏品,它们将无法通过正常的安全机制恢复。但是,您可以尝试以下咒语来获取仍在您的存储库中但无法再访问的存储列表:

git fsck --unreachable | grep 提交 | 剪切 -d\ -f3 | xargs git log --merges --no-walk --grep=WIP

If you find the stash you cleared by mistake, then you can do:

如果您发现错误清除的藏匿处,则可以执行以下操作:

git stash apply <stash>

EDIT:?Use this command instead git fsck --unreachable | grep commit | cut -d ' ' -f3 | xargs git log --merges --no-walk --grep=WIP

编辑:? 改用这个命令git fsck --unreachable | grep commit | cut -d ' ' -f3 | xargs git log --merges --no-walk --grep=WIP

回答by studog

All of the above answers end with a git stash apply [commit]which is good, but is not an exact undo of git stash clear. For that you need to re-stash the orphaned stash-commit. I found these instructionswhich almost worked but needed a flag to get all the way there. Summarizing:

以上所有答案都以 a 结尾,git stash apply [commit]这是好的,但不是git stash clear. 为此,您需要重新存储孤立的存储提交。我发现这些指令几乎有效,但需要一个标志才能到达那里。总结:

  1. Find the orphaned stash commits: git fsck --unreachable | grep commit | cut -d ' ' -f3 | xargs git log --merges --no-walk
  2. Re-stash the commit: git update-ref --create-reflog refs/stash 4b3fc45c94caadcc87d783064624585c194f4be8 -m "My recover stash"
  1. 找到孤立的存储提交: git fsck --unreachable | grep commit | cut -d ' ' -f3 | xargs git log --merges --no-walk
  2. 重新存储提交: git update-ref --create-reflog refs/stash 4b3fc45c94caadcc87d783064624585c194f4be8 -m "My recover stash"

回答by suhailvs

Run this command to find the commit:

运行此命令以查找提交:

git fsck --unreachable | grep commit | cut -d ' ' -f3 | xargs git log --merges --no-walk --grep=WIP

will list something like:

将列出类似的内容:

Checking object directories: 100% (256/256), done.
commit c36e565014d9a927c36f16e78bc327eb375d33b8
Merge: dff6bc1 4e05a0c
Author: suhailvs <[email protected]>
Date:   Thu Jul 19 13:32:01 2018 +0530

WIP on master: dff6bc1 added menu

then checkout that commit c36e565014:

然后结帐该提交c36e565014

git checkout c36e565014

回答by hushed_voice

I had to use

我不得不使用

git fsck --unreachable | grep commit | cut -d ' ' -f3 | xargs git log --merges --no-walk

to fetch all the orphan stash which will give an o/p similar to this:

获取所有孤儿藏匿处,这将提供类似于以下内容的 o/p:

Checking object directories: 100% (256/256), done.
Checking objects: 100% (395/395), done.
commit 3928306034b292770cc4cd2987c034ffad250e0b    //commit stash hash
Merge: 16056a0 ac3c354
Author: Jimmy <mail>
Date:   Thu Nov 14 17:31:05 2019 +0530

    On profile: stashing for

commit 50f6f3a7161dd44bfcef2b8328a2329db4c7ec34

and use

并使用

git apply stash 3928306034b292770cc4cd2987c034ffad250e0b

And I got my stashed changes back. Thanks to https://mobilejazz.com/blog/how-to-recover-a-deleted-git-stash/

我找回了我隐藏的更改。感谢https://mobilejazz.com/blog/how-to-recover-a-deleted-git-stash/

回答by Nathan Hanna

I also deleted a stash, but using the GitKraken gui, so I don't know exactly what git commands it executed. The chosen answer didn't work for me but put me on the right path at least.

我还删除了一个 stash,但使用 GitKraken gui,所以我不知道它执行了哪些 git 命令。选择的答案对我不起作用,但至少让我走上了正确的道路。

In my case, manually searching through --unreachable objects worked. I'm sure there is a more efficient way, but I'm just glad I was able to recover the changes.

就我而言,手动搜索 --unreachable 对象有效。我确信有一种更有效的方法,但我很高兴我能够恢复这些更改。

ids=`git fsck --unreachable | grep blob | cut -d ' ' -f3`
number_of_ids=`echo $ids | wc -l | tr -d '[:space:]'`
for i in {1..$number_of_ids}; do git show `echo $ids | sed -n ${i}p` > evaluate$i.rb;done; 

So this saves all the unreachable objects to files prefixed by "evaluate". I then opened all the files in a text editor (sublime for me subl evaluate*), and evaluated each file in turn, manually copying and pasting the file into the old version of the original file if it was a file from the stash I deleted.

因此,这会将所有无法访问的对象保存到以“评估”为前缀的文件中。然后我在文本编辑器中打开所有文件(对我来说是崇高的subl evaluate*),并依次评估每个文件,如果它是我删除的存储中的文件,则手动将文件复制并粘贴到原始文件的旧版本中。

Tips:

提示:

  • Change the .rbto match the file extension of the files you are looking for, to get appropriate syntax highlighting if you want it.
  • If your files aren't included, you can widen the scope of your search by removing the | grep blob(the blobs were where I found my files).
  • 更改.rb以匹配您要查找的文件的文件扩展名,以便在需要时获得适当的语法突出显示。
  • 如果您的文件未包含在内,您可以通过删除| grep blob(blob 是我找到我的文件的位置)来扩大搜索范围。