Git: list only "untracked" files (also, custom commands)

We Are All Monica picture We Are All Monica · Sep 27, 2010 · Viewed 156.2k times · Source

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?

Answer

takeshin picture takeshin · Sep 27, 2010

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