How to remove .env from Git commit history

Alger Makiputin
2 min readApr 25, 2022

--

I am currently working on a new react native project where I have to use API to send and receive data to the server. React native .gitignore does not ignore .env file out of the box. When I pushed my commits I accidentally include a file that includes sensitive information like username and password, these are information that we do not want to disclose to anyone. And I am sure this happens to everyone sometimes, and you’re probably reading this because you did as well. But don’t worry git have a features to remove a specific file in the commit history. Here’s what you have to do.

#1 Add the file to your .gitignore

First things first, add that file that contains sensitive information to .gitignore. Probably the .env file where we usually store sensitive information.

.env

#2 Run this command

git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch .env" HEAD

Press enter and wait for a few seconds to let git run through your commit history and remove your .env file.

#3 Push the changes

git push --force

After pushing the changes, now you cannot see the .env file anymore on your commit history and you have safeguarded your information. Next time let’s keep in mind to review before we push.

Thanks for reading this article. Leave a comment below if you have any questions, and make sure to follow me here on medium :) https://medium.com/@algerwrites

--

--

Alger Makiputin
Alger Makiputin

Written by Alger Makiputin

Software developer, working across mobile, web, and custom software development. Creator of POSLite www.poslitesoftware.com

Responses (1)