Magento Cloud customers and operators might not know, but Magento Cloud Git repository size on the Cloud is not limitless; and because of this, a junior Engineer might not pay attention to details when pushing new commits into this Git repository.

A common mistake would be to commit and push “media” resources or big static files to the repository, causing an exponential growth when merging between branches; leading to being unable to connect via SSH or browser to any of the environments. Also, the Git repository might not be able to allow pushing new commits.

Usually, the first attempt to check and remove any media resource or unnecessary data is to remove, make a new commit and push back to the repository. Unfortunately, this workflow never really works. 

Deleting files in a commit doesn’t actually reduce the size of the repo since the earlier commits and blobs (files) are still around in previous commits and included in the git database, so the repository will never decrease. 

With that being said, the Magento Cloud repository can be reduced by following these steps:

  1. Navigate to the repository:

cd cloud_repository/

  1. Change to the branch that contains big files:

git checkout cloud-branch

  1. Execute filter-branch to remove the bigs files and save the re-written Git history in HEAD

git filter-branch –force –tree-filter ‘rm -rf pub/media/*’ HEAD

  1. Instruct Git to purge and prune the unwanted data:

git reflog expire –expire=now –all && git gc –prune=now –aggressive

  1. Lastly, force push to the repository:

git push –force origin master

After this, the repository size will be reduced; helping to prevent any of the issues caused by the lack of space in the Git repository; however, before performing these steps make sure to make a copy of the repository in case some info needs to be restored in case of errors

It is also good to take into account that after re-writing and/or purging the Git repository all the collaborators need to rebase or re-clone the repository, in order to avoid re-pushing the old commits and blobs.