How to show relative paths in git diff command?

£可爱£侵袭症+ 提交于 2020-05-09 02:49:26

问题


It works in this way:

MYPC /d/home/project/some/path (master)
$ git diff --name-only --cached
root.txt
some/path/relative.txt

I.e. it shows path from the GIT root, but I need relative paths from current directory.

Expected result :

$ git diff --name-only --cached --AND_SOME_OPTION
../../root.txt
relative.txt

In common sense, it should work like git status.

P.S. The --relative option doesn't work because it will show files from this directory. In our example it will show only relative.txt.

P.P.S

Using --git-dir doesn't work as well:

$ git --git-dir=$(git rev-parse --show-toplevel)/.git  diff --cached --name-only
root.txt
some/path/relative.txt

回答1:


git status -s already outputs relative paths that can be easily isolated.

If you need to use git diff, you can pipe the output to realpath, if available:

$ git diff --name-only | \
    xargs -I '{}' realpath --relative-to=. $(git rev-parse --show-toplevel)/'{}'
../../root.txt
relative.txt



回答2:


So far, I didn't find a way to use relative paths by using git-diff.

The only way work for me:

$ git status -s | awk '{print $2}'
../../root.txt
relative.txt

Or

$ git status -s | cut -c4-

The last approach explained in How can I run "git status" and just get the filenames. . The first one quite similar. :)

But it'd be good to find non-pipeline way. Seem, there is no way to avoid using --cached. So, mostly it's not an answer.



来源:https://stackoverflow.com/questions/45106506/how-to-show-relative-paths-in-git-diff-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!