如何取消删除以前在 git 历史记录中删除的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3150394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to undelete a file previously deleted in git's history?
提问by Tobias Kienzler
Using Chris's answeron another question I could prepend a snapshot-history to my git repository. Since one of the files is not part of my history but only in the snapshots, the first original commit now also contains the deletion of this file. How can I undo that?
使用Chris在另一个问题上的回答,我可以在我的 git 存储库中添加快照历史记录。由于其中一个文件不是我的历史记录的一部分而仅在快照中,因此第一个原始提交现在还包含该文件的删除。我怎样才能撤销它?
At first I thought this was the opposite of How do I remove sensitive files from git's history, but actually I don't want to insert a file into history but just remove the deletion from history.
起初我认为这与如何从 git 的历史记录中删除敏感文件相反,但实际上我不想将文件插入到历史记录中,而只是从历史记录中删除删除。
回答by kubi
git checkout <commit> <filename>
回答by Tobias Kienzler
I got it:
我知道了:
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
editunfortunately this will leave all tags at the old timeline, see here
不幸的是,编辑这会将所有标签保留在旧时间轴上,请参见此处