git,在所有分支上过滤分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7430717/
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
git, filter-branch on all branches
提问by EoghanM
I'm using the following sources to expunge some large files and directories from my repository:
我正在使用以下来源从我的存储库中删除一些大文件和目录:
http://dound.com/2009/04/git-forever-remove-files-or-folders-from-history/
http://dound.com/2009/04/git-forever-remove-files-or-folders-from-history/
Why is my git repository so big?
git filter-branch
only seems to work on the current branch - is there a way to apply it to all branches at once?
git filter-branch
似乎只适用于当前分支 - 有没有办法一次将它应用于所有分支?
回答by Ben Lee
The solution is simple:
解决方法很简单:
git filter-branch [options] -- --all
git filter-branch [options] -- --all
Note the four dashes (two sets of double dashes with a space in between) in -- --all
.
注意 中的四个破折号(两组双破折号,中间有一个空格)-- --all
。
If you look at the docs for git-filter-branch
, it says this:
如果您查看 的文档git-filter-branch
,它会说:
git filter-branch [--env-filter <command>] [--tree-filter <command>]
[--index-filter <command>] [--parent-filter <command>]
[--msg-filter <command>] [--commit-filter <command>]
[--tag-name-filter <command>] [--subdirectory-filter <directory>]
[--prune-empty]
[--original <namespace>] [-d <directory>] [-f | --force]
[--] [<rev-list options>…]
Reading on, the beginning of the docs say: "Lets you rewrite git revision history by rewriting the branches mentioned in the <rev-list options>, applying custom filters on each revision."
继续阅读,文档的开头说:“让您通过重写 <rev-list options> 中提到的分支来重写 git 修订历史记录,在每个修订版上应用自定义过滤器。”
So checking the docs for rev-list
gives:
所以检查文档rev-list
给出:
< rev-list options >… Arguments for git rev-list. All positive refs included by these options are rewritten. You may also specify options such as --all, but you must use -- to separate them from the git filter-branch options.
< rev-list options >... git rev-list 的参数。这些选项包含的所有正面参考都被重写。您还可以指定诸如 --all 之类的选项,但您必须使用 -- 将它们与 git filter-branch 选项分开。
And the docs for git-rev-list
say:
和文档git-rev-list
说:
--all
Pretend as if all the refs in refs/ are listed on the command line as <commit>.
回答by Roberto Tyley
As @ben-lee's answer explains, --all
is required for rewriting all branches. If you have tagsin your repo, you'll want to clean all those, as well as the branches, in order get the benefits of reduction in size, and that will require an additional --tag-name-filter cat
incantation.
正如@ben-lee 的回答所解释的,--all
重写所有分支都需要。如果您的存储库中有标签,您将需要清理所有这些标签以及分支,以获得减小尺寸的好处,这将需要额外的--tag-name-filter cat
咒语。
Although the question specifies using git filter-branch
, the questioner wants to 'expunge some large files and directories from my repository', so it's worth mentioning that the besttool for doing that is actually The BFG Repo Cleaner, a simpler, faster alternative to git filter-branch
. For instance:
尽管问题指定使用git filter-branch
,但提问者想要“从我的存储库中删除一些大文件和目录”,因此值得一提的是,执行此操作的最佳工具实际上是BFG Repo Cleaner,它是git filter-branch
. 例如:
$ bfg --strip-blobs-bigger-than 10M
...removes all blobs bigger than 10MB (that aren't in your latestcommit), and works on allbranches & tags in your repo.
...删除所有大于 10MB 的 blob(不在您的最新提交中),并适用于您的 repo 中的所有分支和标签。
Full disclosure: I'm the author of the BFG Repo-Cleaner.
完全披露:我是 BFG Repo-Cleaner 的作者。
回答by Paul Preibisch
I followed all the instructions for doing this on my Windows box, but I kept getting an error, until I stopped using single quotes and used double quotes instead.
我按照在我的 Windows 机器上执行此操作的所有说明进行操作,但我一直收到错误消息,直到我停止使用单引号并改用双引号为止。
My motivation was that I accidentally checked in my vagrant
environment. Here's the command to remove the vagrant
folder from all branches:
我的动机是我不小心检查了我的vagrant
环境。这是vagrant
从所有分支中删除文件夹的命令:
git filter-branch --tree-filter "rm -rf vagrant" -f HEAD --all
Just replace vagrant
with your directory name, and it'll remove this directory from all branches.
只需替换vagrant
为您的目录名称,它就会从所有分支中删除该目录。