从 git 历史记录中删除已删除的文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21230090/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 09:36:41  来源:igfitidea点击:

Remove a deleted folder form git history

git

提问by Marten Bauer

How can I remove a folder that is already deleted from the git history?

如何删除已从 git 历史记录中删除的文件夹?

In my git repository I have a folder called /foo(1.2GB of size). I have deleted the folder foowith rm -rf /foobeause I do not need it any more. After many other commits I thought. Why is my remote repo so big...I have forgotten to do git rm --cached ...instead of rm -rf .... How can I now remove the folder from git history?

在我的 git 存储库中,我有一个名为/foo(1.2GB 大小)的文件夹。我已经删除了文件夹foorm -rf /foo因为我不再需要它了。在我想过许多其他提交之后。为什么我的远程仓库这么大...我忘了做git rm --cached ...而不是rm -rf .... 我现在如何从 git 历史记录中删除该文件夹?

git rm --cached /foowon't work because the folder is already delete in an earlier commit.

git rm --cached /foo将无法工作,因为该文件夹已在较早的提交中被删除。

回答by user4815162342

The documentationcovers the similar case of purging a file from history:

文档涵盖了从历史记录中清除文件的类似情况:

git filter-branch --index-filter 'git rm --cached --ignore-unmatch filename' HEAD

Since you are deleting a whole directory, add the -rflag to git rm:

由于您要删除整个目录,请将-r标志添加到git rm

git filter-branch --index-filter \
                  'git rm -r --cached --ignore-unmatch path/to/directory' HEAD

Note that this operation will take several minutes on larger repositories.

请注意,在较大的存储库上,此操作将需要几分钟时间。

More importantly, it will make a new repository with distinct history and checksums. If you previously published your repository, the history of the new one will not be compatible with the history others have pulled.

更重要的是,它将创建一个具有不同历史记录和校验和的新存储库。如果您之前发布了您的存储库,则新存储库的历史记录将与其他人提取的历史记录不兼容。