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

Re: Please support add -p with a new file, to add only part of the file

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Jan 9, 2012, 20:47 UTC
Message-ID
<20120109204721.GC23825@burratino>
In-Reply-To
<20120109105134.1239.39047.reportbug@leaf>
(+cc: Wincent who brought us the "add -p" shortcut --- thanks!; Thomas,
 who expanded its scope to git checkout et al; and Jeff, who has done
 some hacking on it)
Hi Josh,
Josh Triplett wrote:
Show 5 quoted lines
> I recently found myself with a new file that I needed to check in part
> of with several commits.  I wanted to use "git add -p newfile" and use
> 'e' to add and commit several times (along with corresponding bits in
> existing files).  However, "git add -p" does not work on a new file,
> only an existing file.

Yep. A workaround is to use "git add -N newfile" before running "git add -p newfile".

I imagine "git add -p '*.c'" should also offer to add hunks from source files that git doesn't know about yet, too.

Here's a quick demo (untested) that might _almost_ do the right thing. Unfortunately it leaves intent-to-add entries around even for files the operator rejects. Anyway, maybe it can be a good starting point for playing around.

Hope that helps, Jonathan

 git-add--interactive.perl |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
diff --git a/git-add--interactive.perl b/git-add--interactive.perl
index 8f0839d2..6e99ff1b 100755
--- a/git-add--interactive.perl
+++ b/git-add--interactive.perl
@@ -1619,6 +1619,10 @@ sub main_loop {
 process_args();
 refresh();
 if ($patch_mode) {
+	if ($patch_mode eq 'stage') {
+		# NEEDSWORK: should use "git update-index --intent-to-add"
+		system(qw(git add --intent-to-add --), @ARGV);
+	}
 	patch_update_cmd();
 }
 else {
-- 
1.7.8.2
Next: Thomas Rast
Message 1 of 7 in “Re: Please support add -p with a new file, to add only part of the file”
  1. Jonathan NiederJan 9, 2012
  2. Thomas RastJan 10, 2012
  3. Junio C HamanoJan 10, 2012
  4. Jeff KingJan 10, 2012
  5. Junio C HamanoJan 10, 2012
  6. Jeff KingJan 10, 2012
  7. Josh TriplettJan 10, 2012

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.