Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git checkout/remove file with special characters

I had a file with special characters in the file name (umlauts). I since have renamed the file, but git still tells me that I have 'Changes not staged for commit'. However, I can't remember the correct name of the file, and git only tells me the name is "Mu\314\210.jpg". I have no clue what format that is suppose to be. utf8? I tried git rm and copied the string, but didn't work.

How can I find out the original name of the file or how can I remove the file without knowing the exact file name (maybe with only the file extension)?

EDIT: I start to have the feeling that the file was created on a Windows PC and the encoding of the umlauts are different there. However, I can't explain why I am not able to remove the file.

I just found out that those are octal representations. Trying out the solutions from Can't remove directories git, Remove a file with a strange name from git, and Git: how to specify file names containing octal notation on the command line, however, didn't help. Especially the solution

git rm `printf "Mu\314\210.jpg"`

didn't delete the file. It still gives me "Mu\314\210.jpg" didn't match any files. Is there some other way to remove a file?

printf told me that "Mu\314\210.jpg" is "Mü.jpg". However, the ü octal representation on my Mac OSX should be \303\274. That's why I suppose that the u\314\210 comes from a Windows PC. Still not sure how that helps though.

I just tried to run what the git-rm suggests git diff --name-only --diff-filter=D -z | xargs -0 git rm --cached. I am quiet positive now that the file can't be removed until it has been added again. I will try to get the file with the old file name and remove it then.

Ok, adding the old file didn't help either. Mac OSX seems to automatically change the octal representation. Seems like the last chance is it remove it on a Windows PC.

like image 288
user3607973 Avatar asked May 22 '14 13:05

user3607973


People also ask

How to remove specific file from git?

The git rm command can be used to remove individual files or a collection of files. The primary function of git rm is to remove tracked files from the Git index. Additionally, git rm can be used to remove files from both the staging index and the working directory.

Does git rm actually delete the file?

By default, the git rm command deletes files both from the Git repository as well as the filesystem. Using the --cached flag, the actual file on disk will not be deleted.

What is rm command in git?

In Git, the term rm stands for remove. It is used to remove individual files or a collection of files. The key function of git rm is to remove tracked files from the Git index. Additionally, it can be used to remove files from both the working directory and staging index.


1 Answers

Using git rm you can do :

git rm Mu*.jpg

This will remove from the index all files Starting with Mu and ending with .jpg

If you have other files in the repo that match the pattern they will be removed to. In that case, you can use :

git add -u Mu*.jpg

from the man page of git add :

-u --update

Only match against already tracked files in the index rather than the working tree. That means that it will never stage new files, but that it will stage modified new contents of tracked files and that it will remove files from the index if the corresponding files in the working tree have been removed.

(This will also stage any other change in the repository done in files matching the pattern, you can always unstage them after if you want using git reset)

like image 130
Fredszaq Avatar answered Oct 14 '22 19:10

Fredszaq