Tutorial: Work with Git in Visual Studio

This article presents Step 6 in the tutorial series Work with Python in Visual Studio.

Git is a version control system that helps you keep track of changes to your code files and projects. By storing your Python project files in Git, you can enable multiple developers to contribute to your files and maintain the integrity of your project over time.

In the previous Steps in this tutorial, you create a Python application and make various changes to the code. In Step 6, you can add your application to a Git repository. You explore how to work with Git tools in Visual Studio to provide source control for your Python projects.

In Step 6 of the tutorial, you learn how to:

  • Use Git tools in Visual Studio
  • Add a Python application to a Git repository

Prerequisites

  • A Python application project that has a Python file (.py) with code created in Step 2: Write and run Python code.

  • Although not required to proceed with this Step, review the instructions in Step 3, Step 4, and Step 5 of this tutorial for an end-to-end Python workflow in Visual Studio.

Add Git source control

Now that you have an application, you might want to add it to a Git repository. Visual Studio makes that process easy with Git tools you can use directly from the IDE.

Tip

Git is the most widely used modern version control system. Whether you're a professional developer or you're learning how to code, Git can be very useful. If you're new to Git, the https://git-scm.com/ website is a good place to start. You can find cheat sheets, a popular online book, and Git Basics videos.

To associate your code with Git, start by creating a new Git repository where your code is located:

  1. In the status bar at the bottom-right of Visual Studio, select Add to Source Control, and then select Git.

    Screenshot that shows how to access the Git source control action from Add to Source Control in Visual Studio.

  2. In the Create a Git repository dialog box, sign in to GitHub:

    Screenshot of the Create a Git Repository dialog window where you can sign in to GitHub.

    The repository name autopopulates based on your folder location. Your new repository is private by default, which means you're the only one who can access it.

    Tip

    Whether your repository is public or private, it's best to have a remote backup of your code stored securely on GitHub. Even if you aren't working with a team, a remote repository makes your code available to you from any computer.

  3. Select Create and Push. After you create your repository, you see status details in the status bar:

    Screenshot of the repo status bar below the Solution Explorer pane in Visual Studio.

Use Git actions in Visual Studio

Here's a brief summary of Git actions available in the Visual Studio status bar:

  • The Up/Down arrows show how many outgoing/incoming commits are in your current branch. You can use this icon to pull any incoming commits or push any outgoing commits.

  • To view a specific commit, select the Up/Down arrow, and then select View Outgoing/Incoming.

  • The Pencil shows the number of uncommitted changes to your code. You can select this icon to view those changes in the Git Changes window.

The Git menu provides tools for repository actions on your files. You can use git fetch, pull, push, and sync for version control in Visual Studio.

To learn more about how to use Git with your app, see the Visual Studio version control documentation.

Tutorial review

Congratulations on completing this tutorial on Python in Visual Studio.

In this tutorial, you learned how to:

  • Create projects and view a project's contents.
  • Use the Visual Studio editor and run a project.
  • Use the Interactive Window for Python to develop new code and easily copy code into the editor.
  • Run a completed program in the Visual Studio Debugger.
  • Install packages and manage Python environments.
  • Work with code in a Git repository.