git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* Change behavior of git add --patch on newly added file?
@ 2019-11-08 22:50 Emily Shaffer
  2019-11-09  4:27 ` Junio C Hamano
  2019-11-11  3:41 ` Jeff King
  0 siblings, 2 replies; 7+ messages in thread
From: Emily Shaffer @ 2019-11-08 22:50 UTC (permalink / raw)
  To: git

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2019-11-13  1:47 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-08 22:50 Change behavior of git add --patch on newly added file? Emily Shaffer
2019-11-09  4:27 ` Junio C Hamano
2019-11-09  7:14   ` Junio C Hamano
2019-11-12 18:47   ` Emily Shaffer
2019-11-12 20:16     ` Johannes Schindelin
2019-11-13  1:47     ` Junio C Hamano
2019-11-11  3:41 ` Jeff King

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).