Adding Only Untracked Files

2019-01-16 00:33发布

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? In the last few months, I've often found myself in a position where I've interactively added some updates to the index and I want to add all of the untracked files to that index before I commit.

Is there a way to add only the untracked files to the index without identifying them individually? I don't see anything obvious in the help docs, but maybe I'm missing it?

Thanks.

标签: git git-add
9条回答
Rolldiameter
2楼-- · 2019-01-16 00:51

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
查看更多
孤傲高冷的网名
3楼-- · 2019-01-16 00:56

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

查看更多
爷的心禁止访问
4楼-- · 2019-01-16 00:56

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)
查看更多
欢心
5楼-- · 2019-01-16 00:58

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

查看更多
来,给爷笑一个
6楼-- · 2019-01-16 00:59

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.

查看更多
一纸荒年 Trace。
7楼-- · 2019-01-16 01:02

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.

查看更多
登录 后发表回答