git - 从所有分支和标签中完全删除 git 存储库对象并将更改推送到远程

标签 git indexing repository history

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

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

$ 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 在本地克隆这个存储库也有同样的效果:更糟糕的是,我所有的原始分支都消失了(正如你可能expect) 所以我只有 master。

我能做些什么来摆脱那些包装好的元素吗?它们的持续存在是否表明它们仍然与某个地方的某个 git 提交对象相关联?我已尝试重新打包修剪打包 但没有任何改变。

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

最佳答案

以下工作重复进行,将存储库减少到大约 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

关于git - 从所有分支和标签中完全删除 git 存储库对象并将更改推送到远程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10622115/

相关文章:

python - neo4jrestclient 索引结果返回 Iterable 而不是 node/url?

git 忽略多个匹配文件

git grep——但仅限于索引中的新文件或修改过的文件

MySQL 唯一和 NULL 索引建议

具有不同结构的 Git fork

java - 如何获取 Spring Data 上的总页数?

entity-framework - 从 IQueryable 生成缓存键以缓存 EF Code First 查询的结果

windows - Git Bash 无法解析 %userprofile%

git - 有没有商业级的Git服务器产品

Mysql-这是一个很好的优化查询吗?