git - 如何在 git stash/git stash pop 之后恢复索引?

标签 git git-stash

在使用 git add -p 对索引添加一些更改后,我发布了一个 git stash 但忘记添加 --keep-index。然后我就傻傻的做了一个git stash pop,我对索引的修改都没有了。有没有办法将索引恢复到git stash之前的状态?

最佳答案

当你刚刚完成 git stash pop 时,输出的最后一行是:

Dropped refs/stash@{0} (ca82a6dff817ec66f44342007202690a93763949)

如果您丢失了它,请参阅 How to recover a dropped stash in Git?找到提交哈希。

一旦你有了哈希,要么:

  • 删除所有当前更改(已应用的存储):

    git reset --hard

    然后使用其 id 重新应用存储,这次使用索引:

    git stash apply ca82a6d --index

  • 只重置索引,这里的重点是索引被保存为存储的第二个父级:

    git reset ca82a6d^2 .

    注意末尾的点。如果您不指定它,它还会将 HEAD 移动到索引(索引将显示为提交)。在这种情况下,运行 git reset --soft HEAD@{1} 将 HEAD 返回到之前的位置。

关于git - 如何在 git stash/git stash pop 之后恢复索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14264467/

相关文章:

bazaar - 如何在化石中模拟 'git stash',bzr?

git - 为什么存储表示为 2 次提交?

git - 如何在 Git 2.23 中禁用 "git checkout"?

android - repo 同步失败,出现 "error: index-pack died of signal 9"

git - 需要安装 Perl 和 Linux

linux - 提交后忽略 Git 中的文件

预提交 Hook 中的 git stash 在第一次提交时失败

git - 如何在执行 merge 请求时重新建立 fork 的存储库 - Gitlab

git - 存储使用 --intent-to-add 添加的文件

git 存储 : where did the files in my directory go?