My git repo looks like this foo (root) /src /test There are two folders under foo, src, and test i can calculate overall repo size by using git gc command but i want to know how much /test's contribution in overall repo size
1 Answers
There can be a couple of different approaches, but the easiest one is to rewrite the repository history so that only history of /test
is left there. Then we can calculate the amound of disk taken by it.
First, clone your repository to some location on your disk:
git clone <url> location1
cd location1
Now make sure that all your repository branches are checked out as local branches, so that their history will be preserved when we will be rewriting the repository later. For that get the list of the repository branches and check out each of them:
git for-each-ref --format="%(refname:lstrip=3)" refs/remotes/ | xargs -L1 git checkout
Core of the solution - we delete all other folders except for /test
:
git filter-branch --subdirectory-filter test -- --all
At this point only content of the /test
directory is left in the repo state (and that folder became the root of the project). However, Git didn't delete some old crud - all your unrelated commits and files are still kept in the local repository storage. There are two alternative approaches how to clean that.
- The easiest one - clone the repo to a different disk location (thus no unrelated remote references will be cloned) and prune the repo there:
cd ..
git clone location1 location2
cd location2
git gc --prune=now
2. More hacky one, but doing everything in-place (taken from this answer):
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 git update-ref -d
git -c gc.reflogExpire=0 -c gc.reflogExpireUnreachable=0 -c gc.rerereresolved=0 -c gc.rerereunresolved=0 -c gc.pruneExpire=now gc
Any of those approaches will clean your repository from all the objects not related to the current repository state.
Now we're ready to assess the size of the /test
folder history:
du -h -s .git
This gives you an idea of how much the folder and its history contributes to the size of the repository.
There can be a couple of other approaches. For example, you can try the same stuff with filtering the repository, but this time delete /test
directory and check how much the repo size decreased. Or you can create a program/script that would examine the commits in the Git log and follow object references to calculate how much /test
space is taken by its tree and blobs - but that would require much more coding.
I like this solution, because it is done just via the command line and gives the answer directly at the end.

- 3,644
- 17
- 24