The Be Sure Blog

Code Snippets | Problem Solving | Tips & Tricks

The Be Sure Blog banner

Delete files and folders from GitHub history

posted on 24.4.2023 by Below Surface in "GitHub"

Usage at your own risk. Read the official documentation for more info.

I wanted to delete two folders from a repositories history. Finally successful for me was the BFG Repo-Cleaner tool. I used it on MacOS like so:

First I downloaded the tool and placed it inside my repository's root. Then ran this command to repack the repository:

git gc

After that, I picked the command to delete all folders named "impressum":

java -jar bfg-1.14.0.jar --delete-folders "impressum"

It ran successfully and asked me to run this command:

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

Then, upload the changes to GitHub:

git add .
git commit -m "delete folder from history"
git push --force

The forced push may be necessary because Git will not just let you overwrite the repositories history. But since that's what we want, --force ignores any warning and pushes the changed history to GitHub.

To check the Git repositories history for a string that should be deleted, this command may be used:

git log -S password

You can exchange "password" with any string that should not be included in the repo anymore. If no string is found, there will be nothing displayed in the terminal. If there is something found, you get information about the commit where the data is included.

I'm still figuring this out, but if there is a conflict when pulling the changes to another machine, an easy way would be to just clone the repository again.

Tool download from here:




More posts of this category

Get started with GitHub Actions

Learn about CI/CD with GitHub


Git Merge VS Rebase, pros and cons for each

Learn more about git merge and git rebase and their differences


Fixing .gitignore is ignoring a .gitignore rule

Ignoring a folder does not work? This may be a solution!


CI/CD: How I finally fixed "err: npm WARN EBADENGINE"

Running the same .sh script may deliver two different results, not anymore!