32

In my git repository I've got a weird file in the staging area that's refusing to be reverted, removed, committed - basically I can't make it go away..

The file must be some ancient OS 9 file sitting there in the folder for years.

Couple days ago I've removed the file in the file system so now git is tracking the deletion of Iconr.

However, it's stuck there.

The error I'm getting via SourceTree (my git UI client) is

fatal: pathspec 'folder/Iconr' did not match any files

Any idea how to make git completely forget about that file?

kenorb
  • 155,785
  • 88
  • 678
  • 743
ATV
  • 4,116
  • 3
  • 23
  • 42
  • 3
    The filename is probably really called `Icon\r` - maybe the UI client doesn't display it properly. The trailing line break can trip various tools but should be dealt with by git when called from the command line – cacau Mar 24 '14 at 11:23
  • @cacau Indeed, on the command line it said 'Icon\r' – ATV Mar 24 '14 at 17:05
  • See http://stackoverflow.com/questions/17556250/how-to-ignore-icon-in-git – cseelus May 15 '15 at 14:08

6 Answers6

33

Adding a line

Icon?

to .gitignore worked for me.

https://answers.atlassian.com/questions/167170/removing-invisible-icon-files-on-a-local-repository-on-a-mac

Tak
  • 441
  • 4
  • 4
  • 4
    amplification: adding 'Icon?' to .gitignore causes 'Icon\r' to be ignored. Since ? is a single character wild card, then names like Iconx, Icon1 will also be ignored. However, 'Icon?' is simple to apply using any text editor ... and is sufficient for many use cases. – marc-medley Dec 10 '14 at 17:59
  • 1
    Add "Icon?" into .gitignore worked for me. "Icon" or "Iconr" or "Icon\r" did not. – Herr_Hansen Jul 05 '15 at 21:09
  • This is a better answer than the accepted answer. Icon\r and several other variants are files OSX stuffs into directories to represent a custom folder icon. There are a number of similar variations, all of which have one extra character, but this answer covers all of them. – mopsyd Dec 06 '17 at 21:16
10

It is best to revert to the command line in order to have a more precise error message.

There you can try a git add -u, followed by a git commit, in order to register the deletion of that file in the repo.
See "git status says file is deleted but git rm says error: pathspec '…' did not match any files" as an example.

You can also preview what a git clean would give: git clean -d -x -n (as explained in "Why is there no “pathspec” when I try to remove a folder in Git?")

The other issue is when that file isn't tracked at all in your current branch, but is tracked in another branch. See that answer to check if that is the case.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
7

The Icon^M-like files most likely are created by some external apps such as Folderol which are highlighting your folders.

You should either remove the app which is doing that, or ignore it in your .gitignore file, e.g.

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

This is a shell command. Or add it as Icon?? or you can reproduce ^M special character by Control-V and hitting Enter.

Related: How to ignore Icon? in git

If you don't want to commit this change into your repository, alternatively add it into your global .gitignore or add it into .git/info/exclude.

See: Using Git / Ignoring files.

To remove the files it-self, try:

find . -type f -name 'Icon?' -print -delete

Or if they're the only one which are unstaged, run: git clean -f.

Community
  • 1
  • 1
kenorb
  • 155,785
  • 88
  • 678
  • 743
3

As a general workaround the .gitignore file can be edited to completely ignore the Icon\r files as described in this blog entry: Git Tip: Ignoring Icon\r in .gitignore

cacau
  • 3,606
  • 3
  • 21
  • 42
3

What shows as "Iconr" in SourceTree is really Icon\r where \r is the hex value 0x0d.

The .gitignore filename pattern matching is limited when compared to POSIX regex. Regular expression patterns such as Icon\r and Icon\x0d did not work for me with SourceTree.

Simple Ignore: Adding Icon? pattern in the .gitignore file will work to have "Iconr" files ignored in SourceTree.

Caveat: Icon? uses ? which is a single character wildcard. So, files and folders like Icon\r, "icona", "icon3" and "Icons" will be ignored with an Icon? pattern. If needed, use file names "Icon01", "icon02", "iconaa" instead of "Icon1", "icon2", "icona" with this workaround to add such files to git.

Precise Ignore: The precise Icon\r filename can be ignored with a binhex editor.

In binhex editor, add 0a 49 63 6f 6e 0d 0d 0a in the .gitignore file. The extra 0as are linefeeds to bracket the Icon\r\r for later editing of an otherwise unix file.

Caveat: If using the precise value 49 63 6f 6e 0d, be careful to not replace \r with \n. In other words, do not edit .gitignore in a text editor set to auto-replace line separators with this approach.

Note: The Simple Ignore Icon? is convenient and sufficient for most use cases. Quit and restart SourceTree to see the changes take effect. macOS's git is case-insensitive by default.

Custom Folder/File Icons: An Icon\r file is created each time a custom icon is added to a folder or file. For example, the custom icon can be added via the "Get Info" window. Here is an example where the default system folder icon has been replaced with a custom folder icon:

"Get Info" window

The Icon\r file(s) can be removed manually via the file's/folder's "Get Info" window or by invoking a shell command to remove the Icon\r files, as noted in kenorb's answer.

marc-medley
  • 8,931
  • 5
  • 60
  • 66
3

You can ignore the Icon? on any level of subfolders using wildcards:

// .gitignore
**/Icon?

Wildcards works since version 1.8.2 of git

Nicolai Lissau
  • 7,298
  • 5
  • 43
  • 57