Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show relative paths in git diff command?

Tags:

git

git-diff

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
like image 415
Kirby Avatar asked Jul 14 '17 15:07

Kirby


1 Answers

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
like image 181
Joao Delgado Avatar answered Oct 16 '22 11:10

Joao Delgado