363

Is there a way to use a command like git ls-files to show only untracked files?

The reason I'm asking is because I use the following command to process all deleted files:

git ls-files -d | xargs git rm

I'd like something similar for untracked files:

git some-command --some-options | xargs git add

I was able to find the -o option to git ls-files, but this isn't what I want because it also shows ignored files. I was also able to come up with the following long and ugly command:

git status --porcelain | grep '^??' | cut -c4- | xargs git add

It seems like there's got to be a better command I can use here. And if there isn't, how do I create custom git commands?

Nick Volynkin
  • 11,882
  • 6
  • 39
  • 62
We Are All Monica
  • 11,597
  • 7
  • 41
  • 69
  • Could you elaborate why do you need `git ls-files -d | xargs git rm`? – takeshin Sep 27 '10 at 06:37
  • That removes all files that git notices are missing. My question was about how to do a related operation - add all files that git isn't currently tracking. I would usually do both of these after renaming, combining, and/or splitting my code files. – We Are All Monica Sep 27 '10 at 06:52
  • If they're missing, then aren't they already deleted? Unless... you pulled from somewhere else and then you're trying to synch with remote... I think I get it. – Buttle Butkus Dec 11 '15 at 04:34
  • I've tested all answers,if there's empty untracked folder, no one can find it. – kittygirl Jan 05 '19 at 02:15
  • 2
    @kittygirl this is correct. Because `git` works only on files, it does not have any way of tracking empty folders. Try `find . -type d -empty` instead. – We Are All Monica Jan 05 '19 at 02:45

9 Answers9

581

To list untracked files try:

git ls-files --others --exclude-standard

If you need to pipe the output to xargs, it is wise to mind white spaces using git ls-files -z and xargs -0:

git ls-files -z -o --exclude-standard | xargs -0 git add

Nice alias for adding untracked files:

au = !git add $(git ls-files -o --exclude-standard)

Edit: For reference: git-ls-files

PEdroArthur
  • 784
  • 7
  • 18
takeshin
  • 44,484
  • 28
  • 112
  • 160
  • 8
    Perfect! What does the `!` mean at the beginning of the alias line, or where is that documented? – We Are All Monica Sep 27 '10 at 06:31
  • @takeshin How to get only parent directory names? not subfolders. Means in these untracked files: /P/a/files , /P/a/images , /P/a/ /P/ , ... - I just wanna list /P/ – Dr.jacky Jul 16 '16 at 11:05
  • 2
    In what situation would you need the alias though? `git add *` will do the same thing: add all untracked files excluding the standard (files who's paths are covered in `.gitignore`). – toinetoine Jul 12 '17 at 12:04
  • 2
    I prefer my alias to work consistently in all directories in the repo (the ls-files command only shows changed files in the current tree), so I have `[alias] lso = "!f() { cd $(git rev-parse --show-toplevel); git ls-files -o; }; f"` – qneill Oct 12 '17 at 15:02
  • Thanks. Good comment. – Darth Egregious Jul 16 '18 at 15:18
  • @takeshin,does it include files tracked by git-lfs ? – kittygirl Jan 05 '19 at 01:24
  • @takeshin,this answer ignore empty folders. – kittygirl Jan 05 '19 at 01:48
75

If you just want to remove untracked files, do this:

git clean -df

add x to that if you want to also include specifically ignored files. I use git clean -dfx a lot throughout the day.

You can create custom git by just writing a script called git-whatever and having it in your path.

Dustin
  • 81,492
  • 18
  • 106
  • 131
  • It's much more common that I want to *add* all untracked files (for example, after I move some things around). In any case, thanks for the tip about custom commands. Where is that mechanism documented? – We Are All Monica Sep 27 '10 at 06:27
  • 6
    Just do `git add -A` or `git add -u` (depending on which makes more sense to you) – Dustin Sep 27 '10 at 17:34
  • 1
    Your comment is the real answer, which is also repeated later by @Mike Lococo – andho Apr 04 '17 at 11:57
  • 2
    and "git clean -dfxn" for a dry run by adding "n" – charo Feb 17 '18 at 13:05
56

git add -A -n will do what you want. -A adds all untracked files to the repo, -n makes it a dry-run where the add isn't performed but the status output is given listing each file that would have been added.

user
  • 85,380
  • 17
  • 189
  • 186
Mike Lococo
  • 561
  • 4
  • 2
  • 8
    This is a great answer! Note that it excludes files in `.gitignore` files, which is usually what we want, but if not, `git add -fAn`. – cdunn2001 Jun 29 '13 at 20:32
  • 1
    This seems like the best answer. – Rob Grant Aug 16 '16 at 09:08
  • 1
    This is THE best answer. It explicitly tells user what it would do (without doing it) and giving chance to review it before actually adding it. Perfect. Thanks! – digitguy Jun 08 '17 at 17:41
  • 1
    It also lists modified files. – Frank-Rene Schäfer Nov 02 '18 at 12:22
  • 1
    nice, but I get it wrapped: `add '$file'`. Whereas `git ls-files --exclude-standard -o` gives a bare output, piping hot. – hyperpallium Aug 16 '19 at 05:23
  • Just to say it clearly: This is not the searched-for answer. The original poster was looking for the bare file names without any ornamental characters (in order to process them by other commands), and without showing modified files, too. – fieres Oct 30 '20 at 10:51
29

Everything is very simple

To get list of all untracked files use command git status with option -u (--untracked-files)

git status -u
HDJEMAI
  • 7,766
  • 41
  • 60
  • 81
Valentyna
  • 323
  • 3
  • 2
  • how about a non-recursive version of that? – David Jun 10 '15 at 15:27
  • Unfortunately, it also lists file ignored thru .gitignore files. – Plouff Mar 01 '16 at 09:31
  • 9
    This is the only answer that showed my ignored files on initial commit, but I had to add `--ignored` to the `git status -u` command – D.N. Feb 27 '17 at 19:18
  • 1
    This is simple & nice. – user218867 Aug 24 '18 at 11:07
  • 1
    This *also* shows modified files. So, not an answer to the question: *only* show untracked files. – Wildcard Sep 12 '18 at 01:10
  • this also outputs extra lines such as `On branch ...`, `Untracked files:` and `(use "git add ..." to include in what will be committed)` before as well as `nothing added to commit but untracked files present (use "git add" to track)` after the actual file list - which must be removed if the output is to be used as input for another command – ssc May 04 '20 at 12:28
29

The accepted answer crashes on filenames with space. I'm at this point not sure how to update the alias command, so I'll put the improved version here:

git ls-files -z -o --exclude-standard | xargs -0 git add
Jean-François Fabre
  • 126,787
  • 22
  • 103
  • 165
youurayy
  • 1,499
  • 1
  • 15
  • 11
  • I think this is the correct answer, because it provides the path of each file to perform an action (like git add) on them afterwards – Pablo Burgos Nov 22 '18 at 15:29
8

When looking for files to potentially add. The output from git show does that but it also includes a lot of other stuff. The following command is useful to get the same list of files but without all of the other stuff.

 git status --porcelain | grep "^?? " | sed -e 's/^[?]* //'

This is useful when combined in a pipeline to find files matching a specific pattern and then piping that to git add.

git status --porcelain | grep "^?? "  | sed -e 's/^[?]* //' | \
egrep "\.project$|\.settings$\.classfile$" | xargs -n1 git add
cmac
  • 181
  • 2
  • 4
8

I know its an old question, but in terms of listing untracked files I thought I would add another one which also lists untracked folders:

You can used the git clean operation with -n (dry run) to show you which files it will remove (including the .gitignore files) by:

git clean -xdn

This has the advantage of showing all files and all folders that are not tracked. Parameters:

  • x - Shows all untracked files (including ignored by git and others, like build output etc...)
  • d - show untracked directories
  • n - and most importantly! - dryrun, i.e. don't actually delete anything, just use the clean mechanism to display the results.

It can be a little bit unsafe to do it like this incase you forget the -n. So I usually alias it in git config.

code_fodder
  • 12,697
  • 10
  • 68
  • 123
  • Why is git output so annoyingly human-readable? The proposed answer prints "Would remove" before each filename, so it's not usable for the OP's use case without further grepping. – fieres Oct 30 '20 at 10:54
3

All previous answers which I checked would list the files to be committed, too. Here is a simple and easy solution that only lists files which are not yet in the repo and not subject to .gitignore.

git status --porcelain | awk '/^\?\?/ { print $2; }'

or

git status --porcelain | grep -v '\?\?'
Frank-Rene Schäfer
  • 2,586
  • 17
  • 38
-2

I think this will do the same thing as the original poster intended:

git add .

Adding some caveats:

  • You have run git status and confirmed your local directories are clean
  • You have run git diff on each file reported in git status, and confirmed your changes are clean
  • Your changes are covered with automated unit testing
  • Your changes are covered with automated integration testing
  • You have run the integration tests through a debugger, verifying the new behavior by white box observing the new code in action
  • You have run all linting / code convention rules and they pass
  • You have run all unit tests and they pass
  • You have run all local integration tests, and they pass
  • You have deployed your changes to an app review environment and manually tested them end to end in isolation from other changes
  • You have merged latest from main branch and re-run all automated unit and integration testing, fixing any merge conflicts and test failures

Now, my friend, you are ready to git add . with impunity.

SE_net4 the downvoter
  • 21,043
  • 11
  • 69
  • 107
Tim Fulmer
  • 8,992
  • 4
  • 22
  • 32
  • 2
    Every time you do `git/svn add .`, the kitten dies. – Nakilon Jun 06 '13 at 15:28
  • Heh, only if you've got a bunch of cruft in your local copies of remote branches ;) Make local branches and you can `git add .` with impunity! – Tim Fulmer Jul 11 '13 at 22:19
  • 1
    @Nakilon, that's why you configure `.gitignore` properly. Still good to avoid `git add .`, though. Better is `git add -u`. – Wildcard Sep 12 '18 at 01:11
  • Heh, been a long time @Nakilon and @Wildcard. Updated the answer for clarity, there are definitely some assumptions about developer workflow that can impact safe usage of `git add .`. – Tim Fulmer Jan 06 '21 at 19:00
  • Wasn't my downvote. Nice list though. It kind of shows that you need to do a lot of stuff anyway to be sure that you are doing the things right and that's why staging files in more precise way than just `git add .` should not be considered like something tedious. That was my point. 2013? Bad that things don't really change -- people still do it and that's the unobvious but real half of the reason to have pull requests and code reviews. Like a fighting the symptoms. Downvotes were probably because you also have to do `git checkout .` before that to not add changes. – Nakilon Jan 06 '21 at 21:41