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 w
Fool proof method if you don't care about download size is to delete the repository (you can just delete the whole folder) and add it again. Make sure everything that needs to be preserved is pushed to the server!
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 git@github.com: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.
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.
git prune
might be a hint. it cleans the repository from unreachable commits (git gc
does not call it)
You might have a lot of git projects cloned on your computer, but only a few of them you are actively working on today.
In those idle projects, the checked out working files can consume a significant amount of disk space. (Sometimes even larger than git's history, because history gets compressed.)
So one way to save disk space is to remove the working files from idle projects you are not working on. A nice way to do that is to create an empty branch which you can switch to when you are not working on the project.
Another more drastic thing you can do is to delete absolutely everything except for the .git/config
file. Or just remove the largest folder, the git history:
rm -rf .git/objects
That will allow you to git fetch
again in future, when you want the history and the files back. Before doing this, you should ensure that you have pushed all your work (including local branches) to the remote repository, so there is nothing in the local git repo that you need to retain.