Create a Global GitIgnore Step-by-Step for MacOS and Windows
Make your life easier with a Global GitIgnore.
Why?
I got tired of copying and pasting the same set of ignore files for every project I created. Now, I keep all my OS and IDE specific files in a global gitignore and keep project specific files in the project’s gitignore.
What files go in a global gitignore file?
Operating system files should go in a global gitignore. This might include things like your IDEs settings, or temp files, metadata files, icon cache files etc..
Files you want to ignore in your project, should go into the project’s .gitignore.
Things like vendor packages, composer packages, cocoa-pods, Python virtual environments, Gradle files, basically any 3rd-party library. If you use some sort of package management system, then those packages should be in the .gitignore. Provided of course, you keep the definition file in your project that allows you to re-download those libraries.
On MacOS
Open Terminal and run the below command to create a new file. You don’t have to name it .gitignore_global
you could name it .gitignore
I appended _global so future me will remember what the file is used for.
touch ~/.gitignore_global
Now, let’s configure Git to use the exclude file ~/.gitignore_global
for all Git repositories.
git config --global core.excludesfile ~/.gitignore_global
Finally, open your new gitignore_global in Notepad or TextEdit and fill it with your computer/IDE level exclusions.
On Windows
Open Git Bash.
Navigate to the root folder of your user profile usually at C:\Users\{myusername}\
and create a .gitignore_global file.
Then run
git config --global core.excludesfile "%USERPROFILE%\.gitignore"
Using Windows PowerShell?
Run:git config --global core.excludesfile "$Env:USERPROFILE\.gitignore"
Verify it
Not every system is setup the same. So you can verify your MacOS, Windows or Windows PowerShell config file is correct by running:
git config --global core.excludesfile
The output should be the full path to your file.
If you see $HOME/.gitignore_global
or %USERPROFILE%\.gitignore
then something has gone wrong.
On Windows, if you can’t get it to work using the %USERPROFILE%
variable, you can just run from the bash prompt
git config — global core.excludesfile ~/.gitignore_global
Then, navigate into that folder and open the .gitconfig
file (it’s hidden) and manually edit the excludesfile
path to to reflect the location of your .gitignore_global
It might looks something like this:
[core]
excludesfile = C:\Users\adammcelhaney\.gitignore_global
Special Note
Git will not ignore a file that was already tracked before a rule was added to this file to ignore it. If you find you have checked in a file that you need to be un-tracked, you can run:
git rm --cached filename
Need suggestions on what to add to your Global GitIgnore?
Be sure to check Github’s own repo of suggested ignore files: https://github.com/github/gitignore