5

For non-ASCII characters in file names, Git will output them in octal notation. For example:

> git ls-files
"\337.txt"

If such a byte sequence does not represent a legal encoding (for the command line's current encoding), I'm not able to enter the corresponding String on command line. How can I still invoke Git commands on these files? Obviously, using the String which is displayed by git ls-files does not work:

> git rm "\337.txt"
fatal: pathspec '337.txt' did not match any files

Tested on Windows, with msysgit 1.7.10 (git version 1.7.10.msysgit.1)

mstrap
  • 16,808
  • 10
  • 56
  • 86
  • See [How to make Git properly display UTF-8 encoded pathnames in the console window?](https://stackoverflow.com/questions/22827239/how-to-make-git-properly-display-utf-8-encoded-pathnames-in-the-console-window). `git config --global core.quotepath off` – Hans Ginzel Dec 01 '17 at 15:59

1 Answers1

10

In Bash, you can use printf for this kind of purpose:

$ printf "\337.txt"
▒.txt

$ git rm `printf "\337.txt"`  # this would pass the awkward filename to git

The problem is, obviously, that the shell doesn't perform octal escaping, neither does git. But printf does.


Also, echo -e can do octal escaping:

$ echo -e '\0337.txt'
▒.txt

But that usage is a bit discouraged, you should prefer printf where you can.

MestreLion
  • 12,698
  • 8
  • 66
  • 57
ulidtko
  • 14,740
  • 10
  • 56
  • 88
  • How would I use that in combination with Git commands? What if I have msysgit installed? – mstrap May 21 '12 at 15:01
  • @mstrap I hope that msys provides a `printf` utility, because it's a standard posix one. You should try if it's available to you. Also see edit. – ulidtko May 21 '12 at 15:05
  • 1
    The printf-version works perfectly from within msysgit's bash. Thanks! – mstrap May 21 '12 at 15:53
  • eg: ` git ls-tree --name-only $GIT_COMMIT | xargs -0 printf | xargs -I files mv files $d ` – qxo Apr 08 '17 at 14:03