Removing a file from Git History
Usually applicable when you want to remove a committed .env
file from your git history.
Note: This will rewrite your git history, so make sure you know what you're doing. And it is advisable to only do this, if your repo is not public (and has no changes pushed to it yet), because chances are someone has scraped or taken a look at it. Either way always regenerate any secrets/keys if exposed.
Procedure
- Update your
.gitignore
file to ignore the file you want to remove from history.
echo '.env' >> .gitignore
- Remove file from git
git rm --cached .env
- Remove file from git history
git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch .env" HEAD
- Force push to remote
git push --force