如何恢复Git中丢失的存储? [英] How to recover a dropped stash in 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}')
这将启动一个存储库浏览器,向您显示存储库中的每一次提交,无论它是否可访问。
你可以用来代替
如果你喜欢在一个单独的GUI应用程序上的控制台上的一个很好的图。 gitk
> git log --graph --oneline --decorate
要发现存储提交,请查找此形式的提交消息:
$ b
在制品上 somebranch : commithash一些旧的提交信息
$ b 注意如果您在
git stash
时未提供消息,则以此形式(以WIP on开头)。 git stash apply git stash apply $ stash_hash
或者您可以使用 I frequently use I know that if I use 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 Otherwise, you can find it using this for Linux and Unix: and for Windows: 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 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 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 Once you know the hash of the commit you want, you can apply it as a stash: Or you can use the context menu in 这篇关于如何恢复Git中丢失的存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! gitk code>为您感兴趣的任何无法访问的提交创建分支。之后,您可以使用所有常规工具对它们进行任何操作。完成后,再将这些分支吹走。
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
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.git stash pop
on screen (thanks, Dolda).git fsck --no-reflog | awk '/dangling commit/ {print $3}'
git fsck --no-reflog | select-string 'dangling commit' | foreach { $bits = $_ -split ' '; echo $bits[2];}
gitk
:gitk --all $( git fsck --no-reflog | awk '/dangling commit/ {print $3}' )
gitk
there with something like git log --graph --oneline --decorate
if you prefer a nice graph on the console over a separate GUI app.git stash
.git stash apply $stash_hash
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.