23

I'm using Visual Studio Code and its integrated version control. I would like to split a group of deleted lines in two different commits.

I know about the "Stage Selected Ranges" option but, from what I see, I can't select deleted lines.

Is there a way to achieve it?

Thanks

enter image description here

Fab
  • 4,526
  • 2
  • 21
  • 45
  • 5
    I tried `Stage Selected Ranges` and it worked well with deleted lines. You can use the mouse or `Shift+Down Arrow` to select the deleted lines. – ElpieKay Jul 26 '19 at 10:08
  • 5
    @ElpieKay It looks like the `Stage Selected Ranges` option works only for selection on the right side, but in my case I have to select lines on the left side. If I select the first 4 lines and then click on the `Stage Selected Ranges` option nothing happens. – Fab Jul 26 '19 at 10:25
  • I selected the lines on the left side. – ElpieKay Jul 26 '19 at 11:05
  • 1
    I know you're asking about vscode, not command line, but for what it's worth, `git add -p ` will let you do what you want. – joanis Jul 26 '19 at 12:46
  • @ElpieKay Now I selected lines from 13 to 17, then CMD+CTRL+P and then Stage Selected Ranges, but all the block from 13 to 22 is added to stage. I don't know, maybe I'm doing something wrong. – Fab Jul 26 '19 at 12:54
  • @joanis thanks but I would like to achieve it using vscode. – Fab Jul 26 '19 at 12:57
  • I don't know how to confirm without git commands if the staged status is as expeced. You could run `git status` after `Stage Selected Ranges` to see if the file is seen in both the staged and changed sectors, and `git diff --cached` to see if only the selected lines are staged. – ElpieKay Jul 26 '19 at 15:08
  • The issue about not being able to select deleted lines has been open since June 2016 and has almost 300 upvotes. Still, not fix yet: https://github.com/microsoft/vscode/issues/8226 – AxeEffect Feb 04 '23 at 00:31

2 Answers2

11

I'm afraid there is no way to do what you want with VS Code.

It's not a VS Code problem, it's a git problem. git is telling VS Code to treat those consecutive changes as one hunk, and so if git can't split it, then VS Code can't either.

Try running git add -p on the command line, and you'll probably see that git treats those changes as one hunk since they are pretty close to each other. Try passing s on the git add -p prompt, and if it says "Sorry, cannot split this hunk", then VS Code can't either.

I think the only way is by manually editing the patch file with git.
See: Can I split an already split hunk with git?

There were requests for VS Code to support patch file editing like this: https://github.com/Microsoft/vscode/issues/69891, to which the response was:

We try to keep VS Code lean and we think the functionality you're asking for is great for a VS Code extension. Maybe you can already find one that suits you in the VS Code Marketplace.

AFAIK, the only patch-related extension is Git Patch. It allows you to create patches from staged/unstaged changes, but it does not let you select which changes to add to the patch. You'll have to edit the patch manually, discard your changes, then apply the patch one by one. It's pretty much the same as doing git add -p and then passing e.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • 3
    Thanks for you answer, I upvote it. However in the past I used Source Tree and I was able to do it without any problem (I mean selecting multiple lines within the same hunk and stage them) – Fab Jul 27 '19 at 14:19
  • 2
    I think SourceTree internally creates and edits the patch files for you. I remember that it has its own patch file functionality. – Gino Mempin Jul 28 '19 at 03:44
  • 1
    Git extensions allows you to do this easily as well. – James Affleck Jan 15 '20 at 21:52
  • Ugh, really Microsoft? Atom was able to do this easily. – wjandrea Nov 05 '22 at 17:14
0

Ctrl+K then Ctrl+Alt+S (VSCode 1.75.0)

CodeBy
  • 714
  • 4
  • 10
  • 25