首页 > 解决方案 > 您可以将现有的 git 存储库转换为“blobless”吗?

问题描述

今天,git 提供了“部分克隆”选项,可以下载存储库的提交和树,同时允许按需下载 blob,从而节省网络带宽和磁盘空间。

这可以在初始期间git clone通过传递来启用--filter=blob:none。但是,有没有办法将已经存在的本地存储库转换为“blobless”格式?这应该通过删除任何已知可从“promisor”远程获得的本地 blob 来节省一些磁盘空间。

标签: gitgit-clone

解决方案


虽然我不知道有专门的就地命令,但您仍然可以执行本地克隆,然后将原始文件夹替换为 blobless 副本。(受knittl评论
启发的解决方案)

# If it's your first time, you'd need to enable filtering locally.
git config --global uploadpack.allowFilter true

# Filter your existing repo into a new one.
# The `file://` protocol followed by a full path is required.
git clone --filter=blob:none file:///full_path_to_existing_repo/.git path_to_new_blobless_copy

# Reconfigure the origin of your new repo.
# You can retrieve it with `git remote -v` in your existing repo.
cd path_to_new_blobless_copy
git remote set-url origin remote_path_to_origin.git
cd -

# Replace your existing repo with the new one.
# Destructive operation that will free up the space of the blobs.
# But will also destroy your local stashes, branches and tags that you didn't clone!
rm -rf /full_path_to_existing_repo
mv path_to_new_blobless_copy /full_path_to_existing_repo

推荐阅读