从历史记录中删除不需要的文件,包括带有filter-branch的所有引用

mat*_*hes 3 git git-svn git-filter-branch git-rewrite-history

我最近克隆了一个SVN存储库,它曾经有一些二进制文件,不再需要它们了.不幸的是,我已经把它推到了Github,其中包含了二进制文件.我现在想要使用'git filter-branch'删除它们,但是当涉及到标签和分支时我遇到了一些问题.

基本上,我创建了一个简单的shell脚本来删除已由以下命令确定的文件列表:

git rev-list --objects --all | grep .jar > files.txt
Run Code Online (Sandbox Code Playgroud)

删除脚本如下所示:

#!/bin/sh
while read file_hash file_to_remove
do
    echo "Removing "$file_to_remove;
    git filter-branch --index-filter "git rm --cached --ignore-unmatch $file_to_remove"
    rm -rf .git/refs/original/;
    git reflog expire --all --expire-unreachable=0;
    git repack -A -d;
    git prune
done < $1
Run Code Online (Sandbox Code Playgroud)

我有一些标签(全部列在.git/packed-refs中),一个.git/refs/remotes/origin(指向Github repo).使用上面的脚本删除文件没有想要的效果('du -cm'仍然输出相同的大小;'git rev-list'仍然列出文件),直到我手动删除.git/packed中的所有引用-refs和.git/refs/remotes/origin目录.

当然,我正在丢失所有标签以及使用这种方法将我的本地更改推回给Github的可能性.有什么我错过了或有没有其他方法从所有分支/标签中删除文件而不破坏我的历史记录?

非常感谢,Matthes

mat*_*hes 7

我最终在裸克隆的存储库(git clone --mirror repo-url)上使用BFG Repo Cleaner.它遍历每个分支/标记,使每个分支/标记工作,甚至比过滤分支快得多.希望这可以帮助其他人遇到类似的问题.

这是我的包装脚本:

#!/bin/bash
#usage: ./remove_files.sh file_list.txt bare-repo-dir
while read file_hash file_to_remove
do
    echo "Removing "$file_to_remove;
    lastFile=`echo $file_to_remove | awk -F/ '{print $NF}'`;
    java -jar bfg.jar --delete-files $lastFile $2;
done < $1

cd $2;
git gc --prune=now --aggressive;
cd ..;
Run Code Online (Sandbox Code Playgroud)