Selectively commit changes

Selectively Commit Changes to a File

Sometimes you're making multiple changes to a file that you don't want to commit together. For example, if you're working on a new feature, but notice some other small things you want to fix. If these changes are in separate files, we can commit each file separately. But what if they’re in the same file?

We can select which chunks and specific lines we want to add to our commit.

We can see which files were changed by opening the Commit tool window ⌘0 (macOS) / Alt+0 (Windows/Linux). Here we can open the diff for a particular file to see which changes were made to that file, using ⌘D (macOS) / Ctrl+D (Windows/Linux). In the diff, we have the option to include specific changes to our commit, by selecting Include into commit in the gutter next to each chunk of modified, deleted or newly added code.

We can even select specific lines from a change to include in a commit. To commit only a specific line from a chunk, right-click the line you want to include and select Split Chunk and Include Current Line into Commit. Alternatively, hover over the gutter and select the checkbox next to the line you want to include in the commit. Or, if we change our mind, we can also hover over the gutter and clear the checkbox next to the line we want to exclude.

Once we have selected all the changes we want to commit, we write a meaningful commit message, and select Commit. Any unselected changes will stay in the current change list, so that you can commit them separately later.

What if we don’t want to add these changes to the same pull request, not even in a separate commit? Maybe you want to do some more cleaning up in your code base, and create a separate pull request for those changes later. We can undo this commit and move these changes to a different change list. To do so, select Move to Another Changelist from the context menu of a modified chunk.

Related Resources

Magic Wand
Use the Magic Wand to Resolve Simple Conflicts
VCS Popup
Open VCS actions using the keyboard
Interactive rebase
Using interactive rebase in IntelliJ IDEA