V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
harttle
V2EX  ›  问与答

Git 仓库瘦身:定位大文件以及批量移除记录

  •  
  •   harttle · Mar 23, 2016 · 6057 views
    This topic created in 3693 days ago, the information mentioned may be changed or developed.

    最近解决了一个一直以来困扰我的问题: Git 仓库因为添加过不该添加的图片等文件而变得很大。 写了一些脚本,把我的 git 仓库( http://github.com/harttle/harttle.github.io )从 142M 变成了 27M 。因为没找到解决方案才自己写脚本,放在这里希望能帮到别人。

    首先通过 rev-list 来找到仓库记录中的大文件:

    git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -5 | awk '{print$1}')"

    然后通过 filter-branch 来重写这些大文件涉及到的所有提交(重写历史记录):

    git filter-branch -f --prune-empty --index-filter 'git rm -rf --cached --ignore-unmatch your-file-name' --tag-name-filter cat -- --all

    为了能够批量处理,还写了一些脚本,整个过程记录在这里:

    http://harttle.com/2016/03/22/purge-large-files-in-gitrepo.html

    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2461 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 07:34 · PVG 15:34 · LAX 00:34 · JFK 03:34
    ♥ Do have faith in what you're doing.