全局.gitignore无效

问题描述

即使配置看起来不错,我也无法让git使用我的.gitignore_global文件

PS C:\path\to\repository> git config --global core.excludesfile
C:/Users/UserXYZ/.gitignore_global
PS C:\path\to\repository> git config core.excludesfile
C:/Users/UserXYZ/.gitignore_global
PS C:\path\to\repository> type C:/Users/UserXYZ/.gitignore_global
# ignore settings for VSCode
.vscode/

因此git应该忽略名称.vscode的所有目录及其中的文件。当我在存储库中的.gitignore中添加.vscode/时,此方法非常有效。但是,它在全局文件中无效:

PS C:\path\to\repository> git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   generic/somefile.cpp

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .vscode/
        generic/.vscode/
        simulator/.vscode/

no changes added to commit (use "git add" and/or "git commit -a")

如果我将该行添加到存储库中的.gitignore,则未跟踪的文件会消失。 我也在使用sourcetree,但我认为这无关紧要,因为在命令行中的行为是完全相同的。

感谢您的帮助!

编辑:引号与否,斜杠或反斜杠(单或双),这似乎没有任何区别。我的.gitconfig看起来像这样,它也不起作用:

[user]
    name = Full Name
    email = [email protected]
[difftool "sourcetree"]
    cmd = 'C:/Program Files/Perforce/p4merge.exe' \"$LOCAL\" \"$REMOTE\"
[mergetool "sourcetree"]
    cmd = 'C:/Program Files/Perforce/p4merge.exe' \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"
    trustExitCode = true
[core]
    excludesFile = "C:\\Users\\UserXYZ\\.gitignore_global"

我的git版本是2.28.0.windows.1

更新: 好吧,这是一个棘手的问题。原来,我的常规.gitignore中有一个部分使全局文件不起作用(即使已正确配置):

#binary ignore pattern
*
!/**/
!*.*

如果我正确地理解了这一点,那么首先,所有内容都将被忽略,然后所有目录和所有带有扩展名的文件都将被带回(包括我想在global_gitignore中输入的内容)。所以现在的问题是一个稍微不同的问题:是否可以从全局.gitignore中覆盖此问题?我无法在项目级别更改.gitignore。

解决方法

Git for Windows希望使用反斜杠指定路径:

git config --global core.excludesFile "C:\\Users\\UserXYZ\\.gitignore_global"