0

I want to show the result of both git diff <PATH> and git diff --staged <PATH>. I know this answer (Show both staged & working tree in git diff?) says you can use git status -vv, but it doesn't accept a path argument so I can't do git status -vv -- <PATH>. Anyone know a way to show both worktree and index diffs for a single path?

I need this solution for a script so I don't mind having to run multiple commands to get the result.

11th Hour Worker
  • 337
  • 3
  • 14

1 Answers1

1

(I assume above that you mean --cached or --staged, not --index.)

The git status command literally just runs two diffs (easy, since it has git diff built into it: that's just two subroutine calls). You can do the same: just run git diff -- path and git diff --cached -- path.

If you want to make your script more bulletproof, be aware that git diff is so-called porcelain and thus changes behavior based on user configurations. To avoid this, use the plumbing commands that git diff and git diff --index will use:

git diff-files -p -- <path>
git diff-index --cached HEAD -p -- <path>

You must decide for yourself, based on intended usage, whether your script should obey user configurations (in which case, don't use the plumbing commands) or not (in which case, do use them).

torek
  • 448,244
  • 59
  • 642
  • 775