Windows 全局 gitignore 不工作... "Untracked files"

Windows global gitignore not working... "Untracked files"

尽管 Windows OS 上有全局 gitignore,但我仍然收到一堆“未跟踪文件”。

下面是它看起来像的截断列表:

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        foo/bar/__pycache__/
        foo/pkg_name.egg-info/
        scripts/MainProcess.log
        scripts/__pycache__/
        test/foo/bar/__pycache__/
        test/test_pkg_name.egg-info/
        venv/

我做错了什么?我错过了什么?


背景

在 Windows 10 Pro 上,我的 git 版本是 git version 2.34.1.windows.1,这是我的 ~/.gitconfig:

[core]
    excludesfile = C:\Users\username\.config\git\ignore

git config --list --show-origin 输出:

file:C:/Users/username/.gitconfig       core.excludesfile=C:\Users\username\.config\git\ignore

git check-ignore **.* 什么都不输出。

这里是用于生成全局 gitignore 的 PowerShell 脚本:

New-Item -ItemType Directory -Force -Path $Env:USERPROFILE\.config\git

'Global/JetBrains','Global/Vim','Global/VisualStudioCode','Global/macOS','Python','Terraform' `
  | ForEach-Object {
    Invoke-RestMethod -Uri "https://raw.githubusercontent.com/github/gitignore/master/$PSItem.gitignore"
  } > $Env:USERPROFILE\.config\git\ignore

git config --global core.excludesfile $Env:USERPROFILE\.config\git\ignore

结果是一个以 LF 结尾的 UTF-16 LE 编码的 C:\Users\username\.config\git\ignore 文件。您可以在此处的 Pastebin 中查看完整文件:https://pastebin.com/a730JHtc

最后,签出的回购还有一个每个回购 .gitignore(UTF-8 编码,CRLF 行尾),如下所示:

# Follow README.rst instructions on global gitignore

它所说的 README.rst 指令已被翻译成这个问题。


研究

有很多类似的问题,none解决了我的问题:

类似的未回答问题:

您的问题是您的忽略文件是 UTF-16 格式的。虽然 UTF-16 在 Windows 上很常见,但在某些编程语言之外的其他地方几乎没有使用它,UTF-8 基本上取代了它。 Git 仅接受 UTF-8 或其他 ASCII-compatible 编码的忽略文件(如果您的模式包含 non-UTF-8 个字符),因此您需要将文件更改为使其工作的正确格式。

我也建议使用 LF 结尾,尽管我不认为这些是它工作所必需的。

为了它的价值,考虑到 ,我们可以将 PowerShell 全局 gitignore 创建命令更新为:

New-Item -ItemType Directory -Force -Path $Env:USERPROFILE\.config\git
'Global/JetBrains','Global/Vim','Global/VisualStudioCode','Global/macOS','Python','Terraform' `
  | ForEach-Object {
    Invoke-RestMethod -Uri "https://raw.githubusercontent.com/github/gitignore/master/$PSItem.gitignore"
  } `
  | Add-Content -Encoding UTF8 -Path $Env:USERPROFILE\.config\git\ignore

原来 git config --global core.excludesfile $Env:USERPROFILE\.config\git\ignore 上面的最后一行是不必要的。