git diff 只输出文件名

标签 git bash git-diff

我在 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/

相关文章:

python - pip 安装并运行 git repo

node.js - -bash : node: command not found (on EC2 instance)

git - git的耐心差异算法实现正确吗?

windows - Git diff 在窗口 git bash shell (Windows) 中损坏

混帐 pull : could not detach HEAD for Unicode filenames

git - 如何生成上个月的 git 提交日志,并将其导出为 CSV?

GitHub 上的 Python 包

bash - 如何确定文件是否不可在 bash 脚本中执行

arrays - 将远程 ssh 命令的输出存储到循环中的局部变量

Git 提交补丁