11

I have a repo that includes SVG images in an icons/ directory. Attempting to add these images to the repo fails, and Git complains with the error message:

The following paths are ignored by one of your .gitignore files:
public/img/icons/my-icon.svg
Use -f if you really want to add them.

When I traced the ignored files using git-check-ignore, I found that the Icon? rule from my .gitignore_global file was the culprit.

$ git check-ignore -v public/img/icons/my-icon.svg 
/Users/ryanatallah/.gitignore_global:42:Icon?   public/img/icons/my-icon.svg

What might be an elegant solution to this problem?

Ryan Atallah
  • 2,977
  • 26
  • 34
  • Why don't you simply remove the line `Icon?` from .gitignore_global or modify it to only match what you want to ignore? – Njol Jan 14 '14 at 09:20
  • Because I still want Git to ignore `Icon?` files. – Ryan Atallah Jan 14 '14 at 09:27
  • When you say you want to ignore **icon** files, is there another pattern that would work instead of `Icon` ? For example - `*.ico` – TheCodeArtist Jan 14 '14 at 09:30
  • Please edit your question and tell us which files you want ignored. Only files called Icon1, Icon2...? Everything that starts with Icon? – sleske Jan 14 '14 at 09:32
  • I'm not sure I understand why that line is the culprit. It should be matching `my-icon.svg` against `Icon?`... and it shouldn't match. That seems odd to me. Perhaps check-ignore is not functioning correctly. Is there a `*.svg` rule in your `.gitignore_global`? – John Szakmeister Jan 14 '14 at 09:33

2 Answers2

19

Turns out this problem is caused by a Git bug where the Icon? gitignore rule also matches directories such as icons/. The solution is to write the rule with the correct control character (which is a carriage return) at the end. This question explains that Icon? files are automatically created for directories with custom icons.

The solution, as documented in this blogpost is to correctly write the rule to my global gitignore file with the carriage return. The following Ruby script does the trick:

>> f = File.open(".gitignore", "a+") # append
=> #<File:.gitignore>
>> f.write("Icon\r\r")
=> 8
>> f.close
=> nil 
Arnaud Valle
  • 1,673
  • 1
  • 16
  • 25
Ryan Atallah
  • 2,977
  • 26
  • 34
  • Thanks for this! My colleague and I were pulling our hair out for half an hour on this one. – Scott Cranfill Apr 09 '14 at 22:11
  • OS X: I tried adding another line to `.gitignore` after using this technique, but it got mixed with `Icon` on the same line. I bypassed the problem by deleting the `Icon` entry and adding it again, only using `f.write("Icon\r\r\n")` this time. After that, I was able to add another entry (`echo "file_to_ignore" >> .gitignore`). – Arnaud Renaud Apr 13 '15 at 10:26
  • `f.write("Icon[\r]\n")` seems preferable. See [here](https://stackoverflow.com/a/33974028/192740) for why. It also prevents Arnaud's problem. – cstork Nov 04 '17 at 23:37
3

To override rules from the parent .gitignore file you need to use ! prefix, for example add in local .gitignore file:

!Icon
Wojciech Bednarski
  • 6,033
  • 9
  • 49
  • 73