How to do a partial commit with VS Code

in #developmentlast month

image.png

VS Code has amazing git support. One of the features I really love is the ability to commit partial file updates.

Why would you want to do that?

Say you are working on a project, you end up coding one thing but then start working on another feature without committing the previous changes. Let's say you added a function, and only want to commit that function and ignore other changes. VS Code has you covered!

Let's start with a simple piece of code.

This code is all committed, let's go ahead and make a few changes.

This is really simple and rather dumb code, but let's pretend it isn't.

I first created the get_webpage() function to wrap the requests call. I later added the print_web_response() function just so we can have a second simple function.

I want to go ahead and commit just the changes that involve get_webpage() and then do a separate commit for the print_web_response() code.

First you want to "Open Changes"

From there you have a really useful visual diff view of your code. Select the lines you want to commit and right click and select Stage Selected Ranges

You should see the diff update reflecting the changes to what is new.

Let's do a git status to see what is staged.

Here you can see app.py is staged, but wait it is also unstaged?

The reason for this is because only a few lines are staged, a rather advanced but useful git feature. Let's commit!

image.png

An updated git status shows there are still changes that need to be staged and commited.

This is a useful feature that VS Code makes super easy to work with. Keep it in mind when you forget to stage some code without staging the entire file.

Cover Image Source

Sort: