对具有非 ASCII 字符的文件名使用 `git diff --name-only` 的输出

Using output of `git diff --name-only` for file names with non-ASCII characters

git diff --name-only 的输出对于非 ASCII 的文件名不是很有用。示例:

git init
echo Germany > Düsseldorf.txt
echo Mexico > Cancún.txt
git add *.txt

while read f
    do cat "$f"
done < <(git diff --cached --name-only)

这导致以下输出:

cat: '"Canc303272n.txt"': No such file or directory
cat: '"D303274sseldorf.txt"': No such file or directory

如何获取对进一步处理有用的暂存文件列表?

How can I obtain a listing of the staged files that's useful for further processing?

使用零分隔流。

git diff -z --cached --name-only |
while IFS= read -r -d '' f; do
   cat "$f"
done

https://mywiki.wooledge.org/BashFAQ/020

无论如何,只要cat,然后:git diff -z --cached --name-only | xargs -0 cat