Git 全局忽略不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7529266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:54:11  来源:igfitidea点击:

Git global ignore not working

git

提问by user880954

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

我已经创建了该文件.gitignore_global并将其放在我的 git install 目录中。当我运行命令时:

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

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

忽略文件中的规则不适用于我的提交。

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

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

What is wrong here?

这里有什么问题?

回答by CharlesB

Maybe you need to do:

也许你需要这样做:

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

And to have some coffee.

还有喝咖啡。

回答by Max MacLeod

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 gitignoreor excludefile do not apply.

也就是说,git只会忽略新文件。如果文件已经被 git 跟踪了,那么当然 git 不会忽略它!所以任何gitignoreexclude文件中的任何模式都不适用

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.

那讲得通。为什么 git 要忽略对它已经跟踪的文件的修改?如果要忽略该文件,则必须先告诉 git 不要跟踪它,然后按照手册中的说明 git ignore 它。有关如何取消跟踪文件的信息,请参阅此答案

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

这一切都让我问,是否可以忽略对跟踪文件的更改?再次,git 提供。这个答案; Git: Ignore tracked files为我们提供了命令(替换file为您要忽略的文件):

git update-index --assume-unchanged file

git update-index --assume-unchanged file

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

最后,这里有一些关于调试 git ignore 的附加信息。

The gitignore(5) Manual Pagetells us:

gitignore(5)手册告诉我们:

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.

用户希望 Git 在所有情况下忽略的模式(例如,由用户选择的编辑器生成的备份或临时文件)通常会进入用户的 ~/.gitconfig 中由 core.excludesfile 指定的文件。它的默认值是 $XDG_CONFIG_HOME/git/ignore。如果 $XDG_CONFIG_HOME 未设置或为空,则使用 $HOME/.config/git/ignore 代替。

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

所以,这是新的,取代了前面~/.gitignore_global提到的。

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

接下来,这真的很有用,从 1.8.2 开始,我们现在有了一些出色的调试工具。看一下:

Git Learns to Ignore Better - New in 1.8.2

Git 学会更好地忽略 - 1.8.2 中的新功能

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

这显示了如何使用新check-ignore标志来验证 git 是否成功忽略了您的模式,例如

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

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

回答by XP84

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

我发现当我这样定义时global core.excludesfile

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

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

它没有用。将其更改为不使用该$HOME变量,如下所示:

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)

回答by VonC

Note: starting git1.7.12 (August 2012):

注意:从git1.7.12(2012 年 8 月)开始

The value of:

  • core.attributesfiledefaults to $HOME/.config/git/attributes and
  • core.excludesfiledefaults to $HOME/.config/git/ignorerespectively when these files exist.

的价值:

  • core.attributesfile默认为 $HOME/.config/git/attributes 和
  • core.excludesfile$HOME/.config/git/ignore当这些文件存在时分别默认为 。

So if you create a $HOME/.config/git/attributesfile, you don't even have to set your core.excludesfilesetting.

因此,如果您创建一个$HOME/.config/git/attributes文件,您甚至不必设置您的core.excludesfile设置。

回答by Leandro Casanova

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

该文件必须在您的主目录中。在 Windows 上,它通常表示:C:\documents and settings\[user]。

On linux it is: /home/[user]

在 linux 上是:/home/[user]

回答by Daniel Pinyol

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.

我遇到了这个问题,因为我最初在没有 --global 的情况下调用了 'git config core.excludefiles' 并且使用了错误的值,因此它在本地存储了该属性。看起来本地属性隐藏了全局属性(具有正确的值),完全忽略了它。

回答by Bob

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

我遇到了同样的问题(在 Windows 上)。我最终检查了我的~/.gitconfig文件,发现我的excludesfile设置为:

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.

在我更改它之后,一切正常。

回答by 1j01

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

全局 git ignore 可能不起作用的另一个原因:路径中的不可见字符。

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

我只是在将git-check-ignore 中的警告消息粘贴到 Chrome 的地址栏中时才发现这是我的问题,幸运的是它变得可见:

enter image description here

在此处输入图片说明

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

(它在终端上不可见。这可能是一个漫长的调试兔子洞......)

回答by Wolfgang Stelzhammer

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.

此外,我对这个文件的编码有一件有趣的事情。不知何故,我的 Windows 10(使用 Powershell)创建了编码为“UTF-16 LE”的文件,但 Git 无法处理这个问题。当我将编码更改为更合理的值时,它可以正常工作。

回答by Nafaa Boutefer

I recently made a mistake and run the following command git config core.excludesfile tagsthis 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/configfile and deleted the line excludesfile tagsand everything is back to normal.

我最近犯了一个错误并运行了以下命令, git config core.excludesfile tags这将 excludesfile 路径更改为当前存储库中的本地文件,并且此配置已写入本地配置文件中.git/config,因此为了修复我刚刚打开.git/config文件并删除了该行excludesfile tags一切都恢复正常。