Can I modify git-add's hunk size?
Asked Answered
M

3

30

I have a file that I've been working on and then realized that I had a mistake in it a few lines above where I've been working. I quickly fixed the mistake and want to commit it before I commit the rest of my work. Great, this is where git add --patch comes in!

Except, I'm being presented with only one hunk incorporating both changes. Is it possible to manually tell git that I want two hunks?

Morganstein answered 13/7, 2009 at 21:34 Comment(3)
Do you mean: "How do I decrease the default hunk size?"Counterpressure
@Counterpressure whoa, recursive link!Astronomy
Thank you for the helpful link, @Geremia, just what I was looking for. Git seems to think my terminal has more lines than it does, and you can’t scroll in patch mode.Amy
L
51

In addition to 'y' and 'n', one of the answers you can give when it asks you about a hunk is 's', for 'split this hunk in to smaller hunks'. The full list:

y - stage this hunk
n - do not stage this hunk
q - quit, do not stage this hunk nor any of the remaining ones
a - stage this and all the remaining hunks in the file
d - do not stage this hunk nor any of the remaining hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
Lizliza answered 13/7, 2009 at 21:36 Comment(4)
If you really need control try 'e' to edit the patch directly. This way you can even change the patch contents (not just select parts of the patch).Boyer
TLDR: To get smaller hunk, I use s.Ashe
when i press s, it just keeps giving me the above list. Not sure what part of "s" it doesn't get.Astronomy
s doesn't work if I want to split 2 adjacent lines into 2 hunksLille
Y
13

git gui will allow you to commit single lines, even if they are surrounded by other modified lines you do not want to commit.

Yolk answered 14/7, 2009 at 6:57 Comment(1)
You can do the same with git cola, too.Soleure
H
0

With Git 2.25 (Q1 2020), The effort to move "git-add--interactive" Perl script to C continues.

As a result, the hunk splitting feature (the one accessed with the 's' key) will change.

See commit 2e40831, commit 54d9d9b, commit ade246e, commit d6cf873, commit 9254bdf, commit bcdd297, commit b38dd9e, commit 11f2c0d, commit 510aeca, commit 0ecd9d2, commit 5906d5d, commit 47dc4fd, commit 80399ae, commit 7584dd3, commit 12c24cf, commit 25ea47a, commit e3bd11b, commit 1942ee4, commit f6aa7ec (13 Dec 2019) by Johannes Schindelin (dscho).
(Merged by Junio C Hamano -- gitster -- in commit 45b96a6, 25 Dec 2019)

built-in add -p: implement the hunk splitting feature

Signed-off-by: Johannes Schindelin

If this developer's workflow is any indication, then this is the most useful feature of Git's interactive addcommand.

Note: once again, this is not a verbatim conversion from the Perl code to C: the hunk_splittable() function, for example, essentially did all the work of splitting the hunk, just to find out whether more than one hunk would have been the result (and then tossed that result into the trash).
In C, we instead count the number of resulting hunks (without actually doing the work of splitting, but just counting the transitions from non-context lines to context lines), and store that information with the hunk, and we do that while parsing the diff in the first place.

Another deviation: the built-in git add -p was designed with a single strbuf holding the diff (and another one holding the colored diff, if that one was asked for) in mind, and hunks essentially store just the start and end offsets pointing into that strbuf.
As a consequence, when we split hunks, we now use a special mode where the hunk header is generated dynamically, and only the rest of the hunk is stored using such start/end offsets. This way, we also avoid the frequent formatting/re-parsing of the hunk header of the Perl version.

Holly answered 29/12, 2019 at 21:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.