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

Change behavior of git add --patch on newly added file?

From
Emily Shaffer <emilyshaffer@google.com>
Date
Nov 8, 2019, 22:50 UTC
Message-ID
<20191108225035.GA60198@google.com>

Should 'git add -p <newly-added-file>' do the same thing as 'git add -N <newly-added-file && git add -p <newly-added-file>'?

To demonstrate:
  git init .
  printf "%s\n" {1..10} >newfile
  git add -p newfile

This outputs "No changes." 'git status' shows that 'newfile' is untracked.

However, since I want each of my commits to be atomic, I want only lines 1-5 in my first commit. (Or more realistically, maybe I only want function stubs.)

I *can* do:
  git add -N newfile
  git add -p newfile

But, why doesn't 'git add -p' just do that on its own? At the very least, "No changes" is a pretty cryptic output - there ARE changes, right here in my workspace! (I think it means "there isn't a 'newfile' in the index, so we can't say whether there is a difference between 'newfile' in the index and 'newfile' in your working tree".)

And if I reason to myself, "I can only add --patch a file which is tracked, so I need to track this file first" and go skimming through the documentation for "git add", -N doesn't jump out very much:

  -N, --intent-to-add   record only the fact that the path will be added later
or,
  -N, --intent-to-add
      Record only the fact that the path will be added later. An entry for the path is placed in
      the index with no content. This is useful for, among other things, showing the unstaged
      content of such files with git diff and committing them with git commit -a.

Considering that other parts of the add documentation talk about tracked or untracked files, I personally miss the upshot that we are tracking a file which was previously untracked with '-N'. (My gut guess is that while many Git users are familiar with "tracked" or "untracked" but less Git users are familiar with what it means to "place in the index".)

Interestingly, in the whole-file deletion case, I *do* get interactive support....kind of.

  rm trackedfile
  git add -p trackedfile
  diff --git a/trackedfile b/trackedfile
  index f00c965..0000000
  --- a/trackedfile
  +++ /dev/null
  deleted file mode 100644
  @@ -1,10 +0,0 @@
  -1
  -2
  -3
  -4
  -5
  -6
  -7
  -8
  -9
  -10
  (1/1) Stage deletion [y,n,q,a,d,?]? e
  Sorry, cannot edit this hunk

Is there a reason that git add -p can't do whole-file support this way? While I'm less sure about what I'd like to see for copied files, I do feel like there's a strong argument for patch adding new or deleted files.

 - Emily
Next: Junio C Hamano
Message 1 of 7 in “Change behavior of git add --patch on newly added file?”
  1. Emily ShafferNov 8, 2019
  2. Junio C HamanoNov 9, 2019
  3. Junio C HamanoNov 9, 2019
  4. Emily ShafferNov 12, 2019
  5. Johannes SchindelinNov 12, 2019
  6. Junio C HamanoNov 13, 2019
  7. Jeff KingNov 11, 2019

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.