Search code examples
windowsgitgitignore

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


Despite having a global gitignore on Windows OS, I am still getting a bunch of "Untracked files".

Below is a truncated list of what it looks like:

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/

What am I doing wrong? What am I missing?


Background

On Windows 10 Pro my git version is git version 2.34.1.windows.1, and here's my ~/.gitconfig:

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

git config --list --show-origin outputs:

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

git check-ignore **.* outputs nothing.

Here is the PowerShell script used to generate the global 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"
  } > $Env:USERPROFILE\.config\git\ignore

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

The result is a C:\Users\username\.config\git\ignore file in UTF-16 LE encoding with LF endings. You can see the full file in a Pastebin here: https://pastebin.com/a730JHtc

Lastly, the checked out repo also has a per-repo .gitignore (UTF-8 encoding, CRLF line endings) inside that looks like this:

# Follow README.rst instructions on global gitignore

The README.rst instructions it speaks of have been translated into this question.


Research

There are many similar questions like this, none of them solved my problem:

Similar unanswered questions:


Solution

  • Your problem is that your ignore file is in UTF-16. While UTF-16 is common on Windows, it is practically unused elsewhere outside of some programming languages, and UTF-8 has mostly supplanted it. Git accepts ignore files only in UTF-8 or other ASCII-compatible encodings (in the event your patterns contain non-UTF-8 characters), so you'll need to change your file to be in the proper format for it to work.

    I would also recommend using LF endings, although I don't believe those are absolutely required for it to work.