gitpatchgit-untracked

Is there a way to add untracked files in git when adding via patch?


So I'm learning the joys of git add -p. I used to use Sourcetree or similar GUI tool to review changes as part of git add, but now I'm getting better with it on the command line.

However, I'm frustrated that I can't add the untracked files in the same command. I'd like it if there was some option like: git add -p --include-untracked that then treats them as a patch like the rest of it.

Is there a better way to do this as a single command?


Solution

  • I don't know of an option to do this directly, seems like a missing feature. Here's some work arounds.

    You can run git add --intent-to-add . or -N before to track all untracked files. This won't add their contents and their changes will show up in git add -p. You can add this as an alias in your .gitconfig.

    [alias]
        addp = !git add -N . && git add -p
    

    You can use interactive mode, git add -i, to get finer control, including adding untracked files.

    $ git add -i
    
    *** Commands ***
      1: status       2: update       3: revert       4: add untracked
      5: patch        6: diff         7: quit         8: help
    What now> 4
      1: foo
    Add untracked>> 1
    * 1: foo
    Add untracked>> ^D
    added 1 path
    
    *** Commands ***
      1: status       2: update       3: revert       4: add untracked
      5: patch        6: diff         7: quit         8: help
    What now> ^D
    Bye.
    
    $ git status
    On branch master
    
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
            new file:   foo