This note was originally posted on akashgoswami.com
A note for myself because I know I’ll probably do it again in the future…
I managed to accidently commited a text file to a repository and pushed it to GitHub 🤦🏽♂️
While it didn’t contain any sensitive data, I still wanted to remove it from the repository’s history. I found this guide on GitHub and these are the exact steps I followed to resolve this issue.
- Install bfg via Homebrew
brew install bfg
- Remove the file from the local repository and push this change to the GitHub repository
- Clone the repository from GitHub to a new location on my machine
gh repo clone GITHUB_USERNAME_HERE/REPOSITORY_NAME_HERE
- Ran the following command to remove the file from the repository’s history
bfg --delete-files FILENAME_HERE_WITH_EXTENSION
- Then I ran these commands together (This was what was recommended after running the command above)
git reflog expire --expire=now --all && git gc --prune=now --aggressive
- Force push the changes to GitHub
git push --force
The file should now be removed from GitHub and you should see a new commit message with no changes made.