5

对于文件名中的非 ASCII 字符,Git 会以八进制输出。例如:

> git ls-files
"\337.txt"

如果这样的字节序列不代表合法的编码(对于命令行的当前编码),我无法在命令行上输入相应的字符串。我怎样才能在这些文件上调用 Git 命令?显然,使用显示的字符串git ls-files不起作用:

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

在 Windows 上测试,使用 msysgit 1.7.10(git 版本 1.7.10.msysgit.1)

4

1 回答 1

9

在 Bash 中,您可以printf用于这种目的:

$ printf "\337.txt"
▒.txt

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

显然,问题在于 shell 不执行八进制转义,git 也不执行。但printf确实如此。


此外,echo -e可以进行八进制转义:

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

但是这种用法有点不鼓励,您应该尽可能选择printf

于 2012-05-21T14:56:20.923 回答