9

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
Kirby
  • 2,847
  • 2
  • 32
  • 42
  • Possible duplicate of [How to make git log show file paths relative to current directory?](https://stackoverflow.com/questions/43750693/how-to-make-git-log-show-file-paths-relative-to-current-directory) – binduck Jul 14 '17 at 15:32
  • @pyx, no, the solution form the question doesn't work for me. – Kirby Jul 14 '17 at 16:49

3 Answers3

9

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
Joao Delgado
  • 861
  • 4
  • 12
  • Yeah, I definitely need `git-diff` because there is the option `--cached` which works for me. Thanks for the post! – Kirby Jul 17 '17 at 16:23
1

Building on Joao Delgado suggestion above to use realpath, you can trick Git into doing what you want using --src-prefix and --dst-prefix:

$ rel=$(realpath --relative-to=. $(git rev-parse --show-toplevel))
$ git diff --src-prefix=a/$rel/ --dst-prefix=b/$rel/
../../root.txt
../../some/path/relative.txt

This will show all modified files (not just those in the current working directory), and it will also use relative paths in the output (for all files though).

Note that at the toplevel this will output paths with a leading ./:

./root.txt
./some/path/relative.txt

Here is how to configure a df alias:

$ git config --global alias.df '!f() { : git diff ; rel=$(realpath --relative-to="$PWD/$GIT_PREFIX" "$PWD"); git diff --src-prefix="a/$rel/" --dst-prefix="b/$rel/" "$@"; }; f'
$ git df
../../root.txt
../../some/path/relative.txt

(this uses the null command : to enable git diff Bash completion for this alias (see https://github.com/git/git/blob/1a4874565fa3b6668042216189551b98b4dc0b1b/contrib/completion/git-completion.bash#L26-L30)

philb
  • 2,031
  • 15
  • 20
0

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.

Kirby
  • 2,847
  • 2
  • 32
  • 42