Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 389
We Are All Monica Avatar asked Sep 27 '10 05:09

We Are All Monica


People also ask

How do you git add all modified files but not untracked?

The easiest way to add all files to your Git repository is to use the “git add” command followed by the “-A” option for “all”. In this case, the new (or untracked), deleted and modified files will be added to your Git staging area.


1 Answers

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

like image 144
takeshin Avatar answered Sep 21 '22 11:09

takeshin