DEV Community

JT Ziolo
JT Ziolo

Posted on

How to create a global gitignore file as an extra precaution against committing secrets and unnecessary files

Did you know that you can set up the equivalent of a global .gitignore file on your system? It's called an excludes file. You can set which file to use as your excludes file in your global git config under core.excludesFile. For example, to set the excludes file to the file named .gitexcludes in your home directory, use: git config --global core.excludesFile "~/.gitexcludes" (you can also edit your global .gitconfig file).

Note that this file is best used as an extra precaution. It should not take the place of setting up .gitignore files in your repos. Your excludes file is local to your system and only works for you, not for collaborators working with you on your repo.

My Excludes File and Explanations

Here are the contents of my excludes file:

node_modules
.env.local
.venv/
.vscode
GitIgnore.csproj
Enter fullscreen mode Exit fullscreen mode

Why I've chosen to ignore these files:

  • node_modules: The local cache containing downloaded Node.js dependencies. Your package.json file and package manager lock files will include all the information required to install your project's dependencies on a given machine. In addition, the contents of node_modules are often specific to the system that the dependencies were installed on, and the folder can be massive in size.
  • .env or .env.local: A common location for secrets that must not be committed. The exact files to ignore depends on your project. I use Next.js App Router for most of my web dev projects, where the convention is to store secrets in .env.local while storing other environment variables in other .env files
  • .venv/: This is what I typically name my virtual environment directory when working with Python. Virtual environments should be created and activated separately on each collaborator's machine, and the directory should not be committed. Instead, commit just the requirements.txt file and use it to install pip packages inside the virtual environment.
  • .vscode: This directory includes things specific to the VSCode IDE (my preferred IDE), like folder-specific user settings and extension files.
  • GitIgnore.csproj: This serves a special purpose in that it allows me to alter the build process for C# project files in repos that I'm working with without needing to worry about accidentally committing these changes later. For example, when one of my C# projects relies on a different C# project as a project dependency, running dotnet build on my C# project will also build the dependency. If the dependency includes custom build steps, then those steps can cause my build to fail, but I can avoid this issue by copying the original project file to GitIgnore.csproj and removing those custom steps. This sort of scheme could also be useful in other languages that have their own project or package files.

Top comments (0)