logo

Ignoring the .env file in Git

If you made a change in your .env file, and you see that Git is not ignoring the file despite having it included in your .gitignore file, there is a significant chance that the file was already added to your repository before you added it to .gitignore.

If the .env file is not being ignored because it was already tracked in your Git repository, you'll need to untrack it. You can use the following command to stop tracking it without deleting it from your working directory:

git rm --cached .env

Commit the change; this removes the file from the index but keeps it in your working directory:

git commit -m "fix: stop tracking .env file"

Then verify the status of your repository to ensure that the .env file is now ignored:

git status

Push changes to your remote repository:

git push origin <branch-name>

Steps to check the commit history

If you are curious and want to check the commit where the .env file was included in your Git repository, you can use the following command:

git log -- .env

This command will show the commit history related to the .env file. Look for the earliest commit in the output to find when it was added.

You can also check the commit history related to the .gitignore file by using the command from below. The output will display a series of commits that have affected the .env file. Each commit will include a commit hash, which is a unique identifier for that specific commit.

git log -- .gitignore

If you want to see details of a specific commit (take the commit hash), and run the following command:

git show <commit-hash>

In the project I'm currently working on, the .env file was already added to the Git repository and, after that, was included to .gitignore.

Current Git workflow