使用 SourceTree 在 Git 中检索已删除的存储

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17743078/
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-10 16:35:36  来源:igfitidea点击:

Retrieve deleted stash in Git using SourceTree

gitatlassian-sourcetree

提问by tusharmath

I am using source tree. I had created a stash of multiple changes and by mistake deleted it. Is there a way to retrieve them back?

我正在使用源树。我创建了一个包含多个更改的存储库,并错误地将其删除。有没有办法把它们找回来?

采纳答案by Jan Krüger

The stash is saved internally as a merge commit referenced from a list of stashes.

stash 在内部保存为从 stash 列表引用的合并提交。

git fsckcan find dangling objects. It will find not just your deleted stash, but probably other stuff, too... so you'll be wanting to look for commits that look like they could be your stash (git show <ID>to display relevant info about an object and decide whether it's the one you're looking for).

git fsck可以找到悬空的物体。它会找到不只是你删除的藏匿处,但可能是其他的东西,太......所以你会想要寻找的提交,看上去就像他们可能是你的藏匿处(git show <ID>显示相关信息有关的对象,并决定是否它是一个您正在寻找)。

Once you have that, all you need to do is re-insert it into the list of stashes. The list is stored in .git/logs/refs/stashand a line has the following format:

一旦你有了它,你需要做的就是将它重新插入到隐藏列表中。该列表存储在.git/logs/refs/stash,一行具有以下格式:

<ID of previous stash commit in list or 0000000000000000000000000000000000000000 if none> <ID of merge commit> Your Name <[email protected]> <UNIX timestamp> <time zone, e.g. +0000><TAB char><description of stash>

Here's a working example:

这是一个工作示例:

16b9a2d400dafe7ea25592029e3e5582d025c7d8 5def7605dfe625e8b3a3152fe52a87cc36694b6a Jan Krüger <email.censored@invalid> 1374227992 +0200  WIP on master: 0dbd812 Update draft release notes to 1.8.4

Just synthesize a line for the stash you want to re-insert (the name/mail/timestamp/description don't haveto be accurate) and you should be able to use it normally again.

只是合成为要重新插入(名称/邮件/时间戳/描述不藏匿线是准确的),你应该能够再次正常使用。

Happy hunting!

狩猎快乐!

回答by ishahak

Based on the above answers, here is a simple sequence:

基于以上答案,这里是一个简单的序列:

Open a terminal window and cd into a folder under the repository. Then:

打开终端窗口并 cd 进入存储库下的文件夹。然后:

git fsck | awk '{print }' > tmp.txt
cat tmp.txt | xargs git show > tmp2.txt

Now open tmp2.txtin editor, locate your lost code, and find the commit-idon top of it. Then apply the code:

现在在编辑器中打开tmp2.txt,找到丢失的代码,并在其上找到commit-id。然后应用代码:

git stash apply <commit id>
rm tmp.txt tmp2.txt

This saved my life!I really thank all those who answered this question. I bless the git creator Linus Torvaldsfor keeping deleted stuff in the git database. Genius!!

这救了我的命!我真的很感谢所有回答这个问题的人。我祝福 git 创建者Linus Torvalds将已删除的内容保留在 git 数据库中。天才!!

回答by qqx

Like the previous answer states, you can use git fsckto list objects that aren't referenced by anything which would include your deleted stash. But, it is possible to use git showto filter that list of objects to show only stashes like:

与之前的答案状态一样,您可以使用git fsck列出未包含已删除存储的任何对象引用的对象。但是,可以使用git show过滤该对象列表以仅显示以下内容:

git fsck 2> /dev/null |
  awk '/commit/{print }' |
  git show --stdin --merges --grep '^WIP on'

If you know when the stash was created, you could also add an argument like --since '2 days ago'to the final line to limit the output further. Hopefully that will cut the list down to a manageable size.

如果您知道 stash 是何时创建的,您还--since '2 days ago'可以在最后一行添加一个参数 以进一步限制输出。希望这将把列表缩减到一个可管理的大小。

Once you've found the correct stash make note of its commit ID, and you can use git stash apply COMMITIDto apply it as if it hadn't been deleted.

一旦你找到了正确的 stash,记下它的提交 ID,你可以用 git stash apply COMMITID它来应用它,就好像它没有被删除一样。

回答by Roddy T.

As Jan Krüger states above, git fsckis the way to go. However, if you find yourself unable (for whatever reason) to successfully synthesize a line in your stash file and make the stash appear in the available list, you can use git stash apply <guid>directly, without adding the line. This will immediately apply (not commit) the file(s) changes to your current branch.

正如 Jan Krüger 上面所说,这git fsck是要走的路。但是,如果您发现自己无法(无论出于何种原因)成功合成存储文件中的一行并使存储出现在可用列表中,则可以git stash apply <guid>直接使用,而无需添加该行。这将立即将文件更改应用(不提交)到您当前的分支。

回答by hcknl

Another solution is:

另一种解决方案是:

git fsck 2>&1 | awk '/dangling commit/{print  "^!"}' | xargs git log

find the author and commit informations(date,hash,author etc.)

查找作者和提交信息(日期、哈希、作者等)

git stash store <hash-id-of-specific-commit>

回答by Tengku Fathullah

This is the most cleaner workaround to recover deleted stash.

这是恢复已删除存储的最干净的解决方法。

  1. git fsck --lost-found

  2. ls -1 .git/lost-found/commit/ | xargs -n 1 git log -n 1 --pretty=oneline

  3. git stash apply [tag]

  1. git fsck --lost-found

  2. ls -1 .git/lost-found/commit/ | xargs -n 1 git log -n 1 --pretty=oneline

  3. git stash apply [tag]

enter image description here

在此处输入图片说明

Replace [tag] with the id, ex:

将 [tag] 替换为 id,例如:

git stash apply 40e47250d0b4fb6143be67c115b708be126e79d3

git stash 应用 40e47250d0b4fb6143be67c115b708be126e79d3

回答by Rados

It might be helpful to use comments while stashing, using:

在存储时使用注释可能会有所帮助,使用:

git stash save "comment"

It saved me some trouble finding already deleted stash using the following:

它使用以下方法为我省去了查找已删除存储的麻烦:

git fsck --lost-found

ls -1 .git/lost-found/commit/ | xargs -n 1 git log -n 1 --pretty=oneline

git stash apply [tag]

回答by Ankur Jain

for i in $(git fsck 2>|/dev/null | grep commit | cut -d' ' -f3); do git --no-pager log -1 $i; echo "-------------------------"; done | less

Then find the commit id#.

然后找到提交 id#。

and do

并做

git stash apply {commit#}