git,所有分支上的过滤分支

发布于 2024-12-05 01:15:47 字数 429 浏览 5 评论 0原文

我正在使用以下来源从我的存储库中删除一些大文件和目录:

http://dound.com/2009/04/git-forever-remove-files-or-folders-from-history/

为什么我的 git 存储库这么大?

git filter-branch 似乎仅适用于当前分支 - 有没有办法将其立即应用于所有分支?

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/

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岛歌少女 2024-12-12 01:15:47

解决方案很简单:

git filter-branch [options] -- --all

注意 -- - 中的四个破折号(两组双破折号,中间有一个空格) -all

如果您查看 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>…]

继续阅读,文档的开头说:“让您重写 git通过重写中提到的分支来修改历史记录,在每个修订上应用自定义过滤器。”

因此,检查 rev-list 的文档给出:

< rev-list 选项 >... git rev-list 的参数。所有正面参考
这些选项包含的内容被重写。您还可以指定选项
例如--all,但必须使用--将它们与git分开
过滤器分支选项。

git-rev-list 的文档说:

--all
Pretend as if all the refs in refs/ are listed on the command line as <commit>.

The solution is simple:

git filter-branch [options] -- --all

Note the four dashes (two sets of double dashes with a space in between) in -- --all.

If you look at the docs for git-filter-branch, it says this:

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."

So checking the docs for rev-list gives:

< 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.

And the docs for git-rev-list say:

--all
Pretend as if all the refs in refs/ are listed on the command line as <commit>.
辞旧 2024-12-12 01:15:47

正如 @ben-lee 的回答所解释的,重写所有分支需要 --all 。如果您的存储库中有标签,您将需要清理所有这些以及分支,以便获得减小大小的好处,这将需要额外的 --tag-name-filter cat 咒语。

尽管问题指定使用 git filter-branch,但提问者想要“从我的存储库中删除一些大文件和目录”,因此值得一提的是,最好的 这样做的工具实际上是 BFG Repo Cleaner,是 git filter-branch 的更简单、更快的替代品。例如:

$ bfg --strip-blobs-bigger-than 10M

...删除所有大于 10MB 的 blob(不在您的最新提交中),并适用于所有分支和分支。您的存储库中的标签。

全面披露:我是 BFG Repo-Cleaner 的作者。

As @ben-lee's answer explains, --all is required for rewriting all branches. If you have tags in 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.

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 best tool for doing that is actually The BFG Repo Cleaner, a simpler, faster alternative to git filter-branch. For instance:

$ bfg --strip-blobs-bigger-than 10M

...removes all blobs bigger than 10MB (that aren't in your latest commit), and works on all branches & tags in your repo.

Full disclosure: I'm the author of the BFG Repo-Cleaner.

○愚か者の日 2024-12-12 01:15:47

我按照在 Windows 机器上执行此操作的所有说明进行操作,但一直收到错误,直到我停止使用单引号并改用双引号。

我的动机是我不小心检查了我的 vagrant 环境。以下是从所有分支中删除 vagrant 文件夹的命令:

git filter-branch --tree-filter "rm -rf vagrant" -f HEAD --all

只需将 vagrant 替换为您的目录名称,它就会从所有分支中删除该目录。

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.

My motivation was that I accidentally checked in my vagrant environment. Here's the command to remove the vagrant folder from all branches:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文