28

If I made two logical changes, which is continuous in code. Git shows it as one single hunk while adding interactively.

Is there a way to specifically add only few lines in a hunk, during add --patch?

Sathish Manohar
  • 5,859
  • 10
  • 37
  • 47
  • 3
    This question possibly duplicates http://stackoverflow.com/questions/2333828/splitting-up-a-git-commit-into-phases-how-to-manually-intervene . The answer to that question adds additional useful detail, too (the `git stash --keep-index` trick). – Adam Monsen Mar 20 '12 at 19:34

1 Answers1

45

The git add --patch mode has option for splitting an individual hunk or editing an individual hunk.

git add --patch:

s - split the current hunk into smaller hunks
e - manually edit the current hunk

Wouldn't that solve your issue regarding your continuous set of lines?

After adding partials to commit, User should use ONLY git commit to commit, using git commit -a or using commit with all files flag ignores added partial and commits all staged files.

Sathish Manohar
  • 5,859
  • 10
  • 37
  • 47
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    Note: For me, it was not enough to commit via `git commit` (no `git commit -a`) after adding only some hunks to the index. Even though `git diff` and `git diff --cached` were giving the expected output, as well as `git status`, issuing `git commit` added all the changes altogether to the commit. I had to use `git stash --keep-index` before, to stash the other changes temporarily, to commit partially, and then `git stash pop` to add them back to the working copy. Msysgit 1.7.11 @ WinXP. YMMV. – jakub.g Oct 08 '12 at 12:07