Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use git diff on untracked files?

Tags:

git

diff

Is it possible to ask git diff to include untracked files in its diff output, or is my best bet to use git add on the newly created files and the existing files I have edited, then use:

git diff --cached 

?

like image 642
Andrew Grimm Avatar asked May 13 '09 02:05

Andrew Grimm


People also ask

Can I commit with untracked files?

Untracked basically means that Git sees a file you didn't have in the previous snapshot (commit), and which hasn't yet been staged; Git won't start including it in your commit snapshots until you explicitly tell it to do so.

Does git stash work on untracked files?

In order to stash untracked files, add the “–include-untracked” option to your “git stash” initial command. Alternatively, you can simply use the “-u” which is equivalent to the untracked longer version.

Can I git push with untracked files?

Both the git stash save and push commands work with the --include-untracked and -all switches. However, the git stash save command is deprecated, which means that any new development should only use push.

How do I make an untracked file patch?

To make the untracked files visible to the git diff command, we staged them (using git add ) and then used the following command to create the patch: git diff --patch --staged; git diff [--options] --cached [<commit>] [--] [<path>...]


2 Answers

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.

git diff  echo "this is a new file" > new.txt git diff  git add -N new.txt 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 

Sadly, as pointed out, you can't git stash while you have an --intent-to-add file pending like this. Although if you need to stash, you just add the new files and then stash them. Or you can use the emulation workaround:

git update-index --add --cacheinfo \ 100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 new.txt 

(setting up an alias is your friend here).

like image 54
araqnid Avatar answered Sep 30 '22 21:09

araqnid


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 
like image 40
Harold Avatar answered Sep 30 '22 19:09

Harold