GitHub has an article for exactly this. Check it out here. To sum up the article: you can use either the git filter-branch
command or the BFG Repo-Cleaner. BFG Repo-Cleaner is easier and faster to use, so I use that. To use BFG Repo-Cleaner follow these steps:
- Download the jar file at the project repo or with macos use
brew install bfg
- Clone a fresh copy of your repo, using the
--mirror
flag:
git clone --mirror git://example.com/some-big-repo.git
if using SSH or
git clone --mirror https://example.com/some-big-repo.git
if using HTTPS.
This is a bare repository so you won't be able to see your files but it will be a full copy of your repository with all commits.
- You can then use the following command to delete specific files from previous commits:
java -jar bfg.jar --delete-files [FILE NAME] --no-blob-protection my-repo.git
or if installed to the PATH
bfg --delete-files [FILE NAME] --no-blob-protection my-repo.git
or to delete a password from an old commit
bfg --replace-text passwords.txt
- Before pushing back up to your repo, check that the repo history has changed by going into your git repo folder and running the following command:
git reflog expire --expire=now --all && git gc --prune=now --aggressive
and then
git gc
to strip out unwanted data that you don't want to push back up to your repo.
- Once your happy, push back up to your remote repo by running
git push
- note that, because you used the --mirror
flag when cloning your repo, when you push back to your repo, you will also push back reference changes.
To read up more about BFG Repo-Cleaner, visit this link.