git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: git add --patch newfile doesn't add newfile to cache ?

From
Jeff King <peff@peff.net>
Date
Oct 20, 2008, 23:50 UTC
Message-ID
<20081020235049.GA23120@coredump.intra.peff.net>
In-Reply-To
<20081020143636.GB3988@gmx.de>
On Mon, Oct 20, 2008 at 04:36:36PM +0200, Marc Weber wrote:
Show 7 quoted lines
> Is this desired behaviour?
> [...]
>         git init
>         echo test > test
>         git add --patch test
>         echo "running status, nothing has been added"
>         git status

I think your example makes sense, but nobody ever really tried it before. I use "git add -p" all the time, but almost always when I am adding a new file, I add the whole contents.

I think there are two ways to go about fixing it:
  - in git-add--interactive.perl, the function patch_update_cmd
    explicitly looks at the list of modified files. It would have to
    also check for untracked files, which is easy. But we also need to
    keep track of which files are modified and which are untracked
    through the whole patching procedure, which is a bit more invasive.
  - the recently-added "git add -N" adds an empty file into the index,
    at which point we could add content in the normal way. So:
      git add -N test
      git add -p test
    should just work (but obviously requires two steps from the user).
    You could do something more automatic like the patch below, but I
    think the semantics aren't quite right. If you stage nothing for a
    newly added file, then you still end up with an empty version of the
    staged file in the index. I would expect the semantics to be:
      1. if you stage any content, then the file is added to the index
         with that content
      2. if you stage no content, then the file remains untracked
---
diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index da768ee..72f8a67 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -811,6 +811,12 @@ EOF
 }
 
 sub patch_update_cmd {
+	my @new = list_untracked();
+	if (@new) {
+		system(qw(git add -N), @new)
+			and die "git add reported failure";
+	}
+
 	my @mods = grep { !($_->{BINARY}) } list_modified('file-only');
 	my @them;
 
Previous: Marc WeberNext: Marc Weber
Message 2 of 4 in “git add --patch newfile doesn't add newfile to cache ?”
  1. Marc WeberOct 20, 2008
  2. Jeff KingOct 20, 2008
  3. Marc WeberOct 22, 2008
  4. Jeff KingOct 22, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.