如何取消删除以前在git历史记录中删除的文件?

Tob*_*ler 45 git history repository undelete

在另一个问题上使用Chris的答案,我可以将快照历史记录添加到我的git存储库中.由于其中一个文件不是我的历史记录的一部分,而只是在快照中,因此第一个原始提交现在也包含此文件的删除.我该怎么撤消?

起初我认为这与我如何从git的历史记录中删除敏感文件相反,但实际上我不想将文件插入历史记录,只是从历史记录中删除删除.

kub*_*ubi 68

git checkout <commit> <filename>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,但这只会恢复它.我希望历史看起来好像文件从未删除过 (11认同)

Tob*_*ler 53

我知道了:

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
Run Code Online (Sandbox Code Playgroud)

编辑不幸的是,这将留下旧时间轴上的所有标签,请参阅此处

  • +1用于回答看似不可能的问题.哇. (2认同)