I have a new file I want to commit, but I only want to stage some of it. There are parts I cannot commit.
How can I partially stage this new file for commit using VS Code?
I am aware of "VS Code: How to stage and commit individual changes in a single file?" but it doesn't seem to work for new files.
I expected to be able to click on the file in the "Source Control" menu, then select some lines I wanted to stage, right click and choose "Stage Selected Ranges". But the "Stage Selected Ranges" option wasn't there.
When that didn't work I tried staging the whole file, clicking on the staged file in the "Source Control" menu, selecting some lines I do not want committed, right clicking and select "Unstage Selected Ranges", but that option also didn't exist.
Also, while I am sure there is probably some way to do it on the command line, I want to know how to do it in my editor.
tl;dr: Simply stage an empty version of the file first.
Details: In VS Code, in order to stage hunks on a file, it must first know about the file. In order to accomplish that you can either commit a version of the file, or just stage it. Here's a working example with the file open but not yet committed or staged:
Note: the following commands are for Windows. Substitute the appropriate commands for your OS if they differ.
Ctrl-A # select the entire file
Ctrl-X # remove all file contents and copy the file into the clipboard
Ctrl-S # Save the file
Ctrl-V # (or Ctrl-Z) paste (or undo) the file to put it back how it was
# Note: don't save again yet!
# Now stage the empty file. The file will appear in "Staged"
# Now click back to open file again
Ctrl-S # Save the file again. The file will also appear in "Changes"
# Now click on the file under "Changes" to open the diff view.
# Now you can stage parts of the file as you normally would.
Warning: the above commands contain a cut and paste. In between those steps if you copy something else you could blow away your clipboard. If this could cause a problem for you then save the file outside of the repo first, just in case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With