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.
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.
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.
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.
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.
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.