0

I'm looking at switching to git (from Perforce). I'm doing an initial add of files to my repository, but for some reason my .gitignore file isn't being used. When verifying the files being added via git status I can see files listed that should be ignored. I must be missing something very basic.

Here's a very simplified directory structure that I'm testing with:

d:/git/
    .git
    Test/
        test.suo
        bin/
            Debug/
                test.dll
    .gitignore

And here is the relevant part of my .gitignore file:

**/*.suo
**/[Bb]in/Debug

When adding the files I call:

git add Test

And then I verify the files that have been added:

git status

Which shows me:

new file:   Test/bin/Debug/test.dll
new file:   Test/test.suo

What am I missing? Why are these files being included?

Justin Helgerson
  • 24,900
  • 17
  • 97
  • 124
  • I think you can just write `*.suo` and `Debug/` in `.gitgnore`. +1 for your profile pic! – juan Oct 05 '17 at 19:11
  • @juan That doesn't make a difference. Those files are still added and `git status` shows the same output. – Justin Helgerson Oct 05 '17 at 19:59
  • Did you remove the files from index or tree? If the files still staged, try `git reset HEAD `. If you committed them, try `git rm --cached ` but be careful. [This answer](https://stackoverflow.com/questions/11451535/gitignore-is-not-working) might help. – juan Oct 05 '17 at 20:19
  • 1
    I haven't committed any data yet; the repository is empty. I've made sure to call `git rm --cached .` to remove all the files from stage. – Justin Helgerson Oct 05 '17 at 20:24
  • 2
    I don't *think* you're missing anything. I just created the same structure and git did what you're expecting it to do, that is, ignore the files. However, it also listed my `.gitignore` as an untracked file. This seems to suggest that git can't find/see your .gitignore file. Can you post the exact directory listing of your project folder? Can you reproduce this from scratch? Are any of the tips [here](https://stackoverflow.com/questions/11451535/gitignore-is-not-working) relevant? (Especially your editor if you're on Windows) – Matt Gibson Oct 05 '17 at 20:51
  • @MattGibson Thanks. Your suggestion that it can't see the file got me down the right path. I originally created the file via PowerShell by doing a `echo $null >> .gitignore` (since Windows is fussy about file names). I'm not sure why, but creating the file that way seemed the be the culprit. Creating the file differently (in Windows Explorer) solved the issue. – Justin Helgerson Oct 05 '17 at 23:52
  • 1
    @JustinHelgerson I've posted an answer with more details on what went wrong for you. Glad you solved your problem! – Matt Gibson Oct 06 '17 at 10:38

1 Answers1

2

Your use of PowerShell's echo $null >> .gitignore to create the file initially is the problem. This is because it creates not a completely empty file (no bytes at all), but a file with a UTF-16 little-endian Byte Order Mark ("BOM") at the beginning.

As noted in the comments to this answer this does not play nicely with tools like git, which generally expect BOM-less files, typically in UTF-8 encoding.

It seems like it's actually quite hacky to create an empty file like that with PowerShell.

Matt Gibson
  • 37,886
  • 9
  • 99
  • 128