Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show untracked files as git diff [duplicate]

Tags:

git

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

like image 981
Mahoni Avatar asked Oct 21 '15 08:10

Mahoni


People also ask

How do I see all untracked files in git?

-unormal which shows untracked files and directories. This is the default behaviour of git status. -uall Which shows individual files in untracked directories.

Does git status show untracked files?

If git status mentions "Untracked files:", you may need to add one or more untracked files. This status message is OK. We are up-to-date, there is nothing to commit, but there is still an untracked file.

How do I make git not show untracked files?

You can use the git clean command to remove untracked files. The -fd command removes untracked directories and the git clean -fx command removes ignored and non-ignored files. You can remove untracked files using a . gitignore file.


1 Answers

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
like image 114
ojrask Avatar answered Oct 08 '22 01:10

ojrask