tags:

views:

878

answers:

2

Is it possible to ask git diff to include untracked files in its diff output? Or is my best bet to git add the new files I've created and the existing files I've edited, and use

git diff --cached

?

+2  A: 

I believe you can diff against files in your index and untracked files by simply supplying the path to both files.

git diff --no-index tracked_file untracked_file

Harold
Does that work if you've got more than one untracked file that you've created since the last commit?
Andrew Grimm
+5  A: 

With recent git versions you can "git add -N" the file (or "--intent-to-add"), which adds a zero-length blob to the index at that location. The upshot is that your "untracked" file now becomes a modification to add all the content to this zero-length file, and that shows up in the "git diff" output.

srh@devo16:/tmp/git-example <master>$ git diff
srh@devo16:/tmp/git-example <master>$ echo "this is a new file" > new.txt
srh@devo16:/tmp/git-example <master>$ git diff
srh@devo16:/tmp/git-example <master>$ git add -N new.txt
srh@devo16:/tmp/git-example <master>$ git diff
diff --git a/new.txt b/new.txt
index e69de29..3b2aed8 100644
--- a/new.txt
+++ b/new.txt
@@ -0,0 +1 @@
+this is a new file

Another point about this is that now that the new file is a modification and the path is known to git, "git stash" will work to stash the file away too.

araqnid
It turns out that my copy of Git isn't recent enough to have add -N, but this answers my question.
Andrew Grimm
You can emulate "git add -N new.txt" with "git update-index --add --cacheinfo 100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 new.txt"(how did I manage to put this on the wrong answer?)
araqnid