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?
You can use:
git add --patch <filename>
or for short:
git add -p <filename>
Git will break down your file into what it thinks are sensible "hunks" (portions of the file). It will then prompt you with this question:
Stage this hunk [y,n,q,a,d,/,j,J,g,s,e,?]?
Here is a description of each option:
+/- by # (thanks veksen)If the file is not in the repository yet, you can first do git add -N <filename>. Afterwards you can go on with git add -p <filename>.
Afterwards, you can use:
git diff --staged to check that you staged the correct changesgit reset -p to unstage mistakenly added hunksgit commit -v to view your commit while you edit the commit message.Note this is far different than the git format-patch command, whose purpose is to parse commit data into a .patch files.
Reference for future: Git Tools - Interactive Staging
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.
Modern Git has also git commit --interactive (and git 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.
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.
If you're using VS Code, you're in luck. Select the range that you want to stage, then use Git: Stage Selected Ranges to stage them, and commit if you want.
I recorded a gif to demonstrate what I meant:
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.
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):
added content Added content is represented by lines beginning with "+". You can prevent staging any addition lines by deleting them.
removed content: Removed content is represented by lines beginning with "-". You can prevent staging their removal by converting the "-" to a " " (space).
modified content: Modified content is represented by "-" lines (removing the old content) followed by "+" lines (adding the replacement content). You can prevent staging the modification by converting "-" lines to " ", and removing "+" lines. Beware that modifying only half of the pair is likely to introduce confusing changes to the index.
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.
It's been 10 years since this question was asked. And I hope this answer will be useful to someone. As mentioned in the answer here, where GUI is not an option, Andrej Shadura's git-crecord tool helps bring an interactive window in which we can select the lines to commit.
Set up the extension as follows:
git clone https://github.com/andrewshadura/git-crecord
cd git-crecord
./setup.py install
ln -s $PWD/git-crecord ~/.local/bin/git-crecord
cd to your git repo and invoke it as follows:
git crecord
This would bring up an interactive interface which can be used as shown below. Pressing the following keys will do certain actions:
f hunk toggle fold (arrow keys can also be used)
space toggle hunk selection
a toggle commit or amend
c confirm and open commit window
Screencast showing a sample usage
I want to add lazygit to the list of tools. It's a nice command-line gui (i.e., works through ssh even if X forwarding is not allowed). It has extensive functionalities (e.g., select lines to commit, interactive rebase), helpful coloring, and is relatively simple to use. Can install in a variety of ways (go, conda, package manager,...). Still actively developed/maintained.
You can use Eclipse IDE, each modified local files can be compared vs the staged area and in this side by side diff view, you are free to choose which hunk to copy from local to staged area, or in the opposite, rollback local change from the staged area.
But there's more :), all graphical git clients do that hunk staging too (git add -p), but none I know can do that : directly edit in the staged area so several changes on same line or same hunk can be more fined picked up (or even write/delete/update stuff that never exists locally), like a "git add -e" working at patch level but without the risk to break the patch content. Moreover since it's with the regular text editor in the diff view, you have syntax color to help and you can do text replace operations (change all indentation types, rename a local variable use in many place, ...) to start committing separately some reformat / refactor before committing your "real" change but without embedding all file changes in the same commit.
Sure Eclipse is more Java oriented but because of its powerful git staging editor, it can be used for other language too. There is an Eclipse based free product only focusing git operation called GitEye : https://www.collab.net/downloads/giteye but seems not maintained so better to use a basic Eclipse distrib like : https://www.eclipse.org/downloads/packages/release/2020-12/r/eclipse-ide-java-developers
Edit : since IntelliJ 2020.3 allows to work with real git index, it is now capable to edit directly the staged area like Eclipse
From the comments of this 2018 answer:
It's a pity that Visual Studio doesn't have this option.
About 10 years ago the alternative source control system Mercurial supported this feature inside of Visual Studio. I really miss it since I use Git.
It is now (Aug. 2022) supported:
Git Line-staging Released!
We are excited to announce the release of Line-staging support in Visual Studio 2022.
Line-staging, a.k.a. interactive staging, enables you to split your changed lines of code across different commits.
Line-staging could also be utilized in reviewing your changes before committing them. Mark your changed lines or sections of code as reviewed by staging them and commit your staged changes when you are done.Start using Line-staging by updating your Visual Studio 2022 to version 17.3 or later.
Read our Line-staging documentation to learn more about how to use and customize this feature.