pr1001
pr1001

Reputation: 21962

Can I modify git-add's hunk size?

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?

Upvotes: 31

Views: 6239

Answers (3)

VonC
VonC

Reputation: 1323055

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.

Upvotes: 0

Bombe
Bombe

Reputation: 83847

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

Upvotes: 13

Jim Puls
Jim Puls

Reputation: 81052

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

Upvotes: 52

Related Questions