Adding Only Untracked Files
Asked Answered
C

11

210

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?

Celestecelestia answered 16/9, 2011 at 15:1 Comment(2)
Regarding "throw everything but untracked files into the index", .gitignore is specifically engineered for that purpose, not git add -u.Thermotensile
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)?Thermotensile
H
318

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

Harmattan answered 16/9, 2011 at 15:6 Comment(12)
I was hoping there was something less, well, interactive, but it's certainly better than file by file. :-)Celestecelestia
echo -e "a\n*\nq\n"|git add -iHarmattan
@Harmattan thank you so is the complete command ----> git add -i a * q ??Belda
Argument list too long... so close!Maxon
What to do, I am also, getting Argument list too long @MaxonIrwin
@Irwin See my answer on how to do this on Windows here: https://mcmap.net/q/12397/-adding-only-untracked-files/…Maxon
Worth noting git add -i also allows wildcards as parameters, so you can do git add -i '*.xml' to only add new xml files.Cyruscyst
Don't forget commit after adding untrack files ;)Imagination
I get a Huh after the aFye
For me with git 2.21.0 is was git add -i then 4 to add untracked then * for all then q to quitNewel
gir add -i then 4 then 1-300 repeat until done, if there are many untracked filesNoncontributory
if the command is too difficult to remember, you can create: aliases - either in your .bashrc file (or equivalent shell), or you can create git aliases. edit: consider the answer below: https://mcmap.net/q/12397/-adding-only-untracked-filesBelda
S
46

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)
Shoon answered 16/9, 2011 at 15:21 Comment(3)
alias gau="git ls-files -o --exclude-standard | xargs -i git add '{}'" works for meCostin
git ls-files --help is quite a useful read: -o, --others Show other (i.e. untracked) files in the outputMaud
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.Within
T
22

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.

Tint answered 4/12, 2013 at 18:51 Comment(1)
This is exactly what I was looking for - allows for git commit -p to walk through new files as well.Uke
R
15

I tried this and it worked :

git stash && git add . && git stash pop

git stash will only put all modified tracked files into separate stack, then left ones are untracked files. Then by doing git add . will stage all files untracked files, as required. Eventually, to get back all modified files from stack by doing git stash pop

Roselane answered 5/11, 2017 at 16:35 Comment(1)
that's really a nice way of doing it!Trager
H
12

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
Hutchings answered 17/11, 2012 at 2:12 Comment(1)
I like this approach, but it doesn't handle spaces in filenames.Baboon
P
12

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.

Papotto answered 11/8, 2015 at 8:56 Comment(0)
I
8

git ls-files lists the files in the current directory. If you want to list untracked files from anywhere in the tree, this might work better:

git ls-files -o --exclude-standard $(git rev-parse --show-toplevel)

To add all untracked files in the tree:

git ls-files -o --exclude-standard $(git rev-parse --show-toplevel) | xargs git add
Inhumation answered 24/4, 2014 at 1:40 Comment(0)
M
2

If you have thousands of untracked files (ugh, don't ask) then git add -i will not work when adding *. You will get an error stating Argument list too long.

If you then also are on Windows (don't ask #2 :-) and need to use PowerShell for adding all untracked files, you can use this command:

git ls-files -o --exclude-standard | select | foreach { git add $_ }
Maxon answered 21/10, 2016 at 8:41 Comment(1)
A more condensed version: git ls-files -o --exclude-standard | % { git add $_ }Andrej
A
1

Lot of good tips here, but inside Powershell I could not get it to work.

I am a .NET developer and we mainly still use Windows OS as we haven't made use of .Net core and cross platform so much, so my everyday use with Git is in a Windows environment, where the shell used is more often Powershell and not Git bash.

The following procedure can be followed to create an aliased function for adding untracked files in a Git repository.

Inside your $profile file of Powershell (in case it is missing - you can run: New-Item $Profile)

notepad $Profile

Now add this Powershell method:

function AddUntracked-Git() {
 &git ls-files -o --exclude-standard | select | foreach { git add $_ }
}

Save the $profile file and reload it into Powershell. Then reload your $profile file with: . $profile

This is similar to the source command in *nix environments IMHO.

So next time you, if you are developer using Powershell in Windows against Git repo and want to just include untracked files you can run:

AddUntracked-Git

This follows the Powershell convention where you have verb-nouns.

Aparri answered 10/5, 2019 at 14:4 Comment(0)
C
1

git add . (add all files in this directory)

git add -all (add all files in all directories)

git add -N can be helpful for for listing which ones for later....

Cracy answered 10/7, 2019 at 19:29 Comment(1)
git add -u will add modified files not untrackedCrossgrained
S
-3

To add all untracked files git command is

git add -A

Also if you want to get more details about various available options , you can type command

git add -i

instead of first command , with this you will get more options including option to add all untracked files as shown below :

$ git add -i warning: LF will be replaced by CRLF in README.txt. The file will have its original line endings in your working directory. warning: LF will be replaced by CRLF in package.json.

* Commands * 1: status 2: update 3: revert 4: add untracked 5: patch 6: diff 7: quit 8: help What now> a

Shawna answered 9/6, 2014 at 16:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.