我在 Bash 窗口中对标签使用 git diff 并且它在大多数情况下都在做我想做的事情。
$ git diff -- numstat TAG2 TAG1
给出这个输出:
4 6 dir1/dir2/dir3/dir4/thisIsMyFile.cs
我怎样才能得到这个输出:
4 6 thisIsMyFile.cs
我不需要/想要路径 (dir1/dir2/dir3/dir4/
),只需要文件名 (thisIsMyFile.cs
)。
最佳答案
据我所知,没有办法单独使用 Git 来做到这一点(即,只为 Git diff 提供正确的参数)。您必须使用 Bash 管道对 Git 输出进行后处理。我建议:
git diff --numstat TAG2 TAG1 | while IFS= read -r line; do
printf '%s\t' "$(cut -f-2 <<<"$line")"
basename "$(cut -f2- <<<"$line")"
done
这有点复杂,因为它旨在保留原始格式并正确解析,即使文件中包含制表符或空格也是如此。如果您可以保证没有文件的文件名中有空格,或者如果您有兴趣使用比 Bash 更强大的脚本语言来解析行,则可以简化它。
关于git diff 只输出文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46332022/