← coderrocketfuel.com

Stop Git From Committing a .env File in a Node.js Application

In a Node.js application, your .env file often contains sensitive data like passwords, private API tokens, etc.

Therefore, you don't want that file committed to your remote repository on GitHub or GitLab.

How can you tell Git to ignore that file when committing and pushing your code to GitHub/GitLab?

In the root location of your application directory, create a new .gitignore file and add this line of code to it:

.env

The .gitignore file gives Git a list of files and/or directories that should not be tracked when creating commits and pushing code to remote locations.

This file should also include things like the node_modules directory, any build files/directories, log files, test outputs, and more.

Thanks for reading and happy coding!