4

我们做了一个客户迁移到一个网站。我们的代码在一个单独的分支上,然后合并到 master 和 release 中。从那以后,Master 也针对其他功能进行了多次分支。所有这些分支使存储库比我在网上找到的示例稍微复杂一些。

我们现在意识到客户的原始媒体——主要是图像和一个大的 CSV 文件——也被检入了 Git。虽然它只有 12MB 左右,但删除它有几个原因(尤其是客户端的文件名包含非 ASCII 字符,这与我们在 OSX 上的 Vagrant box 的共享文件夹玩得很糟糕。)这是存储库的大小细分:

$ du --max-depth 1 -h
12M  ./.git
13M  ./modules
2.0M ./themes
27M  .

尽管二进制文件现在显然存在于多个分支上,但据我所知,我应该能够执行以下操作来删除两个二进制文件,然后删除与它们对应的存储库对象:

$ git filter-branch --tree-filter "git rm -rf --ignore-unmatch modules/custom/mymigration/data/photos/*" # Did this with and without "HEAD" argument
[snip lots of output]
$ git reflog expire --expire=now --all 
$ git gc --aggressive --prune=now

但是,我仍然有一个大的 .git 子文件夹:

$ du --max-depth 1 -h
12M  ./.git
1.4M ./modules
2.0M ./themes
15M  .

最大的文件是 .git/objects/pack/pack-....pack 。当我为此验证 .idx 文件时:

$ git verify-pack -v .git/objects/pack/pack-53c8077d0590dabcf5366589c3d6594768637f5e.idx | sort -k 3 -n | tail -n 5

我得到一长串对象。如果我将它通过管道传输到 rev-list,并为我的迁移数据目录使用 grep:

$ for i in `git verify-pack -v .git/objects/pack/pack-53c8077d0590dabcf5366589c3d6594768637f5e.idx | sort -k 3 -n | tail -n 5 | awk '{print $1}'`;    do 
    git rev-list --objects --all | \
      grep $i | \
      grep modules/custom/mymigration/data
  done
47846536601f0bc3a31093c88768b522a5500c96 modules/custom/mymigration/data/photos/Turkey.jpg
b920e36357d855352f4fdb31c17772d21c01304d modules/custom/mymigration/data/photos/Burger_Top.JPG

然后你可以看到照片仍然在包文件中。

  • 如果我将此存储库推送到(完全空的)远程,然后将该远程克隆到其他完全不同的地方,仍然有 12MB 的包文件。
  • 在本地克隆这个存储库git clone file://path/to/old-repos new-repos也有同样的效果:更糟糕的是,我所有的原始分支都消失了(正如你可能期望的那样),所以我只有 master。

我能做些什么来摆脱那些包装好的物品吗?它们的持续存在是否表明它们仍然与某个地方的某个 git commit 对象相关联?我试过了repackprune-packed但没有任何改变。

此外,如果我只是“摆脱它们”,如果我没有正确完成第一点,有什么可能会破坏吗?如果删除了 git commit 仍然引用的文件对象会发生什么?

4

2 回答 2

7

以下工作可重复地将存储库减少到大约 2.5MB .git 和总共 5.8MB。它包括上面@jamessan 提出的建议。

这将从所有分支中删除对象并将这些删除推送到远程存储库。据我所知,该远程存储库完全没有这些对象(由于存储库大小急剧下降。)

# Configure the repository to push all existing branches & tags
# when none are explicitly specified
git config --add remote.origin.push '+refs/tags/*:refs/tags/*'
git config --add remote.origin.push '+refs/heads/*:refs/heads/*'

# Make sure all local branches exist, so they get filtered
for remote_branch in `git branch --all | grep -v HEAD | sed -e 's/\*//'`; do local_branch=`echo $remote_branch | sed -e 's!remotes/origin/!!'`; git checkout $local_branch; done

# Prevent git <1.7.7.1 from complaining about dirty working directory
git update-index -q --ignore-submodules --refresh

# Do the filtering across --all branches and rewrite tags
# Note that this will necessarily remove signatures on tags
git filter-branch -f --tree-filter "git rm -rf --ignore-unmatch modules/custom/mymigration/data/photos/*" --tag-name-filter cat -- --all

# Remove the backed-up refs
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d

# Clear out the reflog and garbage-collect
git reflog expire --expire=now --all
git gc --aggressive --prune=now

# Push all changes to origin - pushes tags and branches
git push origin
于 2012-05-17T10:16:02.627 回答
2

git-filter-branch 手册页的底部描述了缩小存储库的两种方法。

简单的方法是再次克隆存储库

git clone file:///path/to/repo

更复杂的方法类似于您所做的(reflog expire, gc),但您省略了第一步

删除 git-filter-branch 备份的原始 refs: git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d

于 2012-05-16T16:02:02.037 回答