Skip to content

Instantly share code, notes, and snippets.

@gjerokrsteski
Last active January 5, 2025 11:01
Show Gist options
  • 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
@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!

@aussenseiter
Copy link

Thank you, it really helps

@PranotiPD
Copy link

It did work thank you!

@IdrisKulubi
Copy link

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

@IdrisKulubi
Copy link

It really worked thank you

@delabi
Copy link

delabi commented Jul 11, 2024

Perfect

@coderkhalide
Copy link

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

worked for me, thanks!

worked for me too, thanks

@alamindevms
Copy link

Thanks, worked for current commit. But .env file exist in previous commits.

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

This did not work for me.

@Antardas
Copy link

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

worked for me, thanks!

it's also works for me
Thanks

@gjerokrsteski
Copy link
Author

I'm blown away by the response to this Gist! Thanks so much to everyone who has starred and forked this repo. Your support means the world to me. If you have any questions or suggestions, please don't hesitate to reach out. PEACE!

@sydtanvirali
Copy link

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?

this work for me. thanks

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