387

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.

4
  • 2
    SourceTree makes it much easier and less confusing Commented Jun 23, 2015 at 13:23
  • SourceTree doesn't let me pick lines only hunks. Often I can't split a hunk down to the lines I want Commented Feb 22, 2017 at 16:52
  • 1
    This questions asks for specific lines, I would like to provide them as numbers. Linked question is different that this one. Commented Dec 13, 2020 at 16:54
  • 1
    For most cases git add --patch might suffice, but for control over precisely which lines are added, here's what worked nicely for me. Commented Mar 5, 2024 at 22:48

2 Answers 2

673

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

Sign up to request clarification or add additional context in comments.

14 Comments

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?
@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 kernel.org/pub/software/scm/git/docs/git-add.html
@ThomasW - I think you mean 's' not 'a'
@asgeo1 yes, you're right, I meant 's'.
To clarify the above, once a hunk is displayed, if you press 's', it will break it into smaller hunks.
|
40

Use git add -i to stage the lines then commit as normal, or use git-cola.

Staging lines of a file

1 Comment

Or git gui, as mentioned by this answer of the duplicate question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.