git - 如何在 git diff 命令中显示相对路径?

标签 git git-diff

它的工作方式是这样的:

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/

相关文章:

python - 远程 : ImportError: No module named gitlab

git 差异 : what is the difference between --cached and --staged

git - 错误 : unmappable character for encoding UTF8 after GIT merge

windows - .gitconfig 安装在 Windows 10 的错误驱动器上

git - 使用 svn2git 将 SVN 迁移到 GIT 失败

git - 在单独的终端屏幕上显示 git diff 和 git log 输出

php - 如何在 phpunit 中模拟 git diff 的结果

git - 错误 : unable to unlink old 'gulpfile.js' (Permission denied) while using Docker Compose for development environment

java - 我可以将我的 TODO 转换为 git 错误吗?