Skip to content

Instantly share code, notes, and snippets.

@gjerokrsteski
Last active May 3, 2024 19:42
Show Gist options
  • Star 80 You must be signed in to star a gist
  • Fork 18 You must be signed in to fork a gist
  • Save gjerokrsteski/e4a10352448158ba827493eb116cda51 to your computer and use it in GitHub Desktop.
Save gjerokrsteski/e4a10352448158ba827493eb116cda51 to your computer and use it in GitHub Desktop.
remove env file from git history forever
echo '.env' >> .gitignore
git rm -r --cached .env
git add .gitignore
git commit -m 'untracking .env'
git push origin master
@RichardLindhout
Copy link

This only removes in the most recent unpushed commit not from your whole history

@RichardLindhout
Copy link

RichardLindhout commented Jun 5, 2020

git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch application/.env.example' --prune-empty --tag-name-filter cat -- --all

Updated to work across branches

@RichardLindhout
Copy link

Above works also for history

@ItsCosmas
Copy link

Thanks, this worked for me

@KristenLeach
Copy link

I remember needing to do this a long time ago and the instructions I found laid out SUCH a process... thanks for this tip, quick and easy.

@Mupati
Copy link

Mupati commented Sep 29, 2020

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

I just run this and ended up with this error in my CI Pipeline fatal: empty ident name. Anyway to go about it?

@PfanP
Copy link

PfanP commented Nov 13, 2020

It's really handy!

@jbakker87
Copy link

for me this was the perfect solution! I always ended up modifying the .env between MAC and WIN. Thanks a lot

@RichardLindhout
Copy link

Only this worked for me because I came across the same issue
https://stackoverflow.com/a/55017549/2508481

@bryanakitchen
Copy link

Perfect! Thank you so much!

@domicoder
Copy link

Thank you!

@kinho
Copy link

kinho commented Aug 31, 2021

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

worked for me, thanks!

@akashvaghela09
Copy link

Worked perfectly fine for me, Thanks.

@bhatvikrant
Copy link

This worked! Thanks

@michaelovo
Copy link

Thanks man!

@Gbillington1
Copy link

git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch application/.env.example' --prune-empty --tag-name-filter cat -- --all

Updated to work across branches

This did work to clear the commits from my history, but when checking the git tree with gitk --all I see that this moves them off of the main remote, and I can still see the .env commits. How do I clear these? See photo of git tree

@AbdulWahabAchakzai
Copy link

It worked for me, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment