Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Global gitignore

There are certain files created by particular editors, IDEs, operating systems, etc., that do not belong in a repository. But adding system-specific files to the repo's .gitignore is considered a poor practice. This file should only exclude files and directories that are a part of the package that should not be versioned (such as the node_modules directory) as well as files that are generated (and regenerated) as artifacts of a build process.

All other files should be in your own global gitignore file:

  • Create a file called .gitignore in your home directory and add any filepath patterns you want to ignore.
  • Tell git where your global gitignore file is.

Note: The specific name and path you choose aren't important as long as you configure git to find it, as shown below. You could substitute .config/git/ignore for .gitignore in your home directory, if you prefer.

Mac

git config --global core.excludesfile ~/.gitignore

Windows

git config --global core.excludesfile "%USERPROFILE%\.gitignore"

If using Powershell (credit: @kupmanj):

git config --global core.excludesFile "$Env:USERPROFILE\.gitignore"

This will result in an entry in your .gitconfig that looks like this:

[core]
    excludesfile = {path-to-home-dir}/.gitignore

Confirm location

Particularly for Windows users, verify any filename was correctly parsed for quotes and expansion:

git config --global core.excludesFile

Isn't there already a default global ignore?

Depending on your system, and whether the XDG_CONFIG_HOME environment variable is set, there might be a default location and there might actually be a file at that location. The best practice is to ensure the file exists where you want and explicitly tell git about it using git config --global core.excludesFile.

Global .gitignore contents

Depending on your OS and tools, the following contains sample of what you might want to include. When you run git status before adding any files to your local repo, check to see if any files don't belong. Add them to your global gitignore as appropriate.

# Node
npm-debug.log

# Mac
.DS_Store

# Windows
Thumbs.db

# WebStorm
.idea/

# vi
*~

# General
log/
*.log

# etc...

Visual Studio Code

If you want search to ignore files that you've set in your local .gitignore, you must check:

  • Search: Use Ignore Files

If you want search to ignore files that you've set in your global ignore, you must also check this:

  • Search: Use Global Ignore Files

Or edit settings directly:

"search.useIgnoreFiles": true,
"search.useGlobalIgnoreFiles": true

Final notes

If you want to exclude files on a per-repo basis without modifying .gitignore, you can directly edit .git/info/exclude in the repo. Nothing under the local .git directory is committed.

I find myself often creating "scratch" code that I don't want to commit. I do this enough that I found it useful to add scratch/ to my global ignore. I've personally never worked on a project where this is an issue because a directory called scratch should not be a ignored, but if this is a concern, try using __scratch__ or something similar.

You might find useful ignore patterns for your projects here on GitHub: https://github.com/github/gitignore

@ElMatella
Copy link

ElMatella commented Mar 21, 2018

Cool, that works great! Ideal for the DS_STORE mac file and eventually the .idea folder generated by all the Jetbrains IDEs

@markwartman1
Copy link

markwartman1 commented Apr 1, 2018

So, a list much longer like Octocat has generated, is that list something to put in my own global gitignore file?

@randallb
Copy link

randallb commented Apr 16, 2018

Thanks for this! 😄

@bwangelme
Copy link

bwangelme commented May 30, 2018

It helps me, thanks!

@amfischer
Copy link

amfischer commented Jun 19, 2018

Thank you man, much appreciated!

@eonist
Copy link

eonist commented Sep 6, 2018

Where is the global gitignore file in macOS? ~/.config/git/ignore doesn't exist

@hbroer
Copy link

hbroer commented Oct 2, 2018

you set it to where ever you want it. If you set it with git config --global core.excludesfile ~/.gitignore you have to create it there: ~/.gitignore ;)

@nullcookies
Copy link

nullcookies commented Oct 9, 2018

Thank you

@latch2112
Copy link

latch2112 commented Mar 5, 2019

thanks

@koopmanj
Copy link

koopmanj commented May 1, 2019

thx, for users with blanks in their profilename:
pwsh> git config --global core.excludesfile $env:USERPROFILE\.gitignore , quite obvious but for the most lazy folks

@timxor
Copy link

timxor commented May 28, 2019

thank you!

@tomholford
Copy link

tomholford commented Jun 26, 2019

Thank you, added this to my dotfiles:

https://github.com/tomholford/dotfiles

@OGProgrammer
Copy link

OGProgrammer commented Sep 5, 2019

Had to edit vim ~/.gitignore as the command did not create the file on my mac.

@dioni21
Copy link

dioni21 commented Sep 6, 2019

Nice hint, but please, suggest a differente name, like .gitgnore_global as shown at https://help.github.com/en/articles/ignoring-files

Many people use git at their home, for dotfiles, et al.

@erikyuzwa
Copy link

erikyuzwa commented Sep 24, 2019

until last week, ~/.config/git/ignore was the way to go, but now I had to move to ~/.gitignore - OSX 10.14.6 with git 2.23.0

@samuelwilliams
Copy link

samuelwilliams commented Dec 20, 2019

For adding the Windows global ignore, in some instances there should be quotes around the path, i.e. git config --global core.excludesfile "%USERPROFILE%\.gitignore" as %USERPROFILE% may contain spaces that git will not parse correctly.

Exempli Gratia: C:\Users\Joe Bloggs\.gitignore.

Git will not add the .gitignore path correctly without double quotes.

@BenjaminRqt
Copy link

BenjaminRqt commented Jan 9, 2020

@sF0rman
Copy link

sF0rman commented Feb 18, 2020

had to name the file .gitignore_global for it to work for me (on windows 10)
https://stackoverflow.com/questions/7529266/git-global-ignore-not-working

@rpgdev
Copy link

rpgdev commented Apr 19, 2020

Naming this file .gitignore seems to be somewhat of a misnomer. On Linux at least, a .gitignore file and the core.excludesfile file seem to be interpreted differently by git. For example, this rule .dir/* would be completely ignored in a core.excludesfile but not in a .gitignore.

@lanyado
Copy link

lanyado commented Jun 22, 2020

Thank you!

@skupjoe
Copy link

skupjoe commented Jun 24, 2020

One more thing to add,

Make sure you do this from a command prompt. Powershell will not expand the %USERPROFILE% and it will use this string literally.

@skupjoe
Copy link

skupjoe commented Jul 8, 2020

I found that using .gitgnore_global, for whatever reason, works more reliably with pattern matching. Not sure why!

git config --global core.excludesfile "%USERPROFILE%\.gitignore_global"

@rafaelkendrik
Copy link

rafaelkendrik commented Sep 8, 2020

If the file that you want to ignore already exists in the repository, you can use update-index:

$ git update-index --assume-unchanged FILENAME

@azzamsa
Copy link

azzamsa commented Jan 28, 2021

nice idea!

@keremkayhan
Copy link

keremkayhan commented Aug 11, 2021

great practice. thanks.

@mikeslattery
Copy link

mikeslattery commented Sep 8, 2021

IMO, a global gitignore should only contain things specific to a user's environment (OS, IDE, text editor). For example, I have .idea in mine.

It should not contain files created or used by tools used by all members of the projects that a user works on (e.g. npm, gradle). I do not think *.log, /log or npm-debug.log should be in it. If in doubt, leave it out.

@pofl
Copy link

pofl commented Feb 16, 2022

Thank you for this guide! A useful addition to this would be a link to http://gitignore.io or even directly to https://www.toptal.com/developers/gitignore/api/linux,macos,windows,intellij,visualstudiocode,git
I strongly recommend adding this to your global gitignore.

@zning1994
Copy link

zning1994 commented May 26, 2022

Thanks a lot for sharing about .gitignore_global~

@zning1994
Copy link

zning1994 commented May 26, 2022

Thank you for this guide! A useful addition to this would be a link to http://gitignore.io or even directly to https://www.toptal.com/developers/gitignore/api/linux,macos,windows,intellij,visualstudiocode,git I strongly recommend adding this to your global gitignore.

That's great! Thanks for sharing!

@MooreTattz
Copy link

MooreTattz commented Aug 4, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment