How to ignore Icon? in git

MacosGit

Macos Problem Overview


While trying to setup a dropbox folder with git, I saw a "Icon\r" file which is not created by me. I try to ignore it in the ~/.gitignore file. But adding Icon\r Icon\r\r Icon? won't work at all.

Macos Solutions


Solution 1 - Macos

You can use http://www.vim.org/">vim</a> as well.

  1. vim .gitignore
  2. in a new line write Icon, then
  3. press ctrl+v and then press Enter
  4. repeat step 3
  5. save and exit (shortcut: ZZ)

Now you should have Icon^M^M and it's done :)

For a smarter use you could add it to your gitignore global config file in ~/.gitignore_global.

Solution 2 - Macos

(This improves on the original answer, following a suggestion by robotspacer, according to hidn's explanation.)

The Icon? is the file of OS X folder icon. The "?" is a special character for double carriage return (\r\r).

To tell git to ignore it, open a terminal and navigate to your repository folder. Then type:

printf "Icon\r\r" >> .gitignore

If the file does not exist, it will be created and Icon\r\r will be its one line. If the file does exist, the line Icon\r\r will be appended to it.

Solution 3 - Macos

"Icon[\r]" is probably a better alternative.
In vim, you just put Icon[^M], which is Icon[ followed by CtrlV, Enter then ].

The problem with "Icon\r\r" is EOL conversion.

The whole line is actually "Icon\r\r\n", counting line ending. Based on your setup, CRLF may be converted to LF on commit, so your repo will actually have "Icon\r\n". Say you sync the changes to another repo. You will get "Icon\r\n" in that working directory, which ignores Icon but not Icon^M. If you further edit .gitignore and commit it there, you will end up with "Icon\n" - completely losing \r.

I encountered this in a project where some develop on OS X while some on Windows. By using brackets to separate \r and the line ending, I don't have to repeat \r twice and I don't worry about EOL conversion.

Solution 4 - Macos

The best place for this is in your global gitignore configuration file. You can create this file, access it, and then edit per the following steps:

>> git config --global core.excludesfile ~/.gitignore_global

>> vim ~/.gitignore_global

press i to enter insert mode

type Icon on a new line

while on the same line, ctrl + v, enter, ctrl + v, enter

press esc, then shift + ; then type wq then hit enter

Solution 5 - Macos

Regarding Naming (and Quoting) Things: First, more people would benefit by knowing that ANSI-C Quoting can be used to unambiguously match the macOS icon file. Both Icon$'\r' or $'Icon\r' and work in Bash and Zsh and most other modern shells, I hope, such as Fish.

Keep Your .gitignore Editable: While I'm impressed by the byte-level manipulation offered by other answers here, these methods are brittle in practice. Simply put, programmers tend to use text editors, and many of these editors are configured to alter line endings when saving a file. (For example, see this VS Code discussion about line ending normalization.)

Do you want your careful byte editing undone by your editor? Of course not. So perhaps you find it practical and convenient to configure your editor so that it doesn't affect line endings. You might look into (a) editor-specific configuration settings; or (b) cross-editor configuration (i.e. EditorConfig).

But this gets complex and messy. If want a simpler, more flexible way, use this in your .gitignore file:

# .gitignore
Icon?
![iI]con[_a-zA-Z0-9]

Explanation for the patterns:

  • Use Icon? because the gitignore format does not support \r as an escape code.
  • Use [iI] because Git can be case sensitive.
  • Use [_a-zA-Z0-9] to catch many common ASCII characters; you may want to broaden this.

You can test that your gitignore patterns are working as expected with:

git check-ignore -v *

For example, for testing, with these files in a directory:

-rw-r--r--@  Icon?
-rw-r--r--   icon8
drwxr-xr-x   icons
-rw-r--r--   iconography

... the result of git check-ignore -v * is:

/Users/abc/.gitignore:3:Icon?	"Icon\r"
/Users/abc/.gitignore:4:![iI]con[_a-zA-Z0-9]	icon_
/Users/abc/.gitignore:4:![iI]con[_a-zA-Z0-9]	icons

This is what you want.

Long Term Recommendation This problem would be trivial to fix if Git supported the \r escape in .gitconfig files. One could simply write:

# .gitignore
Icon[\r]

So I suggest we engage with the Git community and try to make this happen.

(If you do want to wade in and suggest a patch to Git, be sure to read first.)

References

From the gitignore documentation:

> Otherwise, Git treats the pattern as a shell glob: "*" matches anything except "/", "?" matches any one character except "/" and "[]" matches one character in a selected range. See fnmatch(3) and the FNM_PATHNAME flag for a more detailed description.

Please see This linuxize.com article for good examples of the square bracket syntax and negation syntax in .gitignore files.

For those that want to dig deep and see how pattern matching has changed over time in the Git source code, you can run this search for uses of fnmatch in the git repository on GitHub.

Solution 6 - Macos

The Icon? is the file of OSX folder icon. It turn out that \r is actually CRLF. So I use ruby to add the line to .gitignore file. Open terminal and navigate to home folder, then:

> irb
>> f = File.open(".gitignore", "a+") #<File:.gitignore>
>> f.write("Icon\r\r")  # output a integer
>> f.close
>> exit

Solution 7 - Macos

For me this worked in TextMate: Icon<CR><CR>. The <CR> is a carriage return character, which is at ctrl-alt-return on the keyboard. You can also find it in the standard Character Viewer app searching for cr. Please note that the <CR> is an invisible character, so it's only visible if the editor is set up to show them.

enter image description here

Solution 8 - Macos

I'm posting just an update answer because the one above didn't work for me but actually simply adding Icon? in my .gitignore worked. If you look at your name file on your Finder, it is actually how it is displayed.

Solution 9 - Macos

Icon[\r] did not work for me. I had to use the following in .gitignore...

Icon*

I also added Icon* to my Settings > Core > Ignored Names in Atom...

.git, .hg, .svn, .DS_Store, ._*, Thumbs.db, desktop.inis, Icon*

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontaiansuView Question on Stackoverflow
Solution 1 - MacosDharmaView Answer on Stackoverflow
Solution 2 - MacoseldesView Answer on Stackoverflow
Solution 3 - MacoshidnView Answer on Stackoverflow
Solution 4 - MacosrjatkinsonView Answer on Stackoverflow
Solution 5 - MacosDavid J.View Answer on Stackoverflow
Solution 6 - MacostaiansuView Answer on Stackoverflow
Solution 7 - MacosAAGDView Answer on Stackoverflow
Solution 8 - MacosAntoine KrajncView Answer on Stackoverflow
Solution 9 - Macosow3nView Answer on Stackoverflow