Adding Only Untracked Files

129,608

Solution 1

It's easy with git add -i. Type a (for "add untracked"), then * (for "all"), then q (to quit) and you're done.

To do it with a single command: echo -e "a\n*\nq\n"|git add -i

Solution 2

git ls-files -o --exclude-standard gives untracked files, so you can do something like below ( or add an alias to it):

git add $(git ls-files -o --exclude-standard)

Solution 3

Not exactly what you're looking for, but I've found this quite helpful:

git add -AN

Will add all files to the index, but without their content. Files that were untracked now behave as if they were tracked. Their content will be displayed in git diff, and you can add then interactively with git add -p.

Solution 4

You can add this to your ~/.gitconfig file:

[alias]
    add-untracked = !"git status --porcelain | awk '/\\?\\?/{ print $2 }' | xargs git add"

Then, from the commandline, just run:

git add-untracked

Solution 5

People have suggested piping the output of git ls-files to git add but this is going to fail in cases where there are filenames containing white space or glob characters such as *.

The safe way would be to use:

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

where -z tells git to use \0 line terminators and -0 tells xargs the same. The only disadvantage of this approach is that the -0 option is non-standard, so only some versions of xargs support it.

Share:
129,608

Related videos on Youtube

Rob Wilkerson
Author by

Rob Wilkerson

I am a development manager and engineer, but I haven't always been one. I've also been an architect, a carpenter and a paratrooper (never a butcher, baker or candlestick maker). I have nearly 15 years of experience designing, engineering and developing Internet solutions. That experience extends to building and leading intra- and international development teams and organizing those teams around an evolving set of tools, standards, practices and processes. Sadly, I still can't design my way out of a wet paper bag.

Updated on June 10, 2021

Comments

  • Rob Wilkerson
    Rob Wilkerson almost 3 years

    One of the commands I find incredibly useful in Git is git add -u to throw everything but untracked files into the index. Is there an inverse of that?

    Such as a way to add only the untracked files to the index without identifying them individually?

    • Pacerier
      Pacerier over 8 years
      Regarding "throw everything but untracked files into the index", .gitignore is specifically engineered for that purpose, not git add -u.
    • Pacerier
      Pacerier over 8 years
      Also, are you asking to add the untracked files while removing all the currently tracked ones, or are you asking to add the untracked files on top of the currently tracked ones (making everything tracked)?
  • Rob Wilkerson
    Rob Wilkerson over 12 years
    I was hoping there was something less, well, interactive, but it's certainly better than file by file. :-)
  • Mat
    Mat over 12 years
    echo -e "a\n*\nq\n"|git add -i
  • Stephan
    Stephan about 12 years
    alias gau="git ls-files -o --exclude-standard | xargs -i git add '{}'" works for me
  • Joshua Kunzmann
    Joshua Kunzmann almost 12 years
    git ls-files --help is quite a useful read: -o, --others Show other (i.e. untracked) files in the output
  • BenKoshy
    BenKoshy almost 8 years
    @Mat thank you so is the complete command ----> git add -i a * q ??
  • Paul Coccoli
    Paul Coccoli over 7 years
    I like this approach, but it doesn't handle spaces in filenames.
  • Simeon
    Simeon over 7 years
    Argument list too long... so close!
  • Lion789
    Lion789 over 7 years
    What to do, I am also, getting Argument list too long @Simeon
  • Simeon
    Simeon over 7 years
    @Lion789 See my answer on how to do this on Windows here: stackoverflow.com/questions/7446640/adding-only-untracked-fiβ€Œβ€‹les/…
  • user1338062
    user1338062 over 5 years
    Worth noting git add -i also allows wildcards as parameters, so you can do git add -i '*.xml' to only add new xml files.
  • marckassay
    marckassay over 5 years
    A more condensed version: git ls-files -o --exclude-standard | % { git add $_ }
  • Pir Abdul
    Pir Abdul about 5 years
    Don't forget commit after adding untrack files ;)
  • pmiranda
    pmiranda almost 5 years
    I get a Huh after the a
  • Tarasovych
    Tarasovych over 4 years
    git add -u will add modified files not untracked
  • nitsujri
    nitsujri over 4 years
    This is exactly what I was looking for - allows for git commit -p to walk through new files as well.
  • Harry B
    Harry B over 4 years
    For me with git 2.21.0 is was git add -i then 4 to add untracked then * for all then q to quit
  • CervEd
    CervEd over 3 years
    gir add -i then 4 then 1-300 repeat until done, if there are many untracked files
  • seeker
    seeker over 3 years
    Nice, however I believe this will only give you the untracked files for the current directory. You would need to use pborenstein's answer above to retrieve a list of untracked files for the entire working tree.