tags:

views:

1132

answers:

5

When I commit changes to a file in git, how can I commit only some of the changes?

Example: commit only 15 lines out of 30 changed lines.

+30  A: 

you can do git add -p filename.x it'll ask you what you want to stage, you can then hit s to split whatever change into smaller chunks. Then hit y to stage that chunk, and d to exit or go to the next file.

cloudhead
+9  A: 

git gui provides this functionality under the diff view. Just right click the line(s) you're interested in and you should see a "stage this line to commit" menu item.

Ionuț G. Stan
+11  A: 

You can use "git add --interactive" or "git add -p <file>", and then "git commit" (not "git commit -a"); see "Interactive mode" in git-add manpage, or simply follow instructions.

If you prefer doing it from GUI, you can use git-gui. You can simply mark chunks which you want to have included in commit. I personally find it easier than using "git add -i". Other git GUIs, like QGit or GitX, can also have this functionality.

Jakub Narębski
Jakub, it was git gui indeed, not gitk. Thanks for correction.
Ionuț G. Stan
A: 

Should you use emacs, take a look at Magit, which provides a git interface for emacs. It supports staging hunks (parts of files) quite well.

Mark van Lent
+1  A: 

For emacs there is also gitsum

zut