恢复 git 目录同时保留历史记录

Restoring git directory while preserving history

我知道您可以按如下方式恢复以前删除的目录

git checkout {revision} -- {dir}

但是,还原目录中所有文件的历史记录都完全消失了。它们都被视为 'new' 个文件。

有没有办法在恢复目录的同时仍保留其文件的历史记录?

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>
git commit --amend
git rebase --continue
git tag -d originalHead

我找到了这个here