Getting a list of all unversioned files in a Git-controlled folder

13,370

Solution 1

Or...

git clean -dnx | cut -c 14-

If you don't want to see ignored files,

git clean -dn | cut -c 14-

Solution 2

Actually, if you use git clean with the -n or --dry-run option, it will print out a list untracked files that it would have removed had you run it with the -f or --force option. Adding the -d flag includes directories that are either empty or contain only untracked files.

So you can run this command from within a git repository:

$ git clean -dn

And get output like this:

Would remove dir/untracked_file_1.txt
Would remove untracked_file_2.txt

Here's a bonus: It respects your .gitignore file as well, though if you add the -X flag, it will list only ignored files.

Share:
13,370
Ryan Lester
Author by

Ryan Lester

Phone: +1(337) 935 0016 Email: [email protected]

Updated on July 10, 2022

Comments

  • Ryan Lester
    Ryan Lester almost 2 years

    Getting a list of unversioned files in a Git-controlled folder is way more annoying than it needs to be. Unless I really suck at reading man pages, it doesn't look like Git provides a facility to perform this operation on its own.

    There may be a more elegant way of performing this, but here's a one-liner I threw together for this task, in case anyone else ever needs to use it.

    Edit: turns out there's a standard way to do this that already works well.

    git ls-files --other [--exclude-standard]