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 in a file?
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 in a file?
git-cola is a great GUI and also has this feature built-in. Just select the lines to stage and press S. If no selection is made, the complete hunk is staged.
You can use
git add --interactive
orgit add -p <file>
, and thengit commit
(notgit commit -a
); see Interactive mode in git-add manpage, or simply follow instructions.Modern Git has also
git commit --interactive
(andgit commit --patch
, which is shortcut to patch option in interactive commit).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, might also have this functionality as well.vim-gitgutter plugin can stage hunks without leaving vim editor using
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
allows visual range selection then
:'<,'>diffput
or:'<,'>diffget
to stage/revert individual line changes.git-meld-index -- quoting from the website:
git-meld-index runs meld -- or any other git difftool (kdiff3, diffuse, etc.) -- to allow you to interactively stage changes to the git index (also known as the git staging area).
This is similar to the functionality of git add -p, and git add --interactive. In some cases meld is easier / quicker to use than git add -p. That's because meld allows you, for example, to:
Usage
In a git repository, run:
You'll see meld (or your configured git difftool) pop up with:
LEFT: temporary directory contining files copied from your working tree
RIGHT: temporary directory with the contents of the index. This also includes files that are not yet in the index but are modified or untracked in the working copy -- in this case you'll see the file contents from HEAD.
Edit the index (right hand side) until happy. Remember to save when needed.
When you're done, close meld, and git-meld-index will update the index to match the contents of the temporary directory on the right hand side of meld that you just edited.
Adding on a previous answer, if you prefer using the command line, entering
git add -e myfile
gives you the choice to choose line by line what you want to commit because this command will open an editor with the differences, like so:As you may known lines that start with
+
are addtions, lines that start with-
are deletions. So:-
with space.
This is what
git add -h
says about adding files this way (patching files):Caution: do not change the content of the file, this is not a good place to do so. Just change the operators of deleted or added lines.
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.