Search code examples
gitignore

.gitignore file not ignoring .env.local or any other supposed private file


I encountered this error because I once mistakenly committed .env.local before adding it to .gitignore. The answer to it below.


Solution

  • If you're facing something similar and you use the Git GUI in VSCode, just follow these steps:

    • Open your .env.local file or any file that's having this issue, copy the content to clipboard, and delete the file entirely.
    • Open your .gitignore file and delete the line that removes that file.
    • Commit your changes (not necessarily publish).
    • Now create a new .env.local file (or the file you deleted earlier in your case.)
    • Switch to your Source Control tab (where you see staged files), right click on this newly created file and add it to .gitignore

    Now, it will be ignored for real.