21

I usually add changes with git add -p, and many times there are large hunks with several blocks of code, separated by blank lines.

However, git won't split the hunk any further, and I have to resort to manual editing.

How can I increase the granularity of the hunks, such that every block of code will be in a separate hunk?

Edit: This is a different question from Git: show more context when using git add -i or git add -e?, since I'M not looking to increase the context around each hunk, but rather increase the number of hunks.

Community
  • 1
  • 1
dimid
  • 7,285
  • 1
  • 46
  • 85

3 Answers3

22

It cant be done,

These are the options you can do within add -p:

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

Once you use the s it will pick the chunk of code which can be considered as a standalone change. If you want to split it even more you will have to use the e to edit the hunk and then add it back to the stage area.

Summary:

To split hunks you use the s flag.
If you need to split it into even smaller chunks you will need to manually edit it using the e option.

enter image description here

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • 1
    Thx, but I'M well aware of how `add -p` works, and I'M looking for a workaround without resorting to manual editing. – dimid Jan 05 '16 at 11:45
  • 3
    @dimid: The answer is "can't be done", and I'm afraid you insisting you want to do it won't help. – Marcus Müller Jan 05 '16 at 11:46
4

As @codeWizard said in his answer, git just doesn't support what you want, and it hence can't be done in git add.

Now, you could write a script yourself that does the following:

  1. copy your locally changed version of to somewhere outside the tree
  2. git checkout -- <changedfile> to bring your modified file to an unmodified state
  3. write a user interface of your own to select ranges in the file that you want to add now
  4. add the modifications to the in-tree .
  5. git commit
  6. If desirable, go to 3. again
Community
  • 1
  • 1
Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
2

git gui lets you select specific lines to stage.

Open git gui in your git repository, then right click on the desired line > "Stage Line For Commit".

Now, it will not let you edit a file (I wish it could open a temporary file in your favorite editor). To work around this, you can simply mark the commit message with a reminder:

AMENDME: Add x
do not forget to rename thing

When you are done with your coding stretch, you can use git rebase -i <original branch>, mark the "AMENDME" commit with the edit command and amend that commit.

sleblanc
  • 3,821
  • 1
  • 34
  • 42
  • 1
    I strongly disagree. My development platform cannot be simpler: a text editor (emacs) and the git command line utility. I sometimes use `git gui` et al. to review and work out hairier commits; when I spot a typo, it's a bit of a pain to go back to my editor just to correct it, when it could do that for me. – sleblanc Mar 03 '16 at 18:23
  • I don't see the point in committing and amending. It is lengthy. – TUSqasi Apr 04 '21 at 13:26