28

I have a source file where 2 features have been added. In order to allow cherry-picking, I'd like to commit that in 2 phases: one for each feature. Until now, in similar situations, using git add -p served me well, to commit one feature while leaving the local files in their final stage.

However, I now have the problem that git add -p wants to stage a hunk that includes edits for both features. Even though the edits are on separate lines, s (for "split") no longer wants to split up the hunk into smaller pieces...

In short: I can't separate the changes for the 2 features this way. Is there a way to manually edit the patch, for example using vi, without actually changing the original file?

bart
  • 7,640
  • 3
  • 33
  • 40
  • 1
    Part of my problem is that I probably can't think of the right terms, to search for in Google. – bart Feb 25 '10 at 12:21
  • You can find detailed instructions on manually editing patches at [the official git add docs - EDITING PATCHES](https://www.kernel.org/pub/software/scm/git/docs/git-add.html#_editing_patches). –  Jul 14 '13 at 19:15

5 Answers5

28

As Alan says, edit the patch by pressing e (instead of s) during git add -p. This will launch your editor with that hunk of the patch so that you can manually edit it. There are comments within the text that explain how to properly discard modifications and it's actually pretty easy.

When you are done, note that you can test it with only the changes you've just added by doing git stash --keep-index. The changes you did not add to the index will be stashed away and you are now free to test just the changes that you are about to commit. When done, simply git stash pop or git stash apply to get the other changes back.

Dan Moulding
  • 211,373
  • 23
  • 97
  • 98
  • 1
    If you find yourself doing `git stash --keep-index` often, `git stash -k` will do the same, and save you some time. – Ryan Feb 13 '12 at 19:46
6

Like other people have said, you can use e to edit the hunk you want to split up.

To only add a portion of the hunk, you can delete the lines from the change you want to split out.

+Line 1
+Line 2
+Line 3

Lets say you want to keep Line 1 and Line 3 in one commit and Line 2 in another. All you have to do is delete Line 2:

+Line 1
+Line 3

This will put Line 1 and Line 3 in your staging area. Line 2 will still be indexed but not staged.

Kousha
  • 1,575
  • 14
  • 18
5

You can edit the patch by pressing e during a git add -p. It will not affect the original file.

Alan Haggai Alavi
  • 72,802
  • 19
  • 102
  • 127
  • git replies with `Huh (e)?`. Maybe my version is a bit old? (Though I only have had it for 2 weeks and it's the official port for CentOS...) – bart Feb 25 '10 at 12:24
  • Correction, wrong phase of `git add -i`. At the prompt for what to do with the hunk, git simply ignores my "e". – bart Feb 25 '10 at 12:26
  • "e" does nothing. git --version says 1.5.5.6. Is that too old? – bart Feb 25 '10 at 12:54
  • Even though my version is just 8 months old, comparing the man pages for git-add, apparently it is... yet it is the most recent version on [EPEL](http://download.fedora.redhat.com/pub/epel/5/i386/repoview/git.html)... :( – bart Feb 25 '10 at 13:11
  • Sheesh apparently support for "e" was added in git 1.6.0 (http://kernel.org/pub/software/scm/git-core/docs/RelNotes-1.6.0.txt)... – bart Feb 25 '10 at 13:25
  • FWIW, I think 1.5.5.6 was released on 20 December 2008. Still, I'm surprised to learn that editing ability was only added in 1.6.0. Seems like I've been doing it forever... – Dan Moulding Feb 25 '10 at 14:02
  • Oops, I had parsed the release date as 12th of June, 2009 ("6" was the time of day, apparently) – bart Feb 25 '10 at 23:47
2

There are Git GUIs that will let you select individual lines that you want to stage, allowing you to split lines that you normally wouldn't be able to using the regular git add --patch from the command line.

Two such GUIs are:

  1. SourceTree.
  2. Git Cola.
0

I usually get merge conflicts from the git stash pop described in @Dan's answer. See git stash and edited hunks for a solution that avoids the conflicts.

Community
  • 1
  • 1
ntc2
  • 11,203
  • 7
  • 53
  • 70