1283

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?

8
  • 10
    You might also want to check out GitHub's ignore suggestions -- help.github.com/articles/ignoring-files; they have a repository of common ignore patterns
    – drzaus
    Commented Jan 14, 2014 at 15:24
  • 6
    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. Commented May 1, 2014 at 0:53
  • 42
    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).
    – Pylinux
    Commented Apr 22, 2016 at 14:37
  • 2
    Python virtual environment directories are a common use case for entries in my global or local excludesfile.
    – Stew
    Commented May 26, 2016 at 17:39
  • 28
    @Pylinux 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.
    – Asclepius
    Commented Oct 3, 2016 at 23:08

14 Answers 14

2036

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

22
  • 4
    Can I use # at the beginning of lines inside ~/.gitignore to add comments inside the global ignore file?
    – Dror
    Commented May 14, 2013 at 13:36
  • 10
    Recent versions of the official Git Windows toolkit use the *nix syntax instead; if this line doesn't work, try the *nix one instead.
    – Andy Jones
    Commented Dec 5, 2013 at 19:21
  • 10
    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/.gitignore
    – Vince
    Commented Feb 22, 2014 at 7:43
  • 30
    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.
    – muruge
    Commented Jun 4, 2014 at 19:00
  • 8
    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: stackoverflow.com/a/22885996/9849440 Commented Feb 20, 2022 at 17:29
462

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.
14
  • 3
    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? Commented Nov 11, 2015 at 18:02
  • 3
    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) Commented Nov 4, 2016 at 14:27
  • 13
    Looks like on Windows the path is also ~/.config/git/ignore.
    – suriv
    Commented Jan 16, 2017 at 8:50
  • 7
    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.
    – iolsmit
    Commented Jul 8, 2019 at 13:07
  • 17
    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... Commented Feb 5, 2020 at 12:29
380

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.

1
  • 2
    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: stackoverflow.com/a/22885996/9849440 Commented Feb 20, 2022 at 17:34
75

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.
0
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!!

2
  • 3
    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?
    – amirhe
    Commented Oct 28, 2021 at 16:46
  • I.m.o it might just be better to append your stuff to the real .gitignore even if it does exist. Commented Mar 15, 2022 at 4:21
25

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 ?

2
  • 1
    I am trying to ignore globally, which is not happening!!
    – Mild Fuzz
    Commented Sep 7, 2011 at 14:38
  • @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/* Commented Apr 19, 2020 at 22:28
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
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
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.

4
  • 6
    It doesn't matter what you call your global ignore file so long as it matches your core.excludesfile config.
    – CB Bailey
    Commented Sep 8, 2011 at 12:24
  • 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. Commented Sep 8, 2011 at 13:56
  • I guess he thought the user would be smart enough to realise.
    – WORMSS
    Commented Mar 22, 2014 at 12:47
  • 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!
    – DarkteK
    Commented Nov 1, 2020 at 4:02
3

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.

4
  • I am trying to do this globally, not just per repo
    – Mild Fuzz
    Commented Sep 7, 2011 at 14:36
  • 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 ?
    – Nic
    Commented Sep 7, 2011 at 14:39
  • yeah, tried that. It seems to me I have followed all the recommended advice! What am I missing?
    – Mild Fuzz
    Commented Sep 7, 2011 at 14:42
  • As suggested, I'd add your git status and excludes info to the question :)
    – Nic
    Commented Sep 7, 2011 at 15:49
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.

1

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

-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.

-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)

Not the answer you're looking for? Browse other questions tagged or ask your own question.