Git-Diff --no-index but .gitignore

Viewed 28

From a git working tree (checked out copy), I'd like to diff the current working copy tree against a different directory (which is outside).

Like so (and also with a subdirectory just for a quick compare):

git diff --no-index --stat -- $HOME/project/pink-blue/subdirectory \
                              $HOME/temp-copy/subdirectory

But right now it shows files added under the outside path.

While this first of all is the documented behaviour, I would be pleased if the maintained .gitignore file would be applied to ignore any added files it would cope with.

Now this may not be an option of git-diff(1) for --no-index. But is it?

As all my trials were so daunting I decided to formulate the question first.

1 Answers

If you're missing the "what have you tried part" (or found it lame), asking the question here on SO finally got me the following idea:

Instead of diffing from the working tree to the elsewhere subdirectory, what about telling git that the work-tree is there?

git --work-tree=$HOME/temp-copy diff --stat -- subdirectory 

This requires I'm committed clean (or --cached ?) therefore is not 100% what I had in mind, but a git diff --quiet should break it then before lying.

It also requires that $HOME/temp-copy has a sub-directory named subdirectory. Which was the case and it may be common.

And it is pretty close as for the untracked files that I didn't want to get listed, they are now ignored per the .gitignore and don't show up any longer.


To list the "unstaged"/new files, it is possible, too:

git --work-tree=$HOME/temp-copy ls-files --others --exclude-standard subdirectory
Related