如何在 Git 中恢复丢失的存储? [英] How to recover a dropped stash in Git?

查看:53
本文介绍了如何在 Git 中恢复丢失的存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常使用 git stashgit stash pop 来保存和恢复我的工作树中的更改.昨天我对我的工作树进行了一些更改,这些更改被我隐藏和弹出,然后我对我的工作树进行了更多更改.我想回去查看昨天的隐藏更改,但 git stash pop 似乎删除了对相关提交的所有引用.

I frequently use git stash and git stash pop to save and restore changes in my working tree. Yesterday I had some changes in my working tree that I had stashed and popped, and then I made more changes to my working tree. I'd like to go back and review yesterday's stashed changes, but git stash pop appears to remove all references to the associated commit.

我知道如果我使用 git stash 那么 .git/refs/stash 包含用于创建存储的提交的引用.而 .git/logs/refs/stash 包含整个存储.但是那些引用在 git stash pop 之后就消失了.我知道提交仍在我的存储库中的某个地方,但我不知道它是什么.

I know that if I use git stash then .git/refs/stash contains the reference of the commit used to create the stash. And .git/logs/refs/stash contains the whole stash. But those references are gone after git stash pop. I know that the commit is still in my repository somewhere, but I don't know what it was.

是否有一种简单的方法可以恢复昨天的存储提交引用?

Is there an easy way to recover yesterday's stash commit reference?

请注意,这对我今天来说并不重要,因为我每天都有备份,并且可以返回昨天的工作树来获取我的更改.我问是因为一定有更简单的方法!

Note that this isn't critical for me today because I have daily backups and can go back to yesterday's working tree to get my changes. I'm asking because there must be an easier way!

推荐答案

一旦您知道您删除的存储提交的哈希值,您就可以将其作为存储应用:

Once you know the hash of the stash commit you dropped, you can apply it as a stash:

git stash apply $stash_hash

或者,您可以使用

git branch recovered $stash_hash

之后,您可以使用所有普通工具为所欲为.完成后,只需将树枝吹走即可.

After that, you can do whatever you want with all the normal tools. When you’re done, just blow the branch away.

如果您刚刚弹出它并且终端仍然打开,您将仍然有 git stash pop 在屏幕上打印的哈希值(谢谢,Dolda).

If you have only just popped it and the terminal is still open, you will still have the hash value printed by git stash pop on screen (thanks, Dolda).

否则,您可以在 Linux、Unix 或 Git Bash for Windows 上找到它:

Otherwise, you can find it using this for Linux, Unix or Git Bash for Windows:

git fsck --no-reflog | awk '/dangling commit/ {print $3}'

...或使用 Windows 的 Powershell:

...or using Powershell for Windows:

git fsck --no-reflog | select-string 'dangling commit' | foreach { $_.ToString().Split(" ")[2] }

这将在您的提交图的提示处向您显示所有提交,这些提交不再从任何分支或标签中引用——每个丢失的提交,包括您曾经创建的每个存储提交,都将在该图中的某个位置.

This will show you all the commits at the tips of your commit graph which are no longer referenced from any branch or tag – every lost commit, including every stash commit you’ve ever created, will be somewhere in that graph.

找到您想要的存储提交的最简单方法可能是将该列表传递给 gitk:

The easiest way to find the stash commit you want is probably to pass that list to gitk:

gitk --all $( git fsck --no-reflog | awk '/dangling commit/ {print $3}' )

...或者如果使用 Windows 版 Powershell,请参阅 emragins 的答案.

...or see the answer from emragins if using Powershell for Windows.

这将启动一个存储库浏览器,显示存储库中的每个提交,无论它是否可访问.

This will launch a repository browser showing you every single commit in the repository ever, regardless of whether it is reachable or not.

如果您更喜欢控制台上的漂亮图形而不是单独的 GUI 应用程序,则可以将 gitk 替换为 git log --graph --oneline --decorate.

You can replace gitk there with something like git log --graph --oneline --decorate if you prefer a nice graph on the console over a separate GUI app.

要发现存储提交,请查找以下形式的提交消息:

To spot stash commits, look for commit messages of this form:

somebranch 上的 WIP:commithash 一些旧的提交消息

注意:如果您在执行 git stash 时未提供消息,则提交消息只会采用这种形式(以WIP on"开头).

Note: The commit message will only be in this form (starting with "WIP on") if you did not supply a message when you did git stash.

这篇关于如何在 Git 中恢复丢失的存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆