它的工作方式是这样的:
MYPC /d/home/project/some/path (master)
$ git diff --name-only --cached
root.txt
some/path/relative.txt
即它显示来自 GIT 根目录的路径,但我需要来自当前目录的相对路径。
预期结果:
$ git diff --name-only --cached --AND_SOME_OPTION
../../root.txt
relative.txt
按常理来说,它应该像git status
那样工作。
附言
--relative
选项不起作用,因为它将显示来自该目录的文件。
在我们的示例中,它将仅显示 relative.txt
。
P.P.S
使用 --git-dir
也不起作用:
$ git --git-dir=$(git rev-parse --show-toplevel)/.git diff --cached --name-only
root.txt
some/path/relative.txt
最佳答案
git status -s
已经输出了可以轻松隔离的相对路径。
如果需要使用 git diff
,可以将输出通过管道传输到 realpath
(如果可用):
$ git diff --name-only | \
xargs -I '{}' realpath --relative-to=. $(git rev-parse --show-toplevel)/'{}'
../../root.txt
relative.txt
关于git - 如何在 git diff 命令中显示相对路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60613517/