397

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.

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
Rayne
  • 31,473
  • 17
  • 86
  • 101
  • 1
    I wish 1) their next hunk shortcut (Alt + F5) would go across files related: https://github.com/microsoft/vscode/issues/24389 | https://github.com/microsoft/vscode/issues/50434 and 2) that there was a way to add hunks without the need to select lines when you want the entire hunk https://github.com/microsoft/vscode/issues/48323 | https://github.com/Microsoft/vscode/issues/13740 – Ciro Santilli OurBigBook.com Dec 24 '22 at 11:57

6 Answers6

616
  • 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 (in the ··· at the top-right corner of the editor, not the ··· at the top of the git panel on the left).

enter image description here

Natacha
  • 1,132
  • 16
  • 23
Benjamin Pasero
  • 113,622
  • 14
  • 75
  • 54
  • 137
    Just in case someone else gets confused like I did: this screenshot refers to the `···` at the top-right corner of the editor, not the `···` at the top of the git panel on the left. – waldyrious Jan 06 '17 at 10:12
  • 10
    If this returns you an error "*Git: Illegal value for `line`*", this has been reported a couple of times on GitHub, but still not fixed. In that case, just toggle "Inline view" and it'll work. [Source](https://github.com/Microsoft/vscode/issues/35705#issuecomment-343638025) – Marc.2377 Apr 29 '18 at 00:21
  • 3
    As mentioned by @Dimitris, this command is also in the Command Palette, so you don't need to be in Source Control view. – eatcrayons Dec 05 '18 at 20:25
  • 3
    Be aware, this stages whole lines, if you want to stage portions of a line, I guess it's back to `get add -i` and interactive editing... – Zarepheth Mar 10 '20 at 17:20
  • 3
    This doesn't work, nothing happens when clicking on 'Stage selected ranges'. – Lee Apr 08 '21 at 11:59
  • is there any method to lock the file so that it never STAGE – elliotching Jun 24 '21 at 06:40
  • @Lee If you select lines in the old content (the left side of the editor), nothing happens. – umitu Oct 29 '21 at 06:28
  • 1
    For DELETED lines, you need to select the lines above and below the deleted line(s) for this to work since you can't select deleted lines ...since they are deleted, LOL :-D – Mycah Dec 17 '21 at 19:22
289

⚡ 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! ✌️

Ahmad Awais
  • 33,440
  • 5
  • 74
  • 56
90

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

  • 11
    There are two things missing though... You can't split hunks and you can't manually edit hunks... :( – Robert Koritnik Feb 14 '18 at 16:28
  • @RobertKoritnik: It's possible like this : https://stackoverflow.com/a/51972071/6419007 And it's actually very convenient. If you just need some part of a hunks, you can select the corresponding lines. – Eric Duminil Feb 18 '23 at 13:41
  • @EricDuminil Not sure how long have you been using VSCode, but at the time of writing of my comment (and the original question), that wasn't at all possible. Sure VSCode has become more mature and partial staging is actually working really nice these days. I've just used it recently (last week). – Robert Koritnik May 11 '23 at 13:02
  • @RobertKoritnik: VSCode definitely got better with time. If your comment isn't up-to-date anymore, you could delete it. – Eric Duminil May 11 '23 at 15:39
  • @EricDuminil Why? Are you going through all of your SO activity at least on a yearly basis and delete outdated questions, answers and comments? Every comment has a datetime stamp on it. I understand you didn't check it. – Robert Koritnik May 12 '23 at 13:01
  • 1
    @RobertKoritnik: That's the purpose of the community. If somebody notices and tells me that my answer or comment is now wrong or outdated, yes, I'm happy to delete the content. Your highly upvoted comment could lead people to believe it's not worth it to look for a solution. Anyway, as you wish, regardless how old your comment is. – Eric Duminil May 12 '23 at 21:38
33

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.

YakovL
  • 7,557
  • 12
  • 62
  • 102
  • 4
    This is one click in Intellij, maybe worth the cost for an IDE, in terms of workflow. – godhar Jun 23 '22 at 12:32
  • 1
    @godhar may be there are plugins that make this one-click, but I prefer hotkeys anyway, so haven't researched this aspect that much – YakovL Jun 23 '22 at 12:47
24

This option has been added to the command palette:

on Mac:

shift+command+P and type "Stage Selected Ranges"

on Windows:

control+shift+P` and type "Stage Selected Ranges"

Next time you open the command palette you don't have to type in, just press enter (the tool preselect your last command).

Manually

Help > Show All Commands > and type "Stage Selected Ranges"

baddy
  • 47
  • 4
Dimitris
  • 341
  • 2
  • 6
4

Use Interactive staging.

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