Adding Only Untracked Files
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.
Related videos on Youtube
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, 2021Comments
-
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 over 8 yearsRegarding "throw everything but untracked files into the index",
.gitignore
is specifically engineered for that purpose, notgit add -u
. -
Pacerier over 8 yearsAlso, 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 over 12 yearsI was hoping there was something less, well, interactive, but it's certainly better than file by file. :-)
-
Mat over 12 years
echo -e "a\n*\nq\n"|git add -i
-
Stephan about 12 yearsalias gau="git ls-files -o --exclude-standard | xargs -i git add '{}'" works for me
-
Joshua Kunzmann almost 12 yearsgit ls-files --help is quite a useful read:
-o, --others
Show other (i.e. untracked) files in the output
-
BenKoshy almost 8 years@Mat thank you so is the complete command ----> git add -i a * q ??
-
Paul Coccoli over 7 yearsI like this approach, but it doesn't handle spaces in filenames.
-
Simeon over 7 years
Argument list too long
... so close! -
Lion789 over 7 yearsWhat to do, I am also, getting Argument list too long @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 over 5 yearsWorth noting
git add -i
also allows wildcards as parameters, so you can dogit add -i '*.xml'
to only add new xml files. -
marckassay over 5 yearsA more condensed version:
git ls-files -o --exclude-standard | % { git add $_ }
-
Pir Abdul about 5 yearsDon't forget commit after adding untrack files ;)
-
pmiranda almost 5 yearsI get a
Huh
after the a -
Tarasovych over 4 years
git add -u
will add modified files not untracked -
nitsujri over 4 yearsThis is exactly what I was looking for - allows for
git commit -p
to walk through new files as well. -
Harry B over 4 yearsFor me with git 2.21.0 is was
git add -i
then4
to add untracked then*
for all thenq
to quit -
CervEd over 3 years
gir add -i
then4
then1-300
repeat until done, if there are many untracked files -
seeker over 3 yearsNice, 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.