Reduce Git Repository Size

I wrote the blog couple days ago – BitBucket Server Unreachable When Merge Pull Request, as mentioned the issue was caused by the big size of the repository. As a workaround/ quick fix, we increased the merge timeout value, and upgraded Git to a newer version which remediate the issue.

The issue will come back if the repository size keeps growing. The proper way to fix the issue permanently is to reduce the size by using Git LFS and BFG. Here are the steps that I would suggest to try:

  1. Clone the bare repository and make a copy of it as a backup. In the sample it is big-repo.git
    git clone --mirror ssh://git@bitbucket/myproject/big-repo.git
  2. Convert big files into lfs. In the sample, I would like to convert any zip, jar, war files to git lfs. Please be aware it will rewrite the history, e.g the commit ID will change.
    java -jar bfg.jar --convert-to-git-lfs "*.{zip,jar,war}" --no-blob-protection big-repo.git
  3. Create a new repository on Bitbucket and enable Git LFS. In the sample, I just call it new-repo.git
  4. Add the new repo to your local git remote, and name it as newrepo.
    git remote add newrepo ssh://git@bitbucket/myproject/new-repo.git
  5. Verify the above remote settings.
    git remote -v
  6. Push your local repository to the new repository.
    git push newrepo --all
  7. Clone the new repo to your local.
    git clone ssh://git@bitbucket/myproject/new-repo.git
  8. Track all zip, jar, war file in the new repo.
    cd new-repo
    git lfs install
    git lfs track "*.zip" "*.jar" "*.war"
  9. Make sure you .gitattributes is tracked by git.
    git add .gitattributes
    git commit -m "track lfs file types"
    git push
  10. Notify each person in your team to install git lfs. Then clone and start to use the new-repo which is lfs-enabled.




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s