我经常使用gitstash和gitstashpop来保存和恢复工作树中的更改。昨天,我在我的工作树中做了一些修改,这些修改是我藏起来并弹出的,然后我对工作树做了更多的修改。我想回去回顾一下昨天隐藏的更改,但gitstashpop似乎删除了对相关提交的所有引用。

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

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


当前回答

通过在终端中写入此命令,可以列出所有无法访问的提交-

git fsck --unreachable

检查无法访问的提交哈希-

git show hash

如果你找到了隐藏的物品,最后申请-

git stash apply hash

其他回答

通过在终端中写入此命令,可以列出所有无法访问的提交-

git fsck --unreachable

检查无法访问的提交哈希-

git show hash

如果你找到了隐藏的物品,最后申请-

git stash apply hash

gitfsck——不可访问|grep提交应该显示sha1,尽管它返回的列表可能很大。gitshow<sha1>将显示它是否是您想要的提交。

gitcherry-pick-m1<sha1>将提交合并到当前分支。

知道了大概的文件名及其位置,并且能够找到丢弃的隐藏文件,并将悬空提交添加到路径中

for i in $(git fsck --no-reflogs | awk '/dangling commit/ {print $3}'); do
  if git log -5 --name-only -u $i | grep -q "<path-to-files>/.*<partial-file-name>.*"; then
    echo "found something in commit $i";
  fi;
done

我来这里的目的是,不管我签了什么,都要如何真正拿回藏起来的东西。特别是,我藏了一些东西,然后检查了一个旧版本,然后打开了它,但在更早的时间点,这个藏的东西是不可用的,所以这个藏的就消失了;我不能只做git stash把它推回到堆栈上。这对我有用:

$ git checkout somethingOld
$ git stash pop
...
nothing added to commit but untracked files present (use "git add" to track)
Dropped refs/stash@{0} (27f6bd8ba3c4a34f134e12fe69bf69c192f71179)
$ git checkout 27f6bd8ba3c
$ git reset HEAD^    # Make the working tree differ from the parent.
$ git stash # Put the stash back in the stack.
Saved working directory and index state WIP on (no branch): c2be516 Some message.
HEAD is now at c2be516 Some message.
$ git checkout somethingOld # Now we are back where we were.

回想起来,我应该使用git stash apply而不是git stashpop。我在做二等分,在每一个二等分步骤上都有一个小补丁。现在我正在做这件事:

$ git reset --hard; git bisect good; git stash apply
$ # Run tests
$ git reset --hard; git bisect bad; git stash apply
etc.

使用gitk的Windows PowerShell等效程序:

gitk--all$(git fsck--no reflog |选择字符串“(悬空提交)(.*)”|%{$_.Line.Split('')[2]})

也许有一种更有效的方法可以在一个管道中完成这项工作,但这确实有效。