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.
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.
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.
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.
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.
Create a local Git repo in an empty folder
You can create a new local repo in an empty folder from Team Explorer.
Under Local Git Repositories in the Connect view of Team Explorer, choose New.
Enter a folder path where the repo will be created. The specified folder must be empty, or not exist. Choose Create.
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.
Your new local Git repo only contains Git resources.
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
- 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.
Or, choose Git > Push to Git service from the menu bar to launch the Create a Git repository window.
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.In your Azure DevOps project, create an empty Git repo without a README file. Copy the clone URL from the Clone Repository popup.
- 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.
Visual Studio Team Explorer
- In the Push to Azure Dev Ops Services section of the Synchronization view in Team Explorer, select the Publish Git Repo button.
- Choose your Azure DevOps account, organization, and a repo name, and then select Publish Repository.
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.
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
- 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.
Or, choose Git > Push to Git service from the menu bar to launch the Create a Git repository window.
- In the Create a Git repository window, choose GitHub, select your GitHub account, owner name, and a new repo name, and then choose Push.
This step creates a new repo in your GitHub account with content from your local repo.