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

1

5

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?

Sufficient answered 7/10, 2019 at 16:18 Comment(0)
P
6

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
Pregnable answered 7/10, 2019 at 17:19 Comment(4)
Amazing ! I use Git for many years now and I encountered the same problem not later than yesterday. Considering what you exposed here, I'll say we no longer lack a new feature, but simply the send of patch to the development team that just appends a note to existing message : "to add a new file by patch, please use git -N first".Caddaric
@Caddaric If you're sending patch files around Git has a robust system for dealing with patches files. But patch files are awkward and lose a lot of the power of Git. Instead consider using Git remotes. "Hey dev team, here's a URL to my repository and my work is in branch feature." Then they can git remote add obsidian <url> and git fetch obsidian and merge obsidian/feature like any other branch.Pregnable
Many thanks once again for these precisions. Of course, when I said "sending a patch", it was in a global sense. I used these commands for Linux Kernel's patch submissions (especially while taking the Eudyptula project) and do pull requests for other projects. But I upvote anyway because these informations remain useful for everyone.Caddaric
That addp alias is the perfect solution. It's just too bad it's not the default. I can't imagine a case where I wouldn't want to add new files. I guess if I'm starting off and haven't done my .gitignore bits correctly yet. But otherwise, in normal code flow, new files and changed files are all part of the work I want to stage, 99% of the time. So now I'm just retraining my brain to do git addp and I'm all goodSufficient

© 2022 - 2024 — McMap. All rights reserved.