Make Git consume less disk space?

What is the best way for git to consume less disk space?

I'm using git-gc on my repositories (which does help, especially if there have been many commits since it was cloned) but I would like suggestions if there is any other command to shrink the disk space used by git.


Solution 1:

There are a few suggestions I can offer:

  1. Delete no longer used branches. They can pin some commits that you don't use and would never use. Take care however to not delete branches that you would later need (perhaps for review, or for comparison of failed effort). Backup first.

  2. Check if you didn't commit some large binary file (perhaps some generated file) by mistake. If you have, you can purge it from history using "git filter-branch"... well, if you didn't share the repository, or it is worth aggravating other contributors to rewrite history. Again: backup first.

  3. You can prune more aggressively, discarding some safeties, bu using git gc --prune=now, or low-level git prune. But take care that you don't remove safeties and backups (like reflog) that you need minute after compacting.

  4. Perhaps what enlarges your repository are some untracked files in working directory. There "make clean" or "git clean" might help (but take care that you don't remove some important files).

  5. Most safe of all those suggestions: you can try to pack more aggressively, using --depth and --window option of low-level git-repack. See also Git Repack Parameters blog post by Pieter de Bie on his DVCS Comparison blog, from June 6, 2008. Or "git gc --aggressive".

Solution 2:

Depending on what you want to do with your repository, you might also consider using the following git clone option:

   --depth <depth>
       Create a shallow clone with a history truncated to the specified
       number of revisions. A shallow repository has a number of
       limitations (you cannot clone or fetch from it, nor push from nor
       into it), but is adequate if you are only interested in the recent
       history of a large project with a long history, and would want to
       send in fixes as patches.

Solution 3:

git-gc calls lots of other commands that are used to clean up and compress the repository. All you could do is delete some old unused branches.

Short answer: No :-(

Solution 4:

Git clone now has a --single-branch option that allows you to checkout a single branch without pulling in the git history of the other branches. If git is consuming a lot of disk space because you have a lot of branches, you can delete your current checkout and re-clone the repo using this option to regain some disk space. For example:

cd ../
rm -rf ./project
git clone -b master --single-branch [email protected]:username/project.git

Also, if your current master has a long history and you don't have any outstanding branches that need to be merged back into master, you can create an archive branch off of master and create a new orphan master with no git history:

git checkout -b master_archive_07162013  # create and switch to the archive branch
git push origin master_archive_07162013  # push the archive branch to the remote and track it
git branch -D master                     # delete local master
git push --delete origin master          # delete remote master
git remote prune origin                  # delete the remote tracking branch
git checkout --orphan master             # create a new master branch with no history
git commit -m "initial commit"           # re-establish the files in the repo
git push origin master                   # push the new master to the remote

The new master branch's tree will not be related to the old archived master branch, so only do this when you are truly archiving the branch.

If you archive your master branch and then git clone master with single-branch, your checkout should be a lot smaller.

Solution 5:

Every git repository contains the entire history. While git does a fairly good job of compressing this stuff, there's simply a lot of data in there.

The "obvious" but potentially not-possible-for-you solution is to start a new repository without all that old history.