113

I've created the file .gitignore_global and put it in my git install directory. When I run the command:

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

the rules in the ignore file do not get applied to my commits.

When I rename the file .gitignore and put it in my project root, the rules do apply.

What is wrong here?

Alfe
  • 56,346
  • 20
  • 107
  • 159
user880954
  • 7,317
  • 6
  • 22
  • 20
  • 1
    @Jarrod ` ~/.gitignore_global` was what the OP meant and just a typo in the post, although he accepted my answer, so your edit isn't good – CharlesB May 24 '12 at 20:24
  • Does this answer your question? [Global Git ignore](https://stackoverflow.com/questions/7335420/global-git-ignore) – codeforester Jan 06 '21 at 09:24
  • 3
    I landed upon question because it was not working for me too. My problem was writing `core.excludesfile` instead of `core.excludesFile` with a small F. Hope this helps anyone new who runs into a similar situation – Bibek Aryal Jan 13 '21 at 03:47
  • see video here https://youtu.be/3LYBdd3RGKs – Shailesh Ladumor Jan 31 '21 at 07:16

15 Answers15

216

Maybe you need to do:

git config --global core.excludesFile ~/.gitignore_global 

And to have some coffee.

Community
  • 1
  • 1
CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • Sorry that's what I meant. The one I copied was just messing about to see what worked. – user880954 Sep 23 '11 at 13:06
  • True for coffee and teeth. Are you sure .gitignore_global is in your $HOME? you say you installed it in git install directory? – CharlesB Sep 23 '11 at 13:44
  • any idea why it still not work here? I got a .7z file that I want to skip; I added these rules: https://gist.github.com/octocat/9257657; and after I change the file, it still gets versioned on .git :( – Aquarius Power May 02 '14 at 23:21
  • My .7z file was already added to .git repo; I had to `git rm` (had to backup before it), so the file begins being ignored! – Aquarius Power May 02 '14 at 23:47
  • 5
    Yes, `gitignore` file only works for untracked files, not for those which got added before – CharlesB May 03 '14 at 18:41
  • I need some coffee too :D – jeremyjjbrown Sep 09 '16 at 17:15
  • Try to just execute `git config --global core.excludesfile` and see which file got printed out, then go and edit that file, this way worked out for me. I guess the second file is the one you want to append for ignoring. – Wei Lin Mar 31 '17 at 18:56
  • 1
    Another thing i just noticed was the pattern of the line in the .gitignore_global file must match the path from the main directory of the project EXACTLY. For example, if the project name is myProject, which generates SQL code in myProject/sql/init, and you want to ignore myProject/sql/init/*.sql the entry must be */sql/init/. Similarly, for generated DAO/DTO code in myProject/msg/dao/ and myProject/dao, there must be two entries to catch ALL files in dao subdirectories - */*/dao/ and */dao/ must be added. – Keith Kibler Jul 12 '17 at 17:26
  • Coffee did the trick and helped me realize I had misspelled my path, as well. – Kyle Krull Dec 06 '18 at 17:39
118

Thought I would chip in on this. There is another reason why the global ignore file appears not to be working. It's something that I don't think has been covered in earlier answers. It's so blindingly obvious that - of course - it's very easy to miss.

It is, that git will only ignore new files. If the file is already being tracked by git, then of course git will not ignore it! So whatever patterns in any gitignore or exclude file do not apply.

That makes sense. Why would git want to ignore modifications to files it is already tracking? If the file is to be ignored, you must first tell git not to track it, then git ignore it as described in the manual. For info on how to untrack files, see this answer.

This all leads me to ask, is it possible to ignore changes to tracked files? Again, git delivers. This answer; Git: Ignore tracked files gives us the command (replace file with the file you want to ignore):

git update-index --assume-unchanged file

Finally, here's some additional info on debugging git ignore.

The gitignore(5) Manual Page tells us:

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.

So, this is new and replaces the previous ~/.gitignore_global mentioned previously.

Next, and this is really useful, is that as of 1.8.2, we now have some excellent debugging tools. Have a look at:

Git Learns to Ignore Better - New in 1.8.2

This shows how to use the new check-ignore flag to verify that git is successfully ignoring your patterns, e.g.

git check-ignore bin/a.dll --verbose

Josiah
  • 60
  • 6
Max MacLeod
  • 26,115
  • 13
  • 104
  • 132
  • I did `git check-ignore .` and it said unable to access ''C:\Users\...\.gitignore''. That is when I realized I had to remove the single inverted commas. I removed them by editing ~\.gitconfig – Mithil Poojary Sep 02 '21 at 14:14
  • Do note that even though this works, there's a huge problem with this. Say you modified your file as needed and hold them with this command, then you `git stash` your changes for all files, and the files on hold will get stashed. When you do `git stash pop`, these files won't get recovered. So you effectively lost your changes and you have to modify those files again. – 15 Volts Dec 16 '22 at 13:53
  • 1
    Great, thanks, "update-index" fixed it for me. :) Actually to me it was not, and still is not, an "obvious" thing at all. – Ellis Jul 19 '23 at 11:51
18

I found that when I had defined the global core.excludesfile like this:

git config --global core.excludesfile $HOME/.config/git/ignore

it didn't work. Changing it to not use the $HOME variable, like this:

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

it immediately started working. Hope this helps someone else.

FYI:

$ git --version
git version 1.7.10.2 (Apple Git-33)
caffreyd
  • 1,151
  • 1
  • 17
  • 25
XP84
  • 1,236
  • 2
  • 11
  • 15
  • I observed this too. I skimmed the documentation but didn't find anything: In addition to .gitignore (per-directory) and .git/info/exclude, git looks into this file for patterns of files which are not meant to be tracked. "~/" is expanded to the value of $HOME and "~user/" to the specified user’s home directory. – rliu Apr 05 '14 at 22:02
  • This is the fix – Erez Jun 02 '22 at 09:01
11

Note: starting git1.7.12 (August 2012):

The value of:

  • core.attributesfile defaults to $HOME/.config/git/attributes and
  • core.excludesfile defaults to $HOME/.config/git/ignore respectively when these files exist.

So if you create a $HOME/.config/git/attributes file, you don't even have to set your core.excludesfile setting.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
4

I had this problem because I had initially called 'git config core.excludefiles' without --global and with a wrong value, and hence it had stored the property locally. Looks like the local property hid the global one (which had the correct value), completely ignoring it.

Daniel Pinyol
  • 2,094
  • 2
  • 15
  • 15
  • This was my issue as well. Took a bit to find – yarian Jun 01 '22 at 17:52
  • Me too. When debugging I did `git config core.excludesfile .gitignore` inside a repository. Had to go inside `.git/config` and remove the [core]/excludesfile line to make it work again. – Yuri Ghensev Jun 14 '22 at 13:22
4

The file must be in your home directory. On windows, it usually means: C:\documents and settings\[user].

On linux it is: /home/[user]

3

Also I had a funny thing with encoding of this file. Somehow my Windows 10 (with Powershell) created the file with the encoding "UTF-16 LE" and somehow Git couldn't handle this. When I change the encoding to a much more sensible value of literally anything else it worked.

Stelzi79
  • 585
  • 3
  • 12
3

Adding another thing ppl might have missed, or at least where I went wrong.

I made the mistake of not copy-pasting the command and I spelled
excludefile (wrong)
instead of
excludesfile (right)
and lost definitely half an hour or so before I reevaluated the spelling, thinking it was something else.

You don't get a warning or error for setting an option that is not a valid option. You do get a warning/error message if the syntax is invalid, which is why I wrongly assumed that a spelling mistake would be caught the same way.

Intrastellar Explorer
  • 3,005
  • 9
  • 52
  • 119
Emile Vrijdags
  • 1,550
  • 2
  • 15
  • 24
2

Another reason a global git ignore may not be working: invisible characters in the path.

I only found out this was my problem when pasting the warning message from git-check-ignore into the address bar of Chrome, where it luckily became visible:

enter image description here

(It wasn't visible on the terminal. And this could have been a long rabbit hole of debugging...)

1j01
  • 3,714
  • 2
  • 29
  • 30
1

I was having the same problem (on Windows). I ended up checking my ~/.gitconfig file and found that my excludesfile was set to:

excludesfile = C:\\Users\\myUserName\\Documents\\gitignore_global.txt

but it should have been:

excludesfile = C:\\Users\\myUserName\\.gitignore_global

After I changed it, everything worked as it should.

Bob
  • 181
  • 1
  • 6
0

Another hard to track reason why .gitignore(_global) appears not working could be leading spaces in the lines. The file is not forgiving for leading spaces. So make sure each line of your file doesn't have any unless it's blank line. It took me a while to figure out.

Just adding my two cents.

luanjunyi
  • 1,634
  • 1
  • 15
  • 17
0

I recently made a mistake and run the following command git config core.excludesfile tags this changed the excludesfile path to a local file in the current repository, and this config has been written to the local config file under .git/config, so to fix that I just opened the .git/config file and deleted the line excludesfile tags and everything is back to normal.

Nafaa Boutefer
  • 2,169
  • 19
  • 26
0

A problem I was running into is that I tried putting INLINE comments in my global git ignore.

For example this does NOT work:

.DS_Store   # from macOS Finder

This DOES work:

# from macOS Finder
.DS_Store
0

I had this problem when using Cygwin with Windows Git. I had forgotten to include Git from Cygwin. When I added Git to my Cygwin software base, the problem went away.

Lyle Z
  • 1,269
  • 10
  • 7
0

after adding directory / filename to .gitignore, you should delete those files and/or directories and recreate them.

This worked for me. Hope works for you as well.

Burakhan Aksoy
  • 319
  • 3
  • 14