360

Possible Duplicate:
How can I commit only part of a file in git

How do I commit a few specific line ranges from a file to git? while ignoring some other line changes in the same file.

Community
  • 1
  • 1
erikvold
  • 15,988
  • 11
  • 54
  • 98

2 Answers2

629

Try git add -p -- it will interactively let you add, skip, or split diff hunks.

Adam DiCarlo
  • 6,655
  • 1
  • 17
  • 9
  • 51
    When I do this, git chooses what constitutes a hunk of change. For instance, I changed a file in three places, but it only gives me two chunks as options to commit: one containing the first two changes, and one for the third change. Is there a way to specify actual line numbers, as the OP asked? – Nathan Long Dec 27 '11 at 21:11
  • 21
    @NathanLong `git add -p` lets you break up the hunk into smaller hunks. See the 'a' option. If that doesn't work then maybe the 'e' option will. See the "Interactive mode" section of http://www.kernel.org/pub/software/scm/git/docs/git-add.html – ThomasW Jul 31 '12 at 06:03
  • 44
    @ThomasW - I think you mean 's' not 'a' – asgeo1 Aug 01 '12 at 07:20
  • 13
    @asgeo1 yes, you're right, I meant 's'. – ThomasW Aug 01 '12 at 07:24
  • 2
    I found a good tutorial for anyone getting started with `git add -p` or using the 'e' option for the first time: http://johnkary.net/blog/git-add-p-the-most-powerful-git-feature-youre-not-using-yet/ – nickiaconis Oct 12 '15 at 23:53
  • 56
    To clarify the above, once a hunk is displayed, if you press 's', it will break it into smaller hunks. – Brian Fegter Jan 12 '16 at 17:12
  • 2
    To get help/ explenation of the choices press `?` then enter as your first command. – Ole Henrik Skogstrøm Mar 16 '17 at 09:52
  • IIRC, does not work on files that are not added yet. – Nakilon May 05 '18 at 13:55
  • In that case you need `-N`: https://stackoverflow.com/a/27321496/322020 – Nakilon May 06 '18 at 04:13
  • 1
    Is there a mode analogous to git-rebase, i.e. to just open whole diff in editor, and mark the lines I want to commit with, say, `a`? – Hi-Angel Aug 17 '18 at 13:04
  • 5
    It looks like there's a limit to the granularity of the hunks ('Sorry, cannot split this hunk'). Does anyone know of a way to split a hunk that contains multiple lines? I have a print statement sandwiched between meaningful changes that I'd like to omit. – poff Mar 13 '20 at 17:37
  • 5
    Edit to my comment above: you can type 'e' to open up an editor and modify the hunk manually. No idea why this is a separate interface. Seems like you should be able to just select line-by-line instead of git deciding for you what an atomic change is. – poff Mar 13 '20 at 17:45
  • 2
    And after open editor with option 'e', you just need to comment (using #) to not adding this lines. – MikeDoouglas Oct 01 '21 at 11:37
37

Use git add -i to stage the lines then commit as normal, or use git-cola until you get used to the command line.

Staging lines of a file

Anthony Raymond
  • 7,434
  • 6
  • 42
  • 59
Peter Coulton
  • 54,789
  • 12
  • 54
  • 72