【问题标题】:How to undelete a file previously deleted in git's history?如何取消删除以前在 git 历史记录中删除的文件?
【发布时间】:2011-03-10 04:30:17
【问题描述】:

在另一个问题上使用Chris's answer,我可以在我的 git 存储库中添加一个快照历史记录。由于其中一个文件不是我的历史记录的一部分,而是仅在快照中,因此第一个原始提交现在也包含该文件的删除。我该如何撤消呢?

一开始我以为这与How do I remove sensitive files from git’s history相反,但实际上我不想将文件插入历史记录,而只是从历史记录中删除删除。

【问题讨论】:

  • Git 不跟踪意图,只跟踪内容。如果文件存在于祖先中并在子提交中被删除,那么“从历史中删除删除”和“重新插入历史”相当于同一件事:确保文件出现在子及其子中.

标签: git repository history undelete


【解决方案1】:

我明白了:

git tag originalHead # just in case
git rebase -i <id of the parent of the commit that deleted the file>
# change pick to edit for that commit
git checkout <id of the previous commit> <filename> # thanks for reminding, kubi
git commit --amend
git rebase --continue
git tag -d originalHead

编辑不幸的是,这会将所有标签留在旧时间轴上,请参阅here

【讨论】:

  • 说真的! git rebase -i 这救了我的命
【解决方案2】:
git checkout <commit> <filename>

【讨论】:

  • 谢谢,但这只会恢复它。我希望历史看起来好像文件从未被删除
猜你喜欢
  • 1970-01-01
  • 2017-10-01
  • 2014-06-19
  • 2020-03-20
  • 1970-01-01
  • 2013-03-31
  • 2017-06-16
  • 2023-01-30
  • 1970-01-01
相关资源
最近更新 更多