When I make changes to a file in Git, how can I commit only some of the changes?
For example, how could I commit only 15 lines out of 30 lines that have been changed
vim-gitgutter plugin can stage hunks without leaving vim editor using
:GitGutterStageHunk
Beside this, it provides other cool features like a diff sign column as in some modern IDEs
If only part of hunk should be staged vim-fugitive
:Gdiff
allows visual range selection then :'<,'>diffput or :'<,'>diffget to stage/revert individual line changes.
As one answer above shows, you can use
git add --patch filename.txt
or the short-form
git add -p filename.txt
... but for files already in you repository, there is, in s are much better off using --patch flag on the commit command directly (if you are using a recent enough version of git):
git commit --patch filename.txt
... or, again, the short-form
git commit -p filename.txt
... and then using the mentioned keys, (y/n etc), for choosing lines to be included in the commit.
For emacs there is also gitsum
For Atom users, the package github includes interactive staging, in the style of git gui. For shortcuts see the package's documentation.
Using Atom allows working with a theme that has dark background (by default, git gui has a white background).
If you are using vim, you may want to try the excellent plugin called fugitive.
You can see the diff of a file between working copy and index with :Gdiff, and then add lines or hunks to the index using classic vim diff commands like dp. Save the modifications in the index and commit with :Gcommit, and you're done.
Very good introductory screencasts here (see esp. part 2).