7

When using git diff it only shows files which are already been tracked. Is there a way to show the untracked files as well for a diff? The goal is to get a git patch that also contains this, namely I have a script which starts with git diff and eventually pipes it to git apply

Mahoni
  • 7,088
  • 17
  • 58
  • 115
  • 2
    Have you poked around SO for articles [like this one](http://stackoverflow.com/questions/855767/can-i-use-git-diff-on-untracked-files) ? – Tim Biegeleisen Oct 21 '15 at 08:33
  • @TimBiegeleisen, I did, but didn't notice the `git ls-files -o` solution because it's undervoted and an incorrect one is marked as a solution instead. – Iulian Onofrei Apr 27 '18 at 10:46

1 Answers1

9

You can show untracked files with git ls-files:

$ git ls-files -o

-o is a flag for showing other type files, meaning anything which is not indexed. See the ls-files documentation at git-scm.com.

EDIT:

This answer did not directly answer the question.

An issue with diffing untracked (unindexed) files is that there is no git history to diff against. You would have to have some history for the untracked file (unless a create diff is OK for you). In the comments a method to do this is as follows:

git ls-files -o --exclude-standard | xargs git add; git diff --staged
^                                  ^                ^
ls all untracked files             pipe to add      diff the staged files

Essentially you need to add all files to the git index to be able to generate diff and patches on the changes.

This adds the untracked files to index so you will have to remember and unstage them yourself after you're done diffing.

For boring input sanitization reasons, this pipe can be improved once again by delimiting the results of git ls-files with the null terminator:

git ls-files -o --exclude-standard -z | xargs -O git add; git diff --staged
^                                   v ^        v        ^
ls all untracked files              | pipe to  |        diff the staged files
                                    | add      |
                                     \__________>-> Use Null as Output/Input delimeter
ojrask
  • 2,799
  • 1
  • 23
  • 23
  • 5
    Useful...but does not answer the question. It lists the files, but does not *show the diff* of those files, which the OP is looking for. Additionally, it also lists .gitignored files, which is probably not what you want when looking or untracked files. – David Hempy Oct 09 '17 at 20:22
  • Agree with above comment. `git ls-files -o --exclude-standard | xargs git add; git diff --staged` will do the trick, however you'll have to unstage the files after. – philraj Nov 09 '17 at 17:02
  • 1
    @DavidHempy ah yes, noticed this now. The issue here will become that as the untracked files are not indexed there is no diff data available. philraj offers a suggestion for this though. I will edit the answer to make this clearer. – ojrask Dec 12 '17 at 11:00