Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Python Source Control Management (Git) in VS Code

VS Code has built in support for source control management. In this lesson, you’ll learn how to work with VS Code’s robust source control features such as:

  • Staging files
  • Making commits
  • Pushing to and pulling from remote repos
  • Viewing diffs

00:00 VS Code has robust source control capabilities. You can commit files, push and pull them from remote repositories, checkout and create branches, fix merge conflicts, and view diffs.

00:14 It has support for Git and GitHub built-in, and if you need to use something else, there’s probably an extension for it in the Marketplace. I’m going to start by opening up a new integrated terminal, just like this, and I’m going to use this to clone a private repository I have on my GitHub account.

00:32 My repo contains a very trivial little Python module that exposes functions for adding and subtracting numbers, similar to what we saw before. Now that I’ve cloned my repo, I’ll change directory into it, just like normal. Now in VS Code I’ll press Command + O to open a folder and I’ll select my repo folder on the desktop.

00:55 If we take a look at my operations.py file, you’ll see what I mean by this being a very trivial module, but it works for demonstration purposes.

01:05 I’ll select File > New File, and I’ll call this program.py and I’ll make sure that I’m saving it into my local repo directory. Let’s see what’s going on with our Git status.

01:18 I could either run git status down here in the integrated terminal, or I can see what’s being displayed in the source control sidebar. Let’s click on this icon that looks like a fork, and now we see on the left that we have all of our files with changes.

01:35 This U means that our file is unstaged, which makes sense because we’ve just created it. To stage the file, hover over the name and click on the little plus icon.

01:47 And now you see that the file has moved to STAGED CHANGES and it has an A, meaning that it’s been added. In our program here, I will quickly import the operations module and now I will print out the result of calling the add() function with two numbers. And just to make sure this works, I will run this file in the integrated terminal and we’ll see our expected output.

02:13 Perfect. Notice on the left that this file is appearing under CHANGES. That’s because we’ve changed the file. The M stands for modified.

02:24 Once again, I’ll click the plus to stage these changes, and now I can enter a commit message up here. So I’ll just give it a basic commit message here, and now I can click on this little check mark to commit these changes, and you’ll see that they disappear from the list.

02:42 If we click on More Options up here in the top-right corner, you’ll see that we have lots and lots of different commands that we can run. But just to keep things simple, I’ll click on this little button down here in the bottom-left corner.

02:57 This will automatically push and pull changes from the remote server. And I’ll click on OK. Just to show that this worked, I’ll move over to our integrated terminal and we’ll run a good old-fashioned git status.

03:13 Once I press Enter, we see that we have nothing to commit.

03:17 Our changes have been pushed to the remote repository. But now, if we switch to the operations module, you’ll see that we didn’t ever actually use our subtraction function.

03:28 I’ll select the whole thing and press Delete, and now you’ll see that this file shows up under CHANGES. If I click on it, VS Code will show me the differences. As you can imagine, this is extremely helpful.

03:44 That’s how to perform some common source control tasks in Visual Studio Code. For the most part, everything else related to source control works just like it does in other editors. In this video series, you learned what Visual Studio Code is, how to find and install extensions, how to work with folders and workspaces, how to run and debug programs, how to work with unit tests, and finally, how to work with some of VS Code’s robust source control features. Like with most large open-source software projects, there are tons of little features riddled throughout the software.

04:22 I learn something new all the time. I encourage you to download VS Code if you haven’t already and play around with it.

04:30 If there’s a feature that you want and you can’t find out how to do it, search for it in the Extensions Marketplace. If you still can’t figure out how to make the editor do what you want and Google searching isn’t helping, head over to the GitHub repository for VS Code and submit a feature request to the engineers.

04:49 Just make sure that you read the contributing guidelines first. I’m Austin Cepalia with realpython.com, and I wish you the best of luck on your programming endeavors. Happy coding!

Avatar image for joseph

joseph on April 6, 2019

Absolutely fantastic. Thanks for whipping this up and sharing.

Avatar image for Daniel

Daniel on April 6, 2019

Great course. Helped to see it all in action and then go through the supporting materials alongside the course.

Avatar image for Austin Cepalia

Austin Cepalia RP Team on April 7, 2019

Thanks, glad you both liked it! I design my courses so that they complement the written tutorials, sometimes adding in extra bits of information or different examples to help clarify a concept. Video + written content go hand-in-hand :)

Would be great to get any optimizations for running python debugging as my vs code currently takes ~2-5 seconds every time i run it.. which doing so regularly is super slow !

Also how can i get other unittest modules (pytest) working as efficiently as you have unittest working ?


Avatar image for Sciencificity

Sciencificity on April 15, 2019

Thanks so much! This was awesome, I am sure to not go back to my previously used editor!

Avatar image for Pavel Krejsa

Pavel Krejsa on April 23, 2019

Hi, thanx a lot for this beginners intro. I would be very happy if you extend it with “virtualenv” like environment with different pip packages per environment.

Avatar image for stephenm

stephenm on June 1, 2019

Great course! Would be nice to add a video on how to setup/config/use virtual environments with VS code

Avatar image for JulianV

JulianV on June 5, 2019

git source control is just a click away.

Avatar image for Subhash Bhushan

Subhash Bhushan on Nov. 14, 2019

All Git-based source code repositories out there - Github, Gitlab, BitBucket - would work by default. If you use Mercurial, you could install the vscode-hg extension.

Avatar image for qmark42

qmark42 on Feb. 1, 2020

I thought that I could do fair in Python coding but have since found that I am a “mid-beginner.” I will be downloading VS. but I expect to come back to this course in the future.

Thanks much to Austin and Real Python.

Avatar image for Lokman

Lokman on March 12, 2020

Thanks @Austin Cepalia, last time I’m using Vim editor and bash. After start learned Vscode looks like editor IDE make things easier.

Avatar image for Ray Leiter

Ray Leiter on March 27, 2020

This video doesn’t appear to me to be a “beginners” video. I mistakenly thought this video was going to concentrate on how to setup the interface for a more productive use of the edit/run cycle. Material like the use of source control techniques and the debug facilities, I believe would be part of an additional video. Also, I think many tutorial authors are somewhat disingenuous when they use a particular operating environment but make the claim that it should be the same as other operating environments.

Avatar image for rickecon

rickecon on April 11, 2020

Great course. Thank you.

Avatar image for Aparna

Aparna on April 26, 2020

Much needed content to get started with VS code. thanks

Avatar image for tsusadivyago

tsusadivyago on May 19, 2020

I am going to try vs code now, great tutorial

Avatar image for Ranjith

Ranjith on May 25, 2020

Really good tutorial. Thank you. I think I will start using VS Code.

Avatar image for Alain Rouleau

Alain Rouleau on July 20, 2020

Thanks for the tutorial. Learned quite a lot and VS Code is now my editor of choice. Tried other IDEs like PyCharm but much prefer VS Code since it’s not so bloated.

Avatar image for Grouchy Old Fart

Grouchy Old Fart on Oct. 11, 2023

I had to bail on this one. I don’t know if this is a difference between Mac and Windows, or if VS Code has changed a great deal in the last four years, or if I am doing something wrong. Whatever the problem is, much of it doesn’t work as advertised.

There is a chance this is way over my head as well.

Avatar image for eyesnight00

eyesnight00 on Feb. 11, 2024

This is 5 years old and the video could do with being updated as VS Code has a few changes.

Avatar image for Bartosz Zaczyński

Bartosz Zaczyński RP Team on Feb. 12, 2024

@eyesnight00 Noted! Do you have any particular suggestions for the updates you’d like to see?

Avatar image for rwelk

rwelk on Feb. 20, 2024

Very informative I liked the info in this video. I have applied the code snippet extension and played with it, works well. Thank you!

Become a Member to join the conversation.