1233

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?

Community
  • 1
  • 1
Mild Fuzz
  • 29,463
  • 31
  • 100
  • 148
  • 10
    You might also want to check out GitHub's ignore suggestions -- https://help.github.com/articles/ignoring-files; they have a [repository](https://github.com/github/gitignore) of common ignore patterns – drzaus Jan 14 '14 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. – Stan Kurdziel May 01 '14 at 00:53
  • 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. – DanielSank Mar 06 '16 at 01:20
  • 36
    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 Apr 22 '16 at 14:37
  • 2
    Python virtual environment directories are a common use case for entries in my global or local excludesfile. – Stew May 26 '16 at 17:39
  • 27
    @Pylinux Actually, per [git-scm](https://git-scm.com/docs/gitignore), 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 Oct 03 '16 at 23:08
  • see video here https://youtu.be/3LYBdd3RGKs – Shailesh Ladumor Jan 31 '21 at 07:15
  • 1
    @Asclepius 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 https://stackoverflow.com/questions/26882445/files-in-git-info-exclude-not-work .. The "update-index --assume-unchanged" solution worked perfectly for me. – Androclus May 02 '22 at 20:51

13 Answers13

1960

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

Smart Manoj
  • 5,230
  • 4
  • 34
  • 59
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • I have done this, then I `rm`ed the *.tmproj, resaved the file and it still shows up as an untracked change – Mild Fuzz Sep 07 '11 at 14:37
  • @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. – CB Bailey Sep 07 '11 at 14:43
  • Will it be because the file has been tracked in the past? – Mild Fuzz Sep 07 '11 at 14:45
  • 3
    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. – CB Bailey Sep 07 '11 at 14:47
  • 1
    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`. – Wil Moore III May 14 '12 at 03:19
  • 4
    Can I use `#` at the beginning of lines inside `~/.gitignore` to add comments inside the global ignore file? – Dror May 14 '13 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 Dec 05 '13 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 Feb 22 '14 at 07:43
  • You can also try `%HOME%` instead of `%USERPROFILE%`. – Steve Crockett Apr 07 '14 at 19:41
  • 29
    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 Jun 04 '14 at 19:00
  • 1
    Note: This works in CMD on my Windows 7, but DOES NOT work in PowerShell. – Joe Jun 16 '14 at 18:26
  • 1
    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`. – zrooda Jun 27 '14 at 12:27
  • 1
    Remember if you apply this change after the fact then you'll need to [clear your repo's cache](http://stackoverflow.com/questions/11451535/gitignore-not-working) for the change to take effect. – Jujhar Singh Aug 01 '14 at 09:41
  • 2
    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. – Ain Tohvri Sep 12 '14 at 13:25
  • 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. – Sonic Soul Apr 07 '15 at 18:30
  • 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. – DrBeco Jun 07 '16 at 17:49
  • 1
    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. – DrBeco Jun 07 '16 at 17:51
  • 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. – Adam Lane Nov 14 '18 at 17:35
  • 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? – redanimalwar Feb 02 '21 at 17:31
  • `~/.gitignore` works on WSL (windows subsystem for linux) too – isaias-b Feb 10 '21 at 13:49
  • 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 https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files#excluding-local-files-without-creating-a-gitignore-file – redfox05 Nov 08 '21 at 18:21
  • 6
    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://stackoverflow.com/a/22885996/9849440 – Jeremiah England Feb 20 '22 at 17:29
432

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.
ironicaldiction
  • 1,200
  • 4
  • 12
  • 27
Steve Jorgensen
  • 11,725
  • 1
  • 33
  • 43
  • 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? – danielsdesk Nov 11 '15 at 18:02
  • 1
    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. – Steve Jorgensen Jan 04 '16 at 10:19
  • 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 (https://help.github.com/articles/ignoring-files/) – AVProgrammer Nov 04 '16 at 14:27
  • 1
    @AVProgrammer I just edited the answer to reflect the change in how the source doc is organized. – Steve Jorgensen Nov 04 '16 at 18:01
  • @Cas do you have the source for the Windows path? It does not work for me. – suriv Jan 16 '17 at 08:27
  • 13
    Looks like on Windows the path is also `~/.config/git/ignore`. – suriv Jan 16 '17 at 08:50
  • 1
    After doing this do I still need to run `git config --global core.excludesfile`? – henrique Apr 17 '19 at 14:35
  • 6
    On macOS this does not work by default. As the [git documentation](https://git-scm.com/docs/gitignore) 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 Jul 08 '19 at 13:07
  • 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. – Brian Vosburgh Jul 08 '19 at 22:17
  • 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. – Venryx Oct 16 '19 at 00:01
  • works by default on macOS `git version 2.21.1 (Apple Git-122.3)`. – Peter Jan 30 '20 at 07:37
  • 16
    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... – Sam Salisbury Feb 05 '20 at 12:29
  • works by default on Mac OS X git version 2.24.3 (Apple Git-128) – The Alchemist Oct 19 '21 at 17:26
  • 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](https://stackoverflow.com/a/52749090/2157640). – Palec Dec 13 '22 at 13:03
374

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.
Lawrence
  • 4,909
  • 1
  • 15
  • 11
  • 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://stackoverflow.com/a/22885996/9849440 – Jeremiah England Feb 20 '22 at 17:34
67

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.
alex
  • 479,566
  • 201
  • 878
  • 984
Phoera
  • 3,046
  • 3
  • 14
  • 21
49
  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!!

Prerak Mann
  • 661
  • 7
  • 15
  • 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 Oct 28 '21 at 16:46
  • I.m.o it might just be better to append your stuff to the real .gitignore even if it does exist. – Prerak Mann Mar 15 '22 at 04: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 ?

LaGrandMere
  • 10,265
  • 1
  • 33
  • 41
  • I am trying to ignore globally, which is not happening!! – Mild Fuzz Sep 07 '11 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/*` – Aspiring Dev Apr 19 '20 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
Purkhalo Alex
  • 3,309
  • 29
  • 27
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
alex
  • 479,566
  • 201
  • 878
  • 984
mouse m.d.
  • 171
  • 1
  • 9
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.

Sri Sankaran
  • 8,120
  • 4
  • 38
  • 47
  • 5
    It doesn't matter what you call your global ignore file so long as it matches your `core.excludesfile` config. – CB Bailey Sep 08 '11 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`. – Sri Sankaran Sep 08 '11 at 13:56
  • I guess he thought the user would be smart enough to realise. – WORMSS Mar 22 '14 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 Nov 01 '20 at 04:02
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.

Nic
  • 13,287
  • 7
  • 40
  • 42
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.

bboydflo
  • 907
  • 1
  • 15
  • 24
-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.

Jose Paez
  • 747
  • 1
  • 11
  • 18
-2

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)

marcdomain
  • 29
  • 5