How to remove unreferenced blobs from my git repo

后端 未结 10 2221
轻奢々
轻奢々 2020-11-22 15:07

I have a GitHub repo that had two branches - master & release.

The release branch contained binary distribution files that were contributing to a very large repo

相关标签:
10条回答
  • 2020-11-22 15:11

    Sometimes, the reason that "gc" doesn't do much good is that there is an unfinished rebase or stash based on an old commit.

    0 讨论(0)
  • 2020-11-22 15:16

    You can use git forget-blob.

    The usage is pretty simple git forget-blob file-to-forget. You can get more info here

    https://ownyourbits.com/2017/01/18/completely-remove-a-file-from-a-git-repository-with-git-forget-blob/

    It will disappear from all the commits in your history, reflog, tags and so on

    I run into the same problem every now and then, and everytime I have to come back to this post and others, that's why I automated the process.

    Credits to contributors such as Sam Watkins

    0 讨论(0)
  • 2020-11-22 15:16

    Before doing git filter-branch and git gc, you should review tags that are present in your repo. Any real system which has automatic tagging for things like continuous integration and deployments will make unwanted objects still referenced by these tags, hence gc can't remove them and you will still keep wondering why the size of repo is still so big.

    The best way to get rid of all un-wanted stuff is to run git-filter & git gc and then push master to a new bare repo. The new bare repo will have the cleaned up tree.

    0 讨论(0)
  • 2020-11-22 15:18

    ... and without further ado, may I present to you this useful command, "git-gc-all", guaranteed to remove all your git garbage until they might come up extra config variables:

    git -c gc.reflogExpire=0 -c gc.reflogExpireUnreachable=0 -c gc.rerereresolved=0 -c gc.rerereunresolved=0 -c gc.pruneExpire=now gc
    

    You might also need to run something like these first, oh dear, git is complicated!!

    git remote rm origin
    rm -rf .git/refs/original/ .git/refs/remotes/ .git/*_HEAD .git/logs/
    git for-each-ref --format="%(refname)" refs/original/ | xargs -n1 --no-run-if-empty git update-ref -d
    

    You might also need to remove some tags, thanks Zitrax:

    git tag | xargs git tag -d
    

    I put all this in a script: git-gc-all-ferocious.

    0 讨论(0)
  • 2020-11-22 15:19

    To add another tip, don't forget to use git remote prune to delete the obsolete branches of your remotes before using git gc

    you can see them with git branch -a

    It's often useful when you fetch from github and forked repositories...

    0 讨论(0)
  • 2020-11-22 15:21

    git gc --prune=now, or low level git prune --expire now.

    0 讨论(0)
提交回复
热议问题