4

Under a specific <directory> (within a git repository),
I want a complete list of files that are:

  • untracked by git
  • but not ignored by git (using .gitignore).

Is there an easy way to do this?

Note that while git status does list untracked files that are not ignored,
it fails to list the actual files within any untracked sub-directories within the git repository.

TheCodeArtist
  • 21,479
  • 4
  • 69
  • 130
skyking
  • 13,817
  • 1
  • 35
  • 57
  • possible duplicate of [Show ignored files in git](http://stackoverflow.com/questions/466764/show-ignored-files-in-git) – TheCodeArtist Sep 02 '15 at 09:30
  • This is not a duplicate since that question asked to show ignored files and this to show **non**-ignored files (that are untracked). – skyking Sep 02 '15 at 09:43
  • Hmmm... ok. Misread the Q. Retracted the my duplicate close-vote. What threw me off was that this question initially appeared to be a misconstrued example that was answered by yourself immediately. **My apologies for not properly reading it the first time**. Added a few details; hopefully it clarifies the Q & A. – TheCodeArtist Sep 02 '15 at 13:39

1 Answers1

3

Yes, by using git ls-files.

git ls-files -o --exclude-standard [directory]
  • -o shows other (i.e. untracked) files in the output
  • --exclude-standard adds the standard Git exclusions
    from .gitignore in each directory, .git/info/exclude and ~/.gitignore_global.

Here is a sample git repository with 2 new untracked local files. Both of which are in a newly added untracked directory. One of the untracked files matches a pattern *.o specified in the .gitignore.

~/linux-stable$ ls -lR kernel/untracked-dir/
kernel/untracked-dir/:
total 8
-rw-rw-r-- 1 cvs cvs 7 Sep  2 18:46 untracked-ignored-file.o
-rw-rw-r-- 1 cvs cvs 7 Sep  2 18:46 untracked-non-ignored-file.c

Running git statussimply lists the new untracked sub-directory and not the individual files within

~/linux-stable$ git status kernel/
On branch master
Your branch is up-to-date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)

kernel/untracked-dir/

nothing added to commit but untracked files present (use "git add" to track)

Whereas using git ls-files -o --exclude-standard, we get :

~/linux-stable$ git ls-files -o --exclude-standard 
kernel/untracked-dir/untracked-non-ignored-file.c

i.e. the actual list of untracked-files (including the ones within any untracked directories).

TheCodeArtist
  • 21,479
  • 4
  • 69
  • 130
skyking
  • 13,817
  • 1
  • 35
  • 57
  • 1
    Looks like you posted a question and its answer almost simultaneously. If you are interested in documenting stuff on StackOverflow, consider joining the [**Documentation Beta**](http://meta.stackoverflow.com/questions/303865/warlords-of-documentation-a-proposed-expansion-of-stack-overflow). – TheCodeArtist Sep 02 '15 at 08:03
  • I thought answering your own question was a behavior that was encouraged. I still think this is suitable for the QA format of stackoverflow. In addition it's not that clear how I find that "Documentation". – skyking Sep 02 '15 at 08:32
  • No offense intented, as detailed in the SO meta question linked in my previous comment above, register for the public beta of [StackOverflow Documentation](https://docs.google.com/forms/d/13ynCK-DEy0osod8VIENajnbFJNZxXm1jyeupBrl5v44/viewform). It aims to be the "next big thing" to submit/maintain such useful bits of clarity to otherwise obscure code/commands. – TheCodeArtist Sep 02 '15 at 15:28
  • I don't like the idea to register if I can't see what I'm registering to before. Fx I registered to stackoverflow because that was were I ended up when I search for answers to my questions and I found answers here before I registered. – skyking Sep 03 '15 at 05:00