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

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

Remove deleted files from git history

gitversion-controlgit-filter-branch

提问by Niklas Schnelle

I'm trying to split a subproject off of my git repository. However unlike in Detach (move) subdirectory into separate Git repositoryI don't have it in it's own subdirectory (and moving it in and doing the above only yields the history after the move).

我正在尝试从我的 git 存储库中拆分一个子项目。但是,与 将子目录分离(移动)到单独的 Git 存储库中不同的是,我没有将它放在自己的子目录中(将它移入并执行上述操作只会在移动后产生历史记录)。

I've cloned the branch from which I want to split off the subproject into it's own repository and removed everything that isn't used by the subproject, so basically I could use this as the repository of my subproject.

我已经克隆了我想从中拆分子项目的分支到它自己的存储库中,并删除了子项目未使用的所有内容,因此基本上我可以将其用作子项目的存储库。

Now I want to get rid of the history of all files that are no longer in this repository so as to only keep the file history for the files that made it into the offspring.

现在我想删除不再存在于该存储库中的所有文件的历史记录,以便只保留使其成为后代的文件的文件历史记录。

I think it must be possible with git-filter-branch but I can't figure out how

我认为 git-filter-branch 一定是可能的,但我不知道如何

Many thanks in advance

提前谢谢了

回答by Neil Forrester

Here are some instructions to do what you want.

这里有一些说明可以做你想做的事。

This will remove file_to_remove:

这将删除file_to_remove

git filter-branch --index-filter 'git rm --cached --ignore-unmatch file_to_remove' --prune-empty -- --all

回答by Niklas Schnelle

Ok now I'm trying with the following technique, will report back if it worked, because it seems to be quite long running: On a zsh or bash ON A CLONED Repository

好的,现在我正在尝试使用以下技术,如果它有效,将报告回来,因为它似乎运行时间很长:在 zsh 或 bash 上克隆存储库

git log --diff-filter=D --summary <start_commit>..HEAD | egrep -o '*[[:alnum:]]*(/[[:alnum:].]*)+$' > deleted.txt

to get all deleted files

获取所有已删除的文件

for del in `cat deleted.txt`
do
    git filter-branch --index-filter "git rm --cached --ignore-unmatch $del" --prune-empty -- --all
    # The following seems to be necessary every time
    # because otherwise git won't overwrite refs/original
    git reset --hard
    git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
    git reflog expire --expire=now --all
    git gc --aggressive --prune=now
done;

This might be extremly dangeours for your data so only try on clones.

这对您的数据来说可能是非常危险的,所以只能尝试克隆。