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

181,416

Solution 1

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

Solution 2

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.

Solution 3

git add -A -n will do what you want. -A adds all untracked and modified 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.

Solution 4

Everything is very simple

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

git status -u

Solution 5

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
Share:
181,416
We Are All Monica
Author by

We Are All Monica

Updated on June 10, 2021

Comments

  • We Are All Monica
    We Are All Monica almost 3 years

    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?