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.
11 Answers
You can use vim as well.
vim .gitignore
- in a new line write
Icon
, then - press ctrl+v and then press Enter
- repeat step 3
- 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
.
-
2I'm not sure if you need step 4?
$ find . -name "Icon^M^M"
gives nothing, but$ find . -name "Icon^M"
gives./Icon
. Is there something different about gitignore?– A.WanSep 29, 2015 at 23:48 -
1
-
Thanks! Do you know why we need two
^M
when it shows up in thels
results as justIcon?
and a quickgit status
shows"Icon\r"
? Weird. Dec 13, 2017 at 19:03 -
1You can (and should) specify the filename precisely with ANSI-C Quoting; e.g.
find . -name $'Icon\r'
– David J.Dec 23, 2020 at 16:45 -
1This answer says what keys to press -- but not what these keypresses do -- or why they are necessary.– David J.Aug 27, 2021 at 3:13
(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.
-
8
-
4Combining the two suggestions, you can use this to add the safer version of the rule to an existing file:
printf "Icon[\r]" >> .gitignore
May 5, 2019 at 20:43 -
I suggest adding more context around how many text editors (depending on their settings) may alter line endings. They do this with good intentions; e.g. in the spirit of standardizing line endings, but in this case, it makes editing a .gitignore file more difficult.– David J.Dec 23, 2020 at 16:47
-
Due to practical considerations (we tend to use text editors, and these editors often 'correct' line endings), this approach is brittle. I suggested an alternative answer at stackoverflow.com/a/65429032/109618– David J.Dec 23, 2020 at 18:16
-
The original answer that had been at the top of the post (with
>
instead of>>
) clobbers the.gitignore
file if it does exist. Given that there's no reason to use it (>>
works in both situations), I simply deleted it. This protects users who hastily look for the first block of code in the accepted answer and copy it blindly. raises hand sheepishly Dec 3, 2021 at 14:21
"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.
-
-
Wasn't able to get this to work unfortunately, but @rjatkinson's answer did work. (Not downvoting though as it may have just been me.) May 11, 2017 at 14:12
-
2
-
Add following line to .gitattributes file to properly view the diff in terminal: /.gitignore text diff Jan 9, 2019 at 9:57
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
-
This answer says what keys to press -- but not what they do -- or why they are necessary.– David J.Aug 27, 2021 at 3:11
-
1+1 Putting this in a global gitignore is the "one and only one truly obvious way to do it."
printf "Icon\r\r" >> ~/.gitignore_global
on the command line (as in eldes's answer) will add the line without using vim. Dec 3, 2021 at 14:26
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.
-
3This answer should be at the top, and the accepted answer should be changed to this. It is truly the best if you edit gitignore files both from the command line or using a text editor (or only the latter).– cristianOct 18, 2021 at 12:12
-
1
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
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.
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.
-
Note that
Icon?
will ignore things like "Icons" as well. For most of the answers to work your line ending should beLF
, but on macs it'sCR
, AFAIK. And it also depends on your git's line ending conversion setting. It's displayed like that because many apps use "?" as a placeholder for any non-printable character. Mar 12, 2021 at 11:38
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*
Add Icon?
to your .gitignore file and save it. It should do the job.
Icon?
-
1Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.– Community BotJul 3, 2022 at 17:12
To avoid wasting time on such trivial issues, I recommend using gibo.
gibo dump macOS >> .gitignore
The result:
### Generated by gibo (https://github.com/simonwhitaker/gibo)
### https://raw.github.com/github/gitignore/e5323759e387ba347a9d50f8b0ddd16502eb71d4/Global/macOS.gitignore
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
Icon?
works for me.!icons
to my .gitignore file so directories and files will be allowed to use that name without getting ignored. Folder icons still get ignored as desired.