Removing a .env file from Git history

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
What's the point in doing this? Regardless of what's in git, the credentials need revoked and git-ignored to prevent in the future. Removing from git history is some extra steps that doesn't really do anything.
Hey Jimmie,
I just like clean repo's. And like I said the env is more a demonstration of a common example.
It might have been another file you wish to remove from the history all together.
Haha, I hope you never have to use it. But that day you do, it's here for you π
π Chris Bongers.
I've made the mistake once, while I was writing an article and pushing the code to GitHub, I revoked the Key but didn't know about this solution.
This will come in handy when someone else asked me or a colleague makes the mistake.
It's just so nice to know this things exist on your blog π
Happens to everyone.
I've added a small subnote: If you accidentally pushed real-world secrets to a repo, you should always revoke and regenerate them.
Thanks for that, I will definitely keep that in mind.
Glad you like it Vishwajeet! π
In this series we'll cover some basic, but very important topics in Git, GitHub and Open source
Let's say we have a file called timezone.js, and we commit this file to Git. All good and well. But then we realized the whole repo used "time zone" with a space. Apparently, there are three correct spellings of timezone: timezone, time zone, and t...
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

I'm sure this happens to everyone sometimes. You accidentally pushed a file with secrets or a password that shouldn't have gotten into the Git history.
In the following example, I "accidentally" pushed my .env file to Git simply because I forgot to add it to me .gitignore file.

Note: If you accidentally pushed secret keys to a repo, you should always revoke them and generate fresh keys!
The best thing to do now is to remove the file right away and add it to your .gitignore file.
In my case, I added the following to the .gitignore.
# Secret file
.env
Let's try and push that to see what happens.

Yep, the .gitignore file doesn't untracked already committed changes. So how can we fix this now?
You can remove a file from Git by running the following command.
git rm -r --cached .env
If we then push this change, you will see that the file is gone in GitHub.

However, this didn't completely solve our issue. If we look at our Git history, we can still find the file and expose the secrets!

To remove the file altogether, we can use the following command.
git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch .env" HEAD
You will get some warnings about this messing up your history as this goes through your whole history and 100% removes its occurrence.
To push this, you have to run the following command.
git push --force
If we look at our history, we can still see the commits that include this .env file, but the content is empty.

Few, thanks for having our back Git!
You can find the repo it tried this in on GitHub.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter