Freigeben über


Create a new Git repo

TFS 2018

Visual Studio 2019 | Visual Studio 2022

A Git repository, or repo, tracks changes to files within a folder. You can create any number of local Git repos on your computer, each stored in its own folder. Each Git repo that you create is independent of other Git repos, so changes you make in one repo won't affect the others.

A Git repo stores every version of every file in the repo, unless you tell Git to ignore a file. Git saves file versions efficiently, so storing a large number of versions doesn't necessarily require a lot of disk space. Git supports comparing file versions, merging different versions, switching between versions, and much more. You can use Git to manage your source code, regardless of whether your code files are inside or outside of a Visual Studio solution. To share your work, you can connect your local Git repo to a remote Git repo that others can access. The remote repo can be an Azure Repos Git repo, a GitHub repo, or other hosted Git repo.

This article provides procedures for the following tasks:

  • Create a local Git repo from a new solution
  • Create a local Git repo from an existing solution
  • Create a local Git repo in an empty folder
  • Connect a local Git repo to an Azure Repos Git repo
  • Connect a local Git repo to a GitHub repo

For an overview of the Git workflow, see Azure Repos Git tutorial.

Prerequisites for access to Azure Repos

  • To view code, you must be a member of an Azure DevOps project with Basic access or higher. If you aren't a project member, get added.

  • To clone or contribute to code, you must be a member of the Contributors security group or have the corresponding permissions.

Create a local Git repo from a new solution

Visual Studio doesn't support creating a local Git repo when you create a new solution. Instead, create your Visual Studio solution, then follow the steps in Create a local Git repo from an existing solution.

Git command line doesn't support creating a new Visual Studio solution. Instead, create your new solution using Visual Studio, then follow the steps in Create a local Git repo from an existing solution.


Create a local Git repo from an existing solution

Create a local Git repo to track file changes in your existing Visual Studio solution.

Visual Studio 2022 provides a Git version control experience by using the Git menu, Git Changes, and through context menus in Solution Explorer. Visual Studio 2019 version 16.8 also offers the Team Explorer Git user interface. For more information, see the Visual Studio 2019 - Team Explorer tab.

  1. In Solution Explorer, right-click the solution name, or right-click any item in the Folder view of Solution Explorer, and then select Create Git Repository. Or, choose Add to Source Control on the status bar in the lower right-hand corner of Visual Studio, and then select Git. If you don't see these options, then your code is already in a Git repo.

    Screenshot of the 'Create Git repository' option in the Solution Explorer context menu in Visual Studio 2019.

    Or, choose Git > Create Git Repository from the menu bar to launch the Create a Git repository window. If you don't see this option, then your code is already in a Git repo.

    Screenshot of the 'Create Git Repository' option in the Git menu from the menu bar of Visual Studio 2019.

  2. In the Create a Git repository window, choose Local only, verify the local path is correct, and then choose Create.

    Screenshot of the 'Create a Git repository' window with the 'Local only' option selected in Visual Studio 2019.

You've now created a local Git repo in the Visual Studio solution folder and committed your code into that repo. Your local Git repo contains both your Visual Studio solution and Git resources.

Screenshot of the Git folder, Git ignore file, and Git attributes file in Windows file explorer.

Create a local Git repo in an empty folder

Visual Studio 2022 doesn't support creating a new local repo by using Team Explorer. Use Git Command Line.

Connect a local Git repo to an Azure Repos Git repo

You can share your code with others by connecting your local Git repo to an Azure Repos Git repo.

  1. In Solution Explorer, right-click the solution name, or right-click any item in the Folder view of Solution Explorer and then select Push to Git service to launch the Create a Git repository window.

    Screenshot of the 'Push to Git service' option in the in Visual Studio 2022 context menu.

    Or, choose Git > Push to Git service from the menu bar to launch the Create a Git repository window.

    Screenshot of the 'Push to Git service' menu option in the Git menu on the menu bar in Visual Studio 2022.

  2. Open a browser and navigate to your Azure DevOps project by using a URL in the form of https://dev.azure.com/<OrganizationName>/<ProjectName>. If you don't have a project yet, create one.

  3. In your Azure DevOps project, create an empty Git repo without a README file. Copy the clone URL from the Clone Repository popup.

    Screenshot of the 'Clone Repository' popup from the Azure DevOps project site.

  4. In the Create a Git repository window, choose Existing remote and enter the repo clone URL from the previous step, and then choose Push. For more information on how to create a new Azure DevOps repo, see Create a new Git repo in your project.

    Screenshot of the 'Create a Git repository' window, with the URL of an empty Azure repo, in Visual Studio 2022.

Connect a local Git repo to a GitHub repo

You can also share your code with others by connecting your local Git repo to a GitHub repo.

  1. In Solution Explorer, right-click the solution name, or right-click any item in the Folder view of Solution Explorer, and then select Push to Git service to launch the Create a Git repository window.

    Screenshot of the 'Push to Git service' option in the in Visual Studio 2022 context menu.

    Or, choose Git > Push to Git service from the menu bar to launch the Create a Git repository window.

    Screenshot of the 'Push to Git service' menu option in the Git menu on the menu bar in Visual Studio 2022.

  2. In the Create a Git repository window, choose GitHub, select your GitHub account, owner name, and a new repo name, and then choose Push.

    Screenshot of the 'Create a Git repository' window, with the URL of an empty GitHub repo, in Visual Studio 2022.

    This step creates a new repo in your GitHub account with content from your local repo.

Next steps