Delen via


Share your code with Azure Repos and Visual Studio

TFS 2018

Visual Studio 2019 | Visual Studio 2022

Share your Visual Studio solution with others by hosting your code in an Azure Repos Git repository. You can use Visual Studio to manage Azure Repos Git repositories or GitHub repositories. This article provides procedures for the following tasks:

  • Add a project or solution to a local Git repository
  • Publish your code to an Azure Repos Git repository or GitHub repository
  • Review your code changes before publishing
  • Link work items to your commits
  • Commit and push updates
  • Pull changes from the main branch of your repository

To clone a GitHub repository, get the clone URL as described in Cloning a repository.

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

Prerequisites

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

To learn more about permissions and access, see Default Git repository and branch permissions and About access levels.

Note

Visual Studio 2019 version 16.8 and later versions provide a new Git menu for managing the Git workflow with less context switching than Team Explorer. Procedures provided in this article under the Visual Studio tab provide information for using the Git experience as well as Team Explorer. To learn more, see Side-by-side comparison of Git and Team Explorer.

Add a project or solution to a new local Git repository

The first step to sharing your Visual Studio solution with others using Git is to create a local Git repo for your code. If it's your first time using Visual Studio to connect to Azure Repos, see Connect to a project from Visual Studio for details.

  1. Create a new local Git repo for your Visual Studio solution, by right-clicking the solution name in Solution Explorer, and then selecting 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 2022.

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

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

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.

Publish your code

You can share your work with others by publishing your local Git repo to an Azure Repos Git repo.

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. Connect to an Azure DevOps repo by right-clicking the solution name in Solution Explorer, and then selecting Push to Git service to launch the Create a Git repository window.

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

    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 2022 context menu.

  2. In the Create a Git repository dialog, select GitHub. Enter any required values under Create a new GitHub repository. Then select Push.

    Screenshot of the 'Create a Git repository' dialog for Visual Studio 2022.

    You can verify that the repo is created by going to GitHub and searching for your repo name in your account.

Review commit changes

It's good practice to review the code changes in your commit before publishing. Visual Studio provides a diff view that helps you see all the changes you've made since the previous commit.

  1. To see what's changed in a commit, select Git > Manage Branches to open the Git Repository window.

    Screenshot of in Visual Studio.

  2. In the Git Repository window, select your branch, right-click the commit, and then choose View Commit Details to open the Commit window.

    Screenshot of the 'View Commit Details' menu option for a commit in the 'Git Repository' window in Visual Studio.

  3. In the Commit window, you can right-click on any file and select Compare with Previous to view the file changes made by the commit.

    Screenshot of the 'Compare with Previous' option in the Commit pane in Visual Studio.

As you develop your software, you can capture which commits support the completion of work items like Azure DevOps tasks or bug fixes. The audit trail of work items linked to commits lets your team understand how tasks and bugs were resolved.

Note

The #ID syntax is supported when you connect to an Azure Repos Git repository. If you connect to a GitHub repository, you can still link to work items, but must use the #ABID syntax, and have previously set up an Azure Boards-GitHub connection.

You can link work items to commits by adding #ID into the commit message. For example, the commit message "Fix bug #12 Missing icons in the reporting tools" would link work item 12 to the commit when the commit is pushed to Azure Repos.

Screenshot of a work item linked to a commit in the 'Git Changes' window in Visual Studio.

Push commit changes

As you write your code, your changes are automatically tracked by Visual Studio. When you're satisfied with your changes, you can save them in Git using a commit, and then push your commit to Azure Repos.

  1. In the Git Changes window, enter a message that describes the changes, and then choose Commit All. Commit All commits unstaged changes and skips the staging area. You can choose to stage all files before committing by selecting the stage all + (plus) button at the top of the Changes section of the Git Changes window.

    Screenshot of the 'Commit All' button in the 'Git Changes' window in Visual Studio.

  2. To push your commit to Azure Repos, select the up-arrow push button.

    Screenshot of the up-arrow push button in the 'Git Changes' window of Visual Studio.

    Or, you can push your commit from the Git Repository window. To open the Git Repository window, select the outgoing / incoming link in the Git Changes window.

    Screenshot of the 'outgoing / incoming' link in the 'Git Changes' window, and the Push link in the 'Git Repository' window of Visual Studio.

    Or, you can choose Git > Push from the menu bar.

    Screenshot of the Push option from the Git menu in Visual Studio.

Pull changes

You can keep your local branches in sync with their remote counterparts by pulling commits created by others. While you're working on your feature branch, it's a good idea to periodically switch to your main branch and pull new commits to keep it current with the remote main branch.

Note

In Visual Studio, Fetch and Pull links can be found in different views and in the Git menu on the menu bar. Those links all do the same thing—fetch or pull from the remote repo to the current branch of the local repo.

  1. In the Git Changes window, you can check for the latest commits on the remote branch by using the down-arrow fetch link, then selecting the outgoing / incoming link to open the Git Repository window. Fetch downloads remote commits that aren't in your local branch, but won't merge them into your local branch. Fetched commits show up in the Incoming Commits section of the Git Repository window. You can double-click a fetched commit to view its file changes.

    Screenshot of the Fetch, Pull, Push and Sync buttons in the 'Git Changes' window of Visual Studio.

  2. Choose Pull to merge the fetched remote commits into your local repo. Pull performs a fetch and then merges the downloaded commits into your local branch.

FAQs

Q: Why does the sign-in page differ between the web portal and Visual Studio?

A: Your sign-in page depends on whether you used a Microsoft account or a work account with Azure DevOps. Always sign in with the username and password that you use for Azure DevOps.

Q: Can I use the Git command prompt with Visual Studio?

A: Visual Studio's Team Explorer and the Git command-line work great together. Changes to your repos made in either tool will be reflected in the other. Make sure to install the latest release of Git for Windows, which has tools to help you connect to your Azure DevOps Services/TFS repos.

See the Azure Repos Git tutorial and the command reference for additional help using Git from the command line.

Next steps