Global Git ignore
Asked Answered
S

14

1266

I want to set up Git to globally ignore certain files.

I have added a .gitignore file to my home directory (/Users/me/) and I have added the following line to it:

*.tmproj

But it is not ignoring this type of files, any idea what I am doing wrong?

Satirical answered 7/9, 2011 at 14:16 Comment(8)
You might also want to check out GitHub's ignore suggestions -- help.github.com/articles/ignoring-files; they have a repository of common ignore patternsChrisoula
You may also want to consider just using a .gitignore inside individual projects. Otherwise if you (or someone else) clone the project on a new system, you'll also have to recreate the global excludesfile and configuration each time.Hetaera
I totally agree with @StanKurdziel. I can't think of a good reason to use a global .gitignore file. Maybe if you have a project with people using a huge variety of editors and IDE's you might not want to clutter the project's .gitignore with all kinds of things tailored to each IDE... but frankly I'd live with the clutter.Blurb
Ignorable files that the project creates should be in the project .gitignore file, ignorable files that stuff on your machine creates should go in the global .gitignore (like editor/IDE temp files and the like).Unshroud
Python virtual environment directories are a common use case for entries in my global or local excludesfile.Scrubland
@Unshroud Actually, per git-scm, personal IDE and workflow specific stuff can go in the untracked file .git/info/exclude in the repo, so it doesn't necessarily have to go in the global file. Also, the default and automatic global gitignore file is $HOME/.config/git/ignore.Motherly
see video here youtu.be/3LYBdd3RGKsKenspeckle
@Motherly Great solution but there is a caveat: Unfortunately, because of IMHO some git silliness regarding adding patterns into .git/info/exclude for files which have already had changes made to them, the user will notice that the file still is not being ignored :-( .. A couple of solutions may be found at #26882945 .. The "update-index --assume-unchanged" solution worked perfectly for me.Sebastiansebastiano
G
2015

You need to set up your global core.excludesfile configuration file to point to this global ignore file e.g:

*nix or Windows git bash:

git config --global core.excludesFile '~/.gitignore'

Windows cmd:

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

Windows PowerShell:

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

For Windows it is set to the location C:\Users\%username%\.gitignore. You can verify that the config value is correct by doing:

git config --global core.excludesFile

The result should be the expanded path to your user profile's .gitignore. Ensure that the value does not contain the unexpanded %USERPROFILE% string.

Important: The above commands will only set the location of the ignore file that git will use. The file has to still be manually created in that location and populated with the ignore list. (from muruge's comment)

You can read about the command at https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files#configuring-ignored-files-for-all-repositories-on-your-computer

Garnishee answered 7/9, 2011 at 14:21 Comment(22)
I have done this, then I rmed the *.tmproj, resaved the file and it still shows up as an untracked changeSatirical
@MildFuzz: Do you mean that it's showing up in git status under Untracked files: ? This shouldn't happen, I've just done a quick test on my system and it works as expected for me.Garnishee
Will it be because the file has been tracked in the past?Satirical
So long as it's not in your HEAD or your index it shouldn't make any difference whether the file was once tracked or not. It may be helpful if you add the output of git status, git config core.excludesfile to your question.Garnishee
Since there are several levels of configuration (system, global, local), you should check to find out your "effective" core.excludesfile. Type: git config core.excludesfile. You may find that it refers to something like $HOME/.gitignore_global.Erlandson
Can I use # at the beginning of lines inside ~/.gitignore to add comments inside the global ignore file?Georgia
Recent versions of the official Git Windows toolkit use the *nix syntax instead; if this line doesn't work, try the *nix one instead.Trivet
I wasn't able to get this to work using the %USERPROFILE% variable. I had to enter the full path to the file using *nix directory separators. e.g. core.excludesfile C:/Users/User/.gitignoreZeiger
You can also try %HOME% instead of %USERPROFILE%.Phono
Couldn't get it working with %USERPROFILE% variable. I had to use git config --global core.excludesfile '~/.gitignore' in windows to get it set to the location C:/users/{myusername}/.gitignore. Also the above command will only set the location of the ignore file that git will use. The file has to still be manually created in that location and populated with the ignore list.Collision
Note: This works in CMD on my Windows 7, but DOES NOT work in PowerShell.Vilma
Yes, it should be noted that if you're running this from git bash on Windows, you should use ~/.gitignore as well. The %USERPROFILE% var will work in cmd.Jasminjasmina
Remember if you apply this change after the fact then you'll need to clear your repo's cache for the change to take effect.Diamonddiamondback
The quotes are actually not required, you could simply go git config --global core.excludesfile ~/.gitignore. At least on Git 1.9.4 that is.Roryros
in my case %USERPROFILE% didn't work. I tried with and without quotes. I think it's taking it as part of the filename. I ended up swapping in the actual location behind the alias and that is working.Heiser
Hello @CharlesBailey, thanks for your answer. I've set my global gitignore long ago and today I have an opposite problem: can I ignore my global gitignore :). I have one repo that I need to push an .exe file. I don't wan't to change my global config for just one repo. Any tips? Thank you.Para
Oh, sorry to bother! I just got it working with command git config --local core.excludesfile "". Thanks anyway, great answer. If you find like it, you can add this addendum to it. May be helpful. See you.Para
On windows, be careful about the quotes when you copy the example above. Make sure you are using normal " quotes in your command. I copied the example and it gave me special quote characters which git thought was part of my path making it appear not to work.Actinochemistry
Can I negate global .gitignore for example if I have /src/ in global ignores? But for one single project I want to have !/src/ will this work?Upperclassman
~/.gitignore works on WSL (windows subsystem for linux) tooImmitigable
As a side note, there is also the option to set a repo specific ignore list without committing it to the repo, using a file set in the .git folder of the repo, which will just stay local to you. See docs.github.com/en/get-started/getting-started-with-git/…Artis
Beware that there is already default location that git uses for this file. And doing this is overwriting it. I would recommend just using the default. See this answer below: https://mcmap.net/q/45439/-global-git-ignoreVista
C
454

Although other answers are correct they are setting the global config value whereas there is a default git location for the global git ignore file.

You can check if the global gitignore exists via:

git config --get core.excludesfile

Typically, it is found in these locations on different operating systems:

*nix:

~/.config/git/ignore

Windows:

%USERPROFILE%\.config\git\ignore

You may need to create the git directory and ignore file and tell git the location via git config --global core.excludesfile ~/.config/git/ignore but then you can put your global ignores into that file and that's it!

Source

Which file to place a pattern in depends on how the pattern is meant to be used.

  • Patterns which a user wants Git to ignore in all situations (e.g., backup or temporary files generated by the user’s editor of choice) generally go into a file specified by core.excludesFile in the user’s ~/.gitconfig. Its default value is $XDG_CONFIG_HOME/git/ignore. If $XDG_CONFIG_HOME is either not set or empty, $HOME/.config/git/ignore is used instead.
Cesarcesare answered 5/4, 2014 at 19:52 Comment(14)
This is great; I didn't know about this... however isn't this a generated file? Can it be overwritten when you update/re-install?Colima
I believe the file is created/modified by running certain git commands. Since it is a settings file in each user's directory, I don't think that it should ever be overwritten.Cesarcesare
I agree this should be the accepted answer, however I didn't find this in -t̶h̶e̶ ̶d̶o̶c̶u̶m̶e̶n̶t̶a̶t̶i̶o̶n̶ a help article (help.github.com/articles/ignoring-files)Kweichow
@Kweichow I just edited the answer to reflect the change in how the source doc is organized.Cesarcesare
@Cas do you have the source for the Windows path? It does not work for me.It
Looks like on Windows the path is also ~/.config/git/ignore.It
After doing this do I still need to run git config --global core.excludesfile?Ileostomy
On macOS this does not work by default. As the git documentation states: $XDG_CONFIG_HOME/git/ignore, $GIT_DIR/info/exclude, .gitignore are parsed, but $XDG_CONFIG_HOME is not set by default on macOS. Thus, if git config --get core.excludesfile doesn't output the folder it didn't work and you have to set it manually to git config --global core.excludesfile ~/.config/git/ignore or actually any other location that's convenient for you.Siloam
Using this default ignore file is nice on Windows because it will be used by both Git for Windows and the Git that comes with Cygwin.Spatterdash
For me, the core.excludesFile was not set to any value by default. (I checked before making any changes and the setting had no value) So, users may still need to set the core.excludesFile setting, even if using the listed paths.Tasty
works by default on macOS git version 2.21.1 (Apple Git-122.3).Freed
This is surely the best answer, is there any way to override the accepted one? A lot of people will end up making their lives more complex than they need to if they follow the other answers...Phenylalanine
works by default on Mac OS X git version 2.24.3 (Apple Git-128)Ammoniate
The location of the ~/.config file may be customized by setting the XDG_CONFIG_HOME environment variable. Seems that on macOS, it is almost a must.Impi
D
379

Before reconfiguring the global excludes file, you might want to check what it's currently configured to, using this command:

git config --get core.excludesfile

In my case, when I ran it I saw my global excludes file was configured to

~/.gitignore_global
and there were already a couple things listed there. So in the case of the given question, it might make sense to first check for an existing excludes file, and add the new file mask to it.
Darreldarrell answered 27/5, 2013 at 21:31 Comment(1)
There is a default location git looks for this file and git config --get core.excludesFile does not return it, unfortunately. What is currently "configured" if nothing is returned from that is something like ~/.config/git/ignore. See this answer below: https://mcmap.net/q/45439/-global-git-ignoreVista
D
70

To create global gitignore from scratch:

$ cd ~
$ touch .gitignore_global
$ git config --global core.excludesfile ~/.gitignore_global
  1. First line changes directory to C:/Users/User
  2. After that you create an empty file with .gitignore_global extension
  3. And finally setting global ignore to that file.
  4. Then you should open it with some kind of notepad and add the needed ignore rules.
Deanadeanda answered 19/8, 2015 at 22:54 Comment(0)
F
51
  1. Create a .gitignore file in your home directory
touch ~/.gitignore
  1. Add files/folders to it

Example

# Files
*.gz
*.tmproj
*.7z

# Folders
.vscode/
build/

# If folders don't work, you can still do this
.vscode/*
build/*
  1. Check if a git already has a global gitignore
git config --get core.excludesfile
  1. Tell git where the file is
git config --global core.excludesfile '~/.gitignore'

Voila!!

Feat answered 1/4, 2019 at 16:24 Comment(2)
It seems that name of the file doesn't matter & I found that Kent. C Dodds named this file .gitignore_global, isn't that is the better option eliminating little chance of collision with real .gitignore?Spry
I.m.o it might just be better to append your stuff to the real .gitignore even if it does exist.Feat
E
26

From here.

If you create a file in your repo named .gitignore git will use its rules when looking at files to commit. Note that git will not ignore a file that was already tracked before a rule was added to this file to ignore it. In such a case the file must be un-tracked, usually with :

git rm --cached filename

Is it your case ?

Event answered 7/9, 2011 at 14:20 Comment(2)
I am trying to ignore globally, which is not happening!!Satirical
@MildFuzz on Linux the core.excludesfile file seems to be interpreted differently than a .gitignore file. If you want to ignore an entire directory just put the name of the directory like .vscode/ instead of .vscode/*Hardy
S
15

If you use Unix system, you can solve your problem in two commands. Where the first initialize configs and the second alters file with a file to ignore.

$ git config --global core.excludesfile ~/.gitignore
$ echo '.idea' >> ~/.gitignore
Switchman answered 21/6, 2019 at 15:26 Comment(0)
C
12

Remember that running the command

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

will just set up the global file, but will NOT create it. For Windows check your Users directory for the .gitconfig file, and edit it to your preferences. In my case It's like that:

[core]
  excludesfile = c:/Users/myuser/Dropbox/Apps/Git/.gitignore
Chrisom answered 30/1, 2015 at 5:47 Comment(0)
D
5

I am able to ignore a .tmproj file by including either .tmproj or *.tmproj in my /users/me/.gitignore-global file.

Note that the file name is .gitignore-global not .gitignore. It did not work by including .tmproj or *.tmproj in a file called .gitignore in the /users/me directory.

Diocesan answered 8/9, 2011 at 11:43 Comment(4)
It doesn't matter what you call your global ignore file so long as it matches your core.excludesfile config.Garnishee
Ah! Good point @CharlesBailey. However this may be something the writer may want to check. The reason the .tmproj file is not getting ignored may be because the user's excludesfile is not be .gitignore.Diocesan
I guess he thought the user would be smart enough to realise.Sigridsigsmond
This should have more upvotes, I had the same issue with my git not reconizing the .gitignore globally, just because its name was .gitignore too... So I decided to change it to .gitignore_global and change the "core.excludesfile" to point to that new file and it worked like a charm!Dehiscence
C
2

You should create an exclude file for this. Check out this gist which is pretty self explanatory.

To address your question though, you may need to either de-index the .tmproj file (if you've already added it to the index) with git rm --cached path/to/.tmproj, or git add and commit your .gitignore file.

Cilka answered 7/9, 2011 at 14:20 Comment(4)
I am trying to do this globally, not just per repoSatirical
Yeah, that's what I linked to in the gist. I was a little confused by the original question. Did you do the git rm --cached ?Cilka
yeah, tried that. It seems to me I have followed all the recommended advice! What am I missing?Satirical
As suggested, I'd add your git status and excludes info to the question :)Cilka
R
1

on windows subsystem for linux I had to navigate to the subsystem root by cd ~/ then touch .gitignore and then update the global gitignore configuration in there.

I hope it helps someone.

Receive answered 4/7, 2018 at 10:50 Comment(0)
A
0

You can use the default global gitignore file that is located here (in POSIX systems):

~/.config/git/ignore

If you don't want to change your git config

Alkoran answered 20/10, 2023 at 15:46 Comment(0)
K
-1

Another possible solution if the .gitignore approach isn't working for you is to:

git update-index --skip-worktree path_to_file

That will ignore changes to that file, both local and upstream, until you decide to allow them again with:

git update-index --no-skip-worktree path_to_file

You can get a list of files that are marked skipped with:

git ls-files -v . | grep ^S

Note that unlike --skip-worktree, the --assume-unchanged status will get lost once an upstream change is pulled.

Kirchhoff answered 7/10, 2021 at 21:49 Comment(0)
E
-3

If you're using VSCODE, you can get this extension to handle the task for you. It watches your workspace each time you save your work and helps you to automatically ignore the files and folders you specified in your vscode settings.json ignoreit (vscode extension)

Efface answered 1/5, 2020 at 15:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.