Optimize size of git repository

Have you worked on a WordPress project and then you discovered the .git folder’s size inside is huge?

Is its size quite small but takes times to clone/change/push branch?




It’s fine if you have a fast connection; otherwise, it’s totally a disaster.

I have experienced on a project like this. Sometimes, the connection got fast. However, there were days that I have to wait times (15-20 min) to update code before working.

The problem we found out in our circumstance is the PSD files that were attached inside a revision. They shouldn’t be tracked by git.

And this is our solution to solve the problem. The highlighted text in yellow is the command we need to use.

  1. Detect the largest files in all of revisions (For ex: find top 10 big files and sort them in descand order(the 3rd field of output result)): git verify-pack -v .git/objects/pack/pack-*.idx | sort -k 3 -g | tail -10find_largest-find_in_git_revisionsThe output is not the content that humand can read. We need to find file name from this mess.
  2. Find large files name from the decoded string(the 1st field of output from step 1): git rev-list –objects –all | grep 7d795c723acdc0edf3dad078796143410708


3. Remove these files out of git(For ex: every .psd file in any folder): git filter-branch –index-filter ‘git rm –cached –ignore-unmatch **/*.psd’

4. Remove git backup: rm -rf .git/refs/original/

5. Expire all of loose objects: git reflog expire –expire=now –all

6. Check if ther’s any loose objects left: git fsck –full –unreachable

7. Repack(recompress objects file into .pack file): git repack -A -d

8. Run git garbage collector: git gc –aggressive –prune=now

9. Push changes: git push -f

Now, your git repository is smaller because of deleting too large files that supposed to be stored outside of git.

I will have another post for explaining about loose objects, garbage collector, pack file somedays.

You can find some better ways or more optimized command. Feel free to discuss and share your knowledge.

Have funs,

P/s: Thank congts for supporting me!


1 Comment

Leave a Reply