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