Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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.

like image 513
Micha Wiedenmann Avatar asked Mar 05 '13 16:03

Micha Wiedenmann


People also ask

What does git add patch do?

git add -p is short for git add --patch and it is a git option that allows you to make more specific commits. How it works is that it will go through all new changes in your code and display hunks of them at a time for you to decide what you would like to stage or not stage.

How do I add files to patch?

To make the untracked files visible to the git diff command, we staged them (using git add ) and then used the following command to create the patch: git diff --patch --staged; git diff [--options] --cached [<commit>] [--] [<path>...]


1 Answers

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.

like image 179
Micha Wiedenmann Avatar answered Sep 21 '22 15:09

Micha Wiedenmann