Adding Only Untracked Files

GitGit Add

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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

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 - Git

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 - Git

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 - Git

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.

Solution 6 - Git

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 over files 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

Solution 7 - Git

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

Solution 8 - Git

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 $_ }

Solution 9 - Git

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.

Solution 10 - Git

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....

Solution 11 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRob WilkersonView Question on Stackoverflow
Solution 1 - GitMatView Answer on Stackoverflow
Solution 2 - GitmanojldsView Answer on Stackoverflow
Solution 3 - GithasenView Answer on Stackoverflow
Solution 4 - GitjoshuadaveyView Answer on Stackoverflow
Solution 5 - GitTom FenechView Answer on Stackoverflow
Solution 6 - GitRohan KhudeView Answer on Stackoverflow
Solution 7 - GitpborensteinView Answer on Stackoverflow
Solution 8 - GitSimeonView Answer on Stackoverflow
Solution 9 - GitTore AurstadView Answer on Stackoverflow
Solution 10 - GitatreyHazelHispanicView Answer on Stackoverflow
Solution 11 - GitL.SidhuView Answer on Stackoverflow