从 git 存储库中完全删除文件及其历史记录

问题描述 投票:0回答:4

我上传了一个字体文件,但在几次更新前我无权分发到 git hub。

我有一个相对不活跃的存储库,如有必要,我有能力通知我的所有成员。我已经尝试了几种解决方案。我需要删除目录中名为

Resources\Video\%font%.ttf
的文件,其中
%font%
是字体的纯色、斜体和粗体版本的名称。我使用什么命令?

git bitbucket delete-file
4个回答
151
投票

在这种情况下,您可以使用带有 --tree-filter 选项的

Git Filter Branch
命令。

语法是

git filter-branch --tree-filter <command> ...

git filter-branch --tree-filter 'rm -f Resources\Video\%font%.ttf' -- --all

编辑已更新

请注意,

git filter-branch
--index-filter
--tree-filter

快得多
git filter-branch --index-filter 'rm -f Resources\Video\%font%.ttf' -- --all

在 Windows 中必须使用

/
而不是
\


命令说明:

< command >
指定任何 shell 命令。

--tree-filter:
Git 会将每个提交检查到工作目录中,运行命令,然后重新提交。

--index-filter:
Git 更新 git 历史记录而不是工作目录。

--all:
过滤所有分支中的所有提交。

注意: 请检查您的文件路径,因为我不确定文件路径

希望这对您有帮助。


56
投票

根据官方 git 文档,强烈建议不要使用

git filter-branch
,推荐的方法是使用贡献的 git-filter-repo 命令。

安装(通过包,或使用包 python3-pip,执行 pip installpip install git-filter-repo

)。

驱魔的命令

filename

然后是:

git filter-repo --invert-paths --path filename

--invert-paths

选项表示排除,不包括以下路径。


24
投票

git filter-branch --index-filter 'git rm --cached --ignore-unmatch Resources\Video\%font%.ttf' HEAD

--tree-filter
 快得多(最多 100 倍),因为它只更新 git 历史记录,而不更新工作目录。

ref:

“git filter-branch”中的“--tree-filter”和“--index-filter”有什么区别? 参考:
https://git-scm.com/docs/git-filter-branch


0
投票
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch Resources\Video\%font%.ttf' --prune-empty --tag--name-filter cat -- --all git push origin --all --force git gc --prune=now
    
© www.soinside.com 2019 - 2024. All rights reserved.