Why is split option missing in git add -p?
Asked Answered
G

2

34

Trying to split a hunk into smaller ones by git add -p and split option, but entire file appears as one hunk and I can't split it.

  1. I can edit, but removing lines causes the patch to fail.
  2. git help add says I should have split, and I recall using it, but in my current example the option doesn't appear in the prompt.
  3. When I choose the option help is printed, which says I can choose s to split :-[

Git version: 1.9.1. Xubuntu 14, oh-my-zsh. The hunk is not one line, it's multiple lines. Any ideas why would that be?

Edited with more data, here's console log:

+last line of long text 
Stage this hunk [y,n,q,a,d,/,e,?]? s
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 hunk and all later hunks in the file
d - do not stage this hunk nor any of the later 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
Geary answered 25/6, 2016 at 15:12 Comment(3)
I believe you have to enter interactive mode (-i) first to use split.Infant
Tried it out right away, but sadly - same effect. IIRC git add -p is just bypassing the original window for choosing options, it jumps straight to patching. Docs say the same (checked now). Thanks nevertheless.Geary
That will be easier with Git 2.17 (Q2 2018), which will display a more precise help message. See my answer below.Auliffe
V
37

The reason it's not working is because your hunk is already as small as git will automatically get it. Split only works when there are non-changed lines separating changed lines close enough that git assumes they belong together.

In order to achieve what you want, you will need to manually edit the patch

Vinegarish answered 25/6, 2016 at 15:27 Comment(1)
Aahh! Non-changed lines separating changed ones, indeed. In that case I'll just split content without git and have it added in parts. /me thanks!Geary
A
5

Why is split option missing in git add -p?

You won't have to ask this question with Git 2.17 (Q2 2018), since the single-key help is now given only for keys that are enabled (e.g. help for '/' won't be shown when there is only one hunk).

See commit 4bdd6e7, commit 88f6ffc, commit 01a6966 (13 Feb 2018) by Phillip Wood (phillipwood).
(Merged by Junio C Hamano -- gitster -- in commit 60f8b89, 06 Mar 2018)

First:

add -p: improve error messages

If the user presses a key that isn't currently active then explain why it isn't active rather than just listing all the keys. It already did this for some keys, this patch does the same for the those that weren't already handled.

And:

add -p: only display help for active keys

If the user presses a key that add -p wasn't expecting then it prints a list of key bindings.
Although the prompt only lists the active bindings the help was printed for all bindings.

Fix this by using the list of keys in the prompt to filter the help.
Note that the list of keys was already passed to help_patch_cmd() by the caller so there is no change needed to the call site.

Auliffe answered 10/3, 2018 at 23:58 Comment(2)
Upvoted! I'll leave accepted as it is for I believe right now more folks will benefit from it. Thank you @Auliffe nevertheless!Geary
@LIttleAncientForestKami I agree: the accepted answer is still the right one.Auliffe

© 2022 - 2024 — McMap. All rights reserved.