删除所有 __pycache__/ 文件夹从Git历史记录中

huangapple go评论52阅读模式
英文:

Remove all __pycache__/ folders from git history

问题

__pycache__/未添加到.gitignore,导致__pycache__/文件夹在整个git历史中被跟踪和提交。

我尝试了以下命令来从整个git历史中删除它:

git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch __pycache/" HEAD

这似乎对顶层根目录中的__pycache__/有效,但子文件夹仍然有它们自己的__pycache__/文件夹,尽管使用了-r递归标志。

如何从git历史中删除所有文件夹中的所有__pycache__/实例?

英文:

__pycache__/ was not added to the .gitignore, resulting in __pycache__/ folders being tracked and committed throughout the git history.

I've attempted this command to remove it from the entire git history

git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch __pycache__/" HEAD

Which seems to have worked for the __pycache__/ in the top-level root directory, but subfolders still have their own __pycache__/ folders, despite the -r recursive flag used.

How can I remove all instances of __pycache__/ throughout all folders from a git history?

答案1

得分: 2

递归标志 -r 不表示“在整个树中查找模式”,而表示“查找顶层目录(们),然后删除这些目录下的所有内容”。要在整个树中删除模式,还需要使用递归模式:

git filter-branch -f --index-filter "git rm --cached --ignore-unmatch */__pycache__/*" HEAD

更新. 使用 [tag:git-filter-repo]:

git filter-repo --force --path-glob '*/__pycache/' --invert-paths
英文:

Recursive flag -r doesn't mean "find the pattern all over the tree", it means "find top-level directory(ies) that match the pattern and remove everything below these directories recursively". To remove a pattern all over the tree you also need to use recursive pattern:

git filter-branch -f --index-filter "git rm --cached --ignore-unmatch */__pycache__/*" HEAD

Upd. Using [tag:git-filter-repo]:

git filter-repo --force --path-glob '*/__pycache__/' --invert-paths

huangapple
  • 本文由 发表于 2023年3月10日 00:39:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75687554.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定