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

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

问题描述

我经常使用 git stash git stash pop 来保存和恢复工作树中的更改。昨天我在我的工作树上发生了一些变化,我把它藏起来并弹出,然后我对工作树做了更多的修改。我想回顾一下,查看昨天存储的更改,但是 git stash pop 似乎会删除对相关提交的所有引用。



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



有没有简单的方法来恢复昨天的存储提交引用? / p>

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

解决方案

如果你刚才弹出它,终端仍然打开,你将仍然有由 git stash打印的散列值pop 在屏幕上(谢谢Dolda)。

否则,你可以在Linux和Unix上找到它:

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

和Windows:

  git fsck --no-reflog |选择字符串'dangling commit'| foreach {$ bits = $ _ -split''; echo $ bits [2];} 

这会告诉你提交提交的所有提交图表,它们不再被任何分支或标签引用 - 每一次丢失的提交,包括你创建的每一个提交提交,都会在该图表中的某处。



找到你想要的隐藏提交的方式可能是将该列表传递给 gitk

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

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



你可以用来代替 gitk > git log --graph --oneline --decorate 如果你喜欢在一个单独的GUI应用程序上的控制台上的一个很好的图。



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


$ b

在制品上 somebranch commithash一些旧的提交信息


$ b 注意如果您在 git stash 时未提供消息,则以此形式(以WIP on开头)。



  git stash apply 

git stash apply $ stash_hash

或者您可以使用 gitk code>为您感兴趣的任何无法访问的提交创建分支。之后,您可以使用所有常规工具对它们进行任何操作。完成后,再将这些分支吹走。

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.

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!

解决方案

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).

Otherwise, you can find it using this for Linux and Unix:

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

and for Windows:

git fsck --no-reflog | select-string 'dangling commit' | foreach { $bits = $_ -split ' '; echo $bits[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.

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}' )

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

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:

        WIP on somebranch: commithash Some old commit message

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.

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

git stash apply $stash_hash

Or you can use the context menu in gitk to create branches for any unreachable commits you are interested in. After that, you can do whatever you want with them with all the normal tools. When you’re done, just blow those branches away again.

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

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