How can I commit some changes to a file, but not others, in VSCode?

Visual Studio-Code

Visual Studio-Code Problem Overview


VSCode has excellent git integration but I can't find a way to do partial commits. Basically, I want to be able to select specific changes in my files and stage them without staging the entire file. This functionality is supported by the git CLI, Github native apps, and Atom plugins.

Mostly making sure I'm not just missing something before opening a ticket for it.

Visual Studio-Code Solutions


Solution 1 - Visual Studio-Code

  • Open up the Source Control view, then click a changed file to open the comparison.

  • Select the lines you want to stage.

  • Click '...' then Stage Selected Ranges.

enter image description here

Solution 2 - Visual Studio-Code

> ⚡ Summing up everything in this post with a gif.

  1. Make sure you're in the source control view to use this feature
  2. Select the range of code
  3. Open ··· at the top-right corner of the editor (not the ··· at the top of the git panel on the left)
  4. You can also press COMMAND (⌘) + K + COMMAND (⌘) + S to open keyboard shortcuts and search for range and then set a keyboard shortcut for this action.

VSCode Range Select Commit

Peace! ✌️

Solution 3 - Visual Studio-Code

Update: October 2017: In the latest VSCode as of October 2017 (version 1.18) you can actually stage specific lines really easy just like you would do it with the git CLI git add -p. You just have to click on the edited line from the side like, watch the diff and apply it or not.

View pending changes and perform partial commits in the editor

Solution 4 - Visual Studio-Code

This option has been moved to the palette:

shift+command+P and type "stage selected ranges"

Solution 5 - Visual Studio-Code

initially, I've made an overview of available methods, but I think this bit should be on top since it's the most useful one:

What I've came up with to make it quicker

As one can see, "Git: Stage Selected Ranges" shown by Command Palette (ctrl + shift + P) for the main editor has the same hotkey as in the "review changes" interface, but it doesn't actually work. For some time, I thought this is a bug, but it turned out to be a configurable behavior: click the settings button:

enter image description here

(or just go ctrl + K ctrl + S/file → preferences → keyboard shortcuts and search for @command:git.stageSelectedRanges)

...and you'll see that by default the "When" expression is isInDiffEditor, so it is not supposed to work in the normal editor. So, change When expression to empty:

enter image description here

And either keep the default hotkey combination and use it in the editor (that's what I did) or set it to something shorter (not a double-combination, I'll consider this too). Profit!

If something's unclear or you want to know other options, here's

The initial overview of available methods

  • basic method is to Stage Selected Ranges in the interface for reviewing changes:

    enter image description here

    1. open Source Control (ctrl + shift + G)
    2. select file
    3. select lines that should be staged (or put a cursor for a single line or lines before and after deleted bits)
    4. open the "..." menu and select "Stage Selected Ranges"
  • on the other hand, you can use hotkeys. Default ones (in v1.56.2) are ctrl + K and then ctrl + alt + S. To set custom key combination, open Keyboard shortcuts (either from File → Preferences or via ctrl + K then ctrl + S) and find stageSelectedRanges (and may be unstageSelectedRanges, revertSelectedRanges)

  • unfortunately, hotkeys mentioned above work only in the "review changes" interface, and you may want to do this in the code editor directly. You can do this via another interface: click the change label:

    enter image description here

    and then the "stage change" button:

    enter image description here

  • despite there's no direct hotkeys to stage selected range from code editor, you can go ctrl + shift + P (open Command Palette) and then type "stage s" and choose "Git: Stage Selected Ranges":

    enter image description here

Also, it's handy to use Alt + F5 to jump to the next change in the file when you review what to stage.

Solution 6 - Visual Studio-Code

Use Interactive staging.

command - git add -p this will show you the file, and you can choose which one you want to commit after parting the changes

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRayneView Question on Stackoverflow
Solution 1 - Visual Studio-CodeBenjamin PaseroView Answer on Stackoverflow
Solution 2 - Visual Studio-CodeAhmad AwaisView Answer on Stackoverflow
Solution 3 - Visual Studio-CodeKonstantinos LeimonisView Answer on Stackoverflow
Solution 4 - Visual Studio-CodeDimitrisView Answer on Stackoverflow
Solution 5 - Visual Studio-CodeYakovLView Answer on Stackoverflow
Solution 6 - Visual Studio-CodeAyushri AroraView Answer on Stackoverflow