Remove accidentally pushed .env file from all git commits

Shawon Saha - Sep 26 '23 - - Dev Community

Make sure to safely backup the .env file first. Because this command will delete it from your system

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

Enter fullscreen mode Exit fullscreen mode
git push --force
Enter fullscreen mode Exit fullscreen mode

If you get error like this
Image description

Run this command and froce push

git update-ref -d refs/original/refs/heads/main
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .