Micha Wiedenmann
Micha Wiedenmann

Reputation: 20823

Adding part of a hunk interactively with git add --patch

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.

Upvotes: 9

Views: 4088

Answers (2)

avivr
avivr

Reputation: 1523

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.

Upvotes: 6

Micha Wiedenmann
Micha Wiedenmann

Reputation: 20823

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.

Upvotes: 18

Related Questions