Git 全局忽略不起作用

我已经创建了文件 .gitignore_global,并把它放在我的 git 安装目录中:

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

忽略文件中的规则不会应用到我的提交中。

当我重命名文件 .gitignore并将其放在项目根目录中时,规则确实适用。

怎么了?

51769 次浏览

Maybe you need to do:

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

And to have some coffee.

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

On linux it is: /home/[user]

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.

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)

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

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.

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.

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.

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.

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

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.

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.

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