9

How can I add only some lines/a part of a hunk interactively with git add --patch, if hunk shown after selecting split is still too large?

@@ -2,9 +2,17 @@ Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed
 diam nonumy eirmod tempor invidunt ut labore et dolore magna
 aliquyam erat, sed diam voluptua. At vero eos et accusam et
 justo duo dolores et ea rebum. Stet clita kasd gubergren, no
+Ut wisi enim ad minim veniam, quis nostrud exerci tation
+ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo
+consequat. Duis autem vel eum iriure dolor in hendrerit in
+vulputate velit esse molestie consequat, vel illum dolore eu
+feugiat nulla facilisis at vero eros et accumsan et iusto
+odio dignissim qui blandit praesent luptatum zzril delenit
+augue duis dolore te feugait nulla facilisi.
 sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem
 ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
 nonumy eirmod tempor invidunt ut labore et dolore magna
 aliquyam erat, sed diam voluptua. At vero eos et accusam et
 justo duo dolores et ea rebum. Stet clita kasd gubergren, no
 sea takimata sanctus est Lorem ipsum dolor sit amet.

Update

As SO user avivir correctly notes "split", might be a valid option.

Community
  • 1
  • 1
Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137

2 Answers2

18

If you want to add only a part of a hunk, you can select edit from the menu that gets displayed:

Stage this hunk [y,n,q,a,d,/,e,?]?

This will start your editor where you can delete unwanted lines from the hunk that is about to be added to the index. The change is virtual, that is your file will not change.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
  • there should also be a "split this hunk" option, which makes that much easier (the hunk can be split down to single lines) – Nevik Rehnel Mar 05 '13 at 16:40
  • 1
    This is a useful reference: [What does each of the y,n,q,a,d,/,K,j,J,g,e,? stand for in context of git -p](http://stackoverflow.com/questions/10605405/what-does-each-of-the-y-n-q-a-d-k-j-j-g-e-stand-for-in-context-of-git-p) – AJP Mar 30 '17 at 10:25
6

Use the 's' (split) option of git add --patch.

This will cause git to split it into smaller hunks. You'll then receive a menu for each new hunk (which is a part of the original), where you can select to stage it, split it even further, etc.

avivr
  • 1,393
  • 15
  • 28