Edit

Share via


Share your code by using Git

Azure DevOps Services | Azure DevOps Server | Azure DevOps Server 2022

Share your code with others in Azure DevOps by using a Git repository. Clone the repo to your computer, create a branch for your changes, commit your work, and open a pull request to merge it back into the main branch.

Tip

You can use AI to help with this task later in this article, or see Enable AI assistance with Azure DevOps MCP Server to get started.

Prerequisites

Category Requirements
Project access Project member.
Git command-line tool One of the following Git command-line tools:
- Git for Windows and Git Credential Manager.
- Git for macOS or Linux. For macOS and Linux, we recommend that you configure SSH authentication.

1. Clone the repo to your computer

When you clone a repo, you create a complete local copy so you can work offline and push changes back later. Before you clone, make sure your code is in an Azure Repos Git repository:

  1. From your web browser, open the team project for your organization and select Repos > Files.

    Screenshot of project with Repos and Files highlighted.

  2. Select Clone, and then Copy the URL.

    Screenshot shows highlighted clone button in repos files.

  3. Open your Git command window (Git Bash on Windows) and go to the folder where you want to store the repo. Run git clone with the URL you copied:

    git clone https://FabrikamFiber01@dev.azure.com/FabrikamFiber01/FabrikamFiber01-01/_git/FabrikamFiber01-01
    

    Git downloads a copy of the code, including all commits and branches, into a new folder.

  4. Switch to the cloned repository directory:

    cd fabrikam-web
    

    Keep this command window open to work in a branch.

2. Work in a branch

Git branches isolate your changes from other work in the project. The recommended Git workflow creates a new branch for every feature or fix. The examples in this article use the branch users/jamal/feature1.

  1. Create a branch with the branch command.

    git branch users/jamal/feature1
    
  2. Use checkout to switch to that branch.

    git checkout users/jamal/feature1
    

    Tip

    Create and switch in one step with git checkout -b users/jamal/feature1. If you're working with a previously cloned repo, run git pull origin main first to ensure your branch starts from the latest code.

3. Work with the code

Edit files locally, commit your changes, and push the commit to the server.

  1. Open the README.md file in the cloned repo folder, make some changes, and Save the file.

  2. Stage and commit your changes:

    git add .
    git commit -m "My first commit"
    

    git add . stages new and changed files. git commit -m saves them as a commit with the specified message. Git always commits to the current branch, so verify you're on the right one before committing.

  3. Push your commit to the server:

    git push origin users/jamal/feature1
    

Your code is now in the remote repository on the users/jamal/feature1 branch. To merge it into main, create a pull request.

4. Merge your changes with a pull request

Pull requests let your team review and approve code before it merges. Create a pull request when your branch is ready for feedback - you can abandon it at any time.

  1. In your web browser, go to your project and select Repos > Files.

  2. Select Create a pull request in the upper-right corner. If you don't see a message like You updated users/jamal/feature1 just now, refresh your browser.

    Create a pull request

    The pull request targets the default branch (main). The title and description come from your commit message. You can add reviewers and link work items before creating.

    New pull request

  3. Select Create.

  4. Review the Overview tab, and then select Complete > Complete merge to merge your code into main.

    Complete pull request

Note

For more information, see Create, view, and manage pull requests.

Your changes are now in main, and the users/jamal/feature1 branch is deleted from the remote repository.

View history

To see your merged changes, go to Repos > Files in the web portal and select History.

Screenshot of web portal, with History highlighted

Select the Files tab and then the README file to view your changes.

Screenshot of README file

Clean up

Delete your local copy of the branch after the merge completes:

git checkout main
git pull origin main
git branch -d users/jamal/feature1

These commands switch to main, pull the latest code (including your merged changes), and delete the local users/jamal/feature1 branch.

Use AI to manage Git repositories

If you configure the Azure DevOps MCP Server, you can use AI assistants to manage your Git repositories and pull requests through natural language prompts.

Example prompts for Git management

Task Example prompt
List repositories List all Git repositories in <Contoso> project
View pull requests Show my open pull requests in <Contoso> project
Check PR status Get the status of pull request <456> in <Contoso> project
Find active branches List branches with active pull requests in the <webapp> repo in <Contoso>
Review PR details Show the files changed in pull request <456> in <Contoso>
Check build status Show the build status for pull request <456> in <Contoso>
Summarize PR activity List all pull requests merged into <main> in the <webapp> repo in <Contoso> this week
Find stale branches List branches in the <webapp> repo in <Contoso> that have had no commits in the last <30> days
Review reviewer workload Show how many open pull requests each team member is reviewing in <Contoso> project

Next steps