Git ignores .gitignore when working with PowerShell

Today, a Git problem drove me nuts: I added files to the .gitignore file to make sure Git does not track and commit them to the repository. However, Git stubbornly ignored the file and kept adding all the files in my directory to the index.

As it turned out, PowerShell was the problem. I added the files in .gitignore with a simple echo test.txt > .gitignore. I got suspicious when I saw the file’s size and remembered that PowerShell uses UTF-16 to handle Strings. When editing the file in an external editor, Git recognized the content and ignored the files.

So, the solution for me was to use an external text editor for .gitignore or explicitly change the encoding when adding an entry via PowerShell using ac -path .gitignore -Value "test.txt" -Encoding ascii

Git ignores .gitignore in PowerShell due to UTF-16 encoding

Über Stefan

Polyglot Clean Code Developer

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax