1. Home

Anil

< Blog />

Slim down your git repository size

in

I’ve recently moved to Github for my private repositories and was in the process of migrating my repositories over. I wanted to be a good citizen and reduce my repository size as much as possible by excluding binaries I had previously committed on an iOS project.

Here’s how I did it.

Removing the files from your existing git repository

First, we’ll need to add our large files to our .gitignore and tell git to stop tracking them.

In my case, I ignored these frameworks from an iOS project I’m working on, I can easily re-install them using package manager CocoaPods.

# .gitignore
Pods/Realm
Pods/Crashlytics
Pods/Fabric
Pods/TwitterKit
Pods/Digits
Pods/TwitterCore

We’ll need to tell git to stop tracking these files.

git rm -r --cached Pods/Realm
git rm -r --cached Pods/Crashlytics
git rm -r --cached Pods/Fabric
git rm -r --cached Pods/TwitterKit
git rm -r --cached Pods/Digits
git rm -r --cached Pods/TwitterCore

You’ll have to commit and push your changes.

Remember to add your updated .gitignore.

# Your removed cached items should be automatically added
git add .gitignore
git commit -m "Removed large files for prune"
git push origin master

Pruning the repository

We’ll use a popular tool called bfg which removes large files like git-filter-branch does.

Installing bfg

Install it via homebrew (or your preferred means) like so:

brew install bfg

Prepare the repository

Create a bare clone of your git repository:

git clone --mirror git@github.com:lakhman/fat-repo.git

Note

Note This will clone your actual core repository history, and so you will not see your files here.

Create a backup of the repository:

cp -r fat-repo.git fat-repo.git-backup

Remove the files from your local repository

Remove all files larger than 1M.

bfg -b 1M fat-repo.git

This will remove all files over 1000K, except HEAD (which is protected). View all the options by reading the docs or running bfg.

The bfg command should output a command like so, you need to run this in your project root to finalize the process.

# Jump into our project
cd fat-repo.git

# reflog - Process our reference logs as we've manually updated them
# gc     - Cleanup unnecessary files and optimize the local repository
git reflog expire --expire=now --all && git gc --prune=now --aggressive

You can view more information about these commands here:

gc https://git-scm.com/docs/git-gc
reflog https://git-scm.com/docs/git-reflog

Push your changes to your remote

Finally, we’ll want to push our changes to a remote repository.

Tip

Use git config --list to view your settings.

It’s best to do this in a new repository, create one (I’ll be using Github, we’ll call it slim-repo).

git remote remove origin
git remote add origin git@github.com:lakhman/slim-repo.git
git push -u origin master

If you’re using Github, you can see your repository size by going to your Account Settings > Repositories.


You should now have a slimmer repository, remember you’ll have to clone your repository again as the references will have changed during this the process.