Git is a tool I use daily. I am not a master of Git by any means, but I’m always looking to enhance my knowledge in tools I use regularly. I wanted to find a way to be able to see how many megabytes a given Git repository is. Thankfully git is very flexible and has tools for this.After some Google searching I found that git has the command count-objects:
NAME
git-count-objects – Count unpacked number of objects and their disk
consumption
SYNOPSIS
git count-objects [-v] [-H | –human-readable]
DESCRIPTION
This counts the number of unpacked object files and disk space consumed by
them, to help you decide when it is a good time to repack.
Being aware of this I tried using this command on a local git repository and it gave me the information I wanted. Its output has the same structure always — so I then wanted to create 2 bash functions which:
- examine if we are in a Git repository folder, and if so then
- use git count-objects to output the size of the repository on-disk.
The simple bash functions I wrote are as follows. I typically place such functions into my .bash_aliases file and source it on system startup.
function is-cwd-git-repo() { local answer='' if [ -d .git ]; then answer=$(echo .git) else answer=$(git rev-parse --git-dir 2> /dev/null) fi; return [[ "$answer" ]] && 0 || 1 } function git-repo-size() { local size='' if [ is-cwd-git-repo ]; then size=$(git count-objects -vH | grep "size-pack" | sed 's/size-pack: //') echo "git repo is ${size}." else echo "not in git repo." return 1; fi; }
An example run from a git repository of mine is:
✔ aw-dgurba /var/www/2014/my-app esci-20141-shortfixes > git-repo-size git repo is 3.85 MiB.
So I can examine the filesizes of any git repository I am working on if I’m curious of the repo size. Git provides other useful tools to manage and archive repositories, such as git-repack and git-archive.
Looking for quality web hosting? Look no further than Arvixe Web Hosting!