Building a Windows Global Gitignore File

Having a .gitignore file is handy when working in a git repository by keeping a list of file patterns to ignore. It’s a good practice to ignore files that aren’t needed in the repository, such as system files, image thumbnails, and other artifacts that are not needed by others. See the official documentation for more details.

Make sure to check out the post titled Developing with Python 3 on Windows for more goodies.

Generating a Project Specific Gitignore

Generating a gitignore file requires some knowledge of what you wish to ignore. Using a gitignore generator makes this trivial – I suggest trying gitignore.io. By supplying some information about your project details, such as the operating system and programming language, the generator spits out the guts of a gitignore file that can be copied into your project.

For example, I have entered Python and Windows into the project input box below:

The resulting gitignore file contains a long list of entries that instruct git to ignore files matching these patterns.

Making Gitignore Globally Available

I find it annoying having to constantly seed each project with a new gitignore file. Instead, I prefer to keep a global gitignore file in my user profile location. This is a great place to store system-level ignore patterns, such as for Windows or Linux, because your operating system is static.

To do this, drop the .gitignore file into your user profile folder, such as \Users\chris\ for me, as this does not trigger User Account Control (UAC) elevation and makes sense for a user configuration.

Next, update the git configuration by using the command below from a PowerShell console:

git config --global core.excludesfile "$env:USERPROFILE\.gitignore"

The $env:USERPROFILE is referring to an environmental variable value (hence $env:) and is replaced with your user profile path when executed. Alternatively, you can add the parameter directly to the .gitconfig file.

Once complete, the entry below will appear in gitconfig:

core.excludesfile=C:\Users\chris\.gitignore

Alternatively, use git to query the configuration using the command below:

git config --global core.excludesfile

Which should result in the proper path:

> git config --global core.excludesfile

C:\Users\chris\.gitignore

Keep in mind that the git configuration value must be the real path to your gitignore file.

Copying Gitignore into a Project

If you’d like to copy the gitignore file into the project folder, you can easily do so with a one-liner script.

Copy-Item $env:USERPROFILE\.gitignore .\.gitignore