Adding Only Untracked Files

前端 未结 11 1898
傲寒
傲寒 2020-12-07 07:04

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

相关标签:
11条回答
  • 2020-12-07 07:42

    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.

    0 讨论(0)
  • 2020-12-07 07:46

    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
    
    0 讨论(0)
  • 2020-12-07 07:47

    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 $_ }
    
    0 讨论(0)
  • 2020-12-07 07:51

    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.

    0 讨论(0)
  • 2020-12-07 07:52

    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

    0 讨论(0)
  • 2020-12-07 07:54

    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

    0 讨论(0)
提交回复
热议问题