Oh OH! I pushed some sensitive files to my github repo. These included my .env file. I was going crazy looking for ways to remove this and other non-sense files from my repo. How to remove sensitive files from Github was tricky for me I have to admit. After putting my favorite espresso yourself bucket hat I was able to find a trick that worked for me.

I’m using a mac, I found a tool BFG that helped me remove my .env file. But, first do the below:

Edit your .gitignore. Make sure it includes the files you don’t want to push to your repo:

.env
*.env
pycache/

Then run: (make sure you use the correct path to your .env file)

git rm --cached .env

This removes .env from the Git index, but not from your local disk.

Remove sensitive data from Github history now

I used the BFG repo cleaner I mentioned above. Run the below commands:

First backup your repo:

git clone --mirror https://github.com/YOUR_USERNAME/YOUR_REPO.git

Run BFG: (I ran this one from my current project. Make sure your path to your .env is correct)

bfg --delete-files .env

Next, clean up the mess:

git reflog expire --expire=now --all
git gc --prune=now --aggressive
git push --force

In my case I had to:

git add .
git commit -m "code cleanup"
git push

To see the changes reflected in my Github repo in my account.

Brief summary of git and bfg commands

git and bfg commands

Contact me if you have any questions. See my coffee mugs, T-Shirt designs in my shop. I make my own designs.

Leave a comment

Your email address will not be published. Required fields are marked *