I would like to share the simplest and easy-to-understand solution which worked for me.
First clone a fresh copy of your repository, using the --mirror flag:
git clone --mirror https://github.com/username/youproject.git
Then download latest version of the BFG JAR file from BFG Repo-Cleaner. Rename it as bfg.jar, and paste it inside the YourRepoName.git folder.
Then run following lines in Git Bash.
java -jar bfg.jar --delete-files yourfilename (only the file name is needed; there isn't any need to mention the path)
git reflog expire --expire=now --all && git gc --prune=now --aggressive (it will strip out the unwanted dirty data which has been expelled out due to the above command)
I faced an issue here. My project had open pull requests. If there are any open pull requests then you need to run this command:
git show-ref | cut -d' ' -f2 | grep 'pull-request' | xargs -r -L1 git update-ref -d
After this, you can simply push the master branch.
git push -u origin master
It’s done. But remember to keep a copy in local of old repository before performing above action. All unmerged branches and open pull requests may get deleted.
I reduced my repository size from 40 MB to 4 MB by removing unwanted APK files which got pushed with my commits.