Поделиться через


Create a new Git repo

TFS 2017 | TFS 2015 | TFS 2013

Visual Studio 2019 | Visual Studio 2017 | Visual Studio 2015

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

Create a local Git repo when you create a new Visual Studio solution.

Visual Studio 2019 doesn't support creating a new Git repo when creating a solution. Instead, create your Visual Studio solution, 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.

  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. This step only applies to the Visual Studio Git version control experience: 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.

    Team Explorer doesn't launch the Create a Git repository window, and assumes you want a local Git repo.

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

You can create a new local repo in an empty folder from Team Explorer.

  1. Under Local Git Repositories in the Connect view of Team Explorer, choose New.

    Screenshot of the new repository option in the 'Local Git Repositories' section of the 'Connect' view of 'Team Explorer' in Visual Studio 2019.

  2. Enter a folder path where the repo will be created. The specified folder must be empty, or not exist. Choose Create.

    Screenshot of the new repository path and Create button in the 'Local Git Repositories' section of the 'Connect' view of 'Team Explorer' in Visual Studio 2019.

  3. You've now created a new local Git repo. To view the folder contents, right-click the new repo entry and select Open in File Explorer.

    Screenshot of the new repository entry and its context menu in the 'Local Git Repositories' section of the 'Connect' view of 'Team Explorer' in Visual Studio 2019.

    Your new local Git repo only contains Git resources.

    Screenshot of the new repository folder in Windows File explorer showing a .git folder, a .gitignore file, and a .gitattributes file.

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.

Visual Studio 2019 version 16.8 and later versions provides a Git version control experience while maintaining the Team Explorer Git user interface. To use Team Explorer, uncheck Tools > Options > Preview Features > New Git user experience from the menu bar. You can use Git features from either interface interchangeably. Below, we provide a side-by-side comparison for publishing your work to an Azure Repos Git repo.

Note

One advantage of connecting to a project through Team Explorer is you gain access to the Work Items hub. For an overview of Team Explorer features, see Navigate in Visual Studio Team Explorer.

Visual Studio Git

  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' menu option in the Git menu on the menu bar in Visual Studio 2019.

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' option in the in Visual Studio 2019 context menu.

  1. 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.

  2. 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.

  1. 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 2019.

Visual Studio Team Explorer

  1. In the Push to Azure Dev Ops Services section of the Synchronization view in Team Explorer, select the Publish Git Repo button.

Screenshot of the 'Push' view of 'Team Explorer' in Visual Studio 2019.

  1. Choose your Azure DevOps account, organization, and a repo name, and then select Publish Repository.

Screenshot of the Azure DevOps account, organization, and repo name options and the 'Publish Repository' button in the 'Synchronization' view of 'Team Explorer' in Visual Studio 2019.

This step creates a new project in your Azure DevOps account with the same name that you selected for the repo. To create the repo in an existing project, select Advanced next to the Repository name, and then choose a project.

Your code is now in a Azure Repos Git repo. You can view your code on the web by selecting See it on the web.

Screenshot of the 'See it on the web' link in the 'Home' view of 'Team Explorer' in Visual Studio 2019.

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.

Visual Studio 2019 version 16.8 and later versions provides a Git version control experience while maintaining the Team Explorer Git user interface. To use Team Explorer, uncheck Tools > Options > Preview Features > New Git user experience from the menu bar. You can use Git features from either interface interchangeably. Below, we provide a side-by-side comparison for publishing your work to a GitHub repo.

Visual Studio Git

  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' menu option in the Git menu on the menu bar in Visual Studio 2019.

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' option in the in Visual Studio 2019 context menu.

  1. 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 2019.

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

Visual Studio Team Explorer

  1. In the Publish to GitHub section of the Synchronization view in Team Explorer, select your GitHub account, owner name, and a new repo name, and then choose Publish.

Screenshot of the GitHub account, name, and repo name options and the 'Publish' button in the 'Synchronization' view of 'Team Explorer' in Visual Studio 2019.

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

Next steps