Share via


Share your code with Git

TFS 2018

Share your code with others in Azure DevOps when you use a Git repository.

Prerequisites

You must have an organization and project in Azure DevOps. When you create a project, Azure DevOps automatically creates an empty repository in Repos.

1. Install Git command-line tools

Install one of the following Git command-line tools:

2. Clone the repo to your computer

To work with a Git repo, clone it to your computer, which creates a complete local copy of the repo. Your code might be in one of several places.

  1. Complete the following step that's applicable to your scenario:

  2. From your web browser, open the project for your organization, and select Code.

  3. Select Clone in the upper-right corner of the Code window, and copy the URL.

    Screenshot of project in web browser, with Copy icon highlighted.

  4. Open the Git command window (Git Bash on Git for Windows). Go to the folder where you want the code from the repo stored on your computer, and run git clone, followed by the path copied from Clone URL in the previous step. See the following example:

    git clone https://contoso-ltd.visualstudio.com/MyFirstProject/_git/contoso-demo
    

    Git downloads a copy of the code in a new folder for you to work with. The download includes all commits and branches from the repo.

  5. Switch your directory to the repository that you cloned.

    cd contoso-demo
    

    Keep the command window open to work in a branch.

3. Work in a branch

Git branches isolate your changes from other work being done in the project. We recommend using the Git workflow, which uses a new branch for every feature or fix that you work on. For our examples, we use the branch, users/jamal/feature1.

  1. Create a branch with the branch command.

    git branch users/jamal/feature1
    

    This command creates a reference in Git for the new branch. It also creates a pointer back to the parent commit so Git can keep a history of changes as you add commits to the branch.

    If you're working with a previously cloned repository, ensure that you've checked out the right branch (git checkout main) and that it's up to date (git pull origin main) before you create your new branch.

  2. Use checkout to switch to that branch.

    git checkout users/jamal/feature1
    

    Git changes the files on your computer to match the latest commit on the checked-out branch.

    Tip

    When you create a branch from the command line, the branch is based on the currently checked-out branch. When you clone the repository, the default branch (typically main) gets checked out. Because you cloned, your local copy of main has the latest changes.

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

    You can replace the first three commands in the previous example with the following command, which creates a new branch named users/jamal/feature1 based on the latest main branch.

    git pull origin main:users/jamal/feature1
    

    Switch back to the Git Bash window that you used in the previous section. Run the following commands to create and check out a new branch based on the main branch.

    git pull origin main:users/jamal/feature1
    git checkout feature1
    

4. Work with the code

In the following steps, we make a change to the files on your computer, commit the changes locally, and then push the commit to the repo stored on the server.

  1. Browse to the folder on your computer where you cloned the repo, open the README.md file in your editor of choice, and make some changes. Then, Save and close the file.

  2. In the Git command window, go to the contoso-demo directory by entering the following command:

    cd contoso-demo
    
  3. Commit your changes by entering the following commands in the Git command window:

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

    The git add . command stages any new or changed files, and git commit -m creates a commit with the specified commit message.

    Check which branch you're working on before you commit, so that you don't commit changes to the wrong branch. Git always adds new commits to the current local branch.

  4. Push your changes to the Git repo on the server. Enter the following command into the Git command window:

    git push origin users/jamal/feature1
    

Your code is now shared to the remote repository, in a branch named users/jamal/feature1. To merge the code from your working branch into the main branch, use a pull request.

5. Merge your changes with a pull request

Pull requests combine the review and merge of your code into a single collaborative process. After you’re done fixing a bug or new feature in a branch, create a new pull request. Add the members of the team to the pull request so they can review and vote on your changes. Use pull requests to review works in progress and get early feedback on changes. There’s no commitment to merge the changes because you can abandon the pull request at any time.

The following example shows the basic steps of creating and completing a pull request.

  1. Open the team project for your organization from your web browser and select the Code page.

  2. Select Clone in the upper-right corner of the Code page and copy the Clone URL.

    Retrieve the clone URL

  3. Open the Git command window, for example Git Bash on Git for Windows, and browse to the folder where the repo is stored on your computer.

    Run git clone followed by the path copied from the Clone URL in the previous section, as shown in the following example.

    git clone https://dev.azure.com/contoso-ltd/MyFirstProject/_git/contoso-demo
    

    Git downloads a copy of the code into a new folder for you to work with. The download includes all commits and branches from the repo.

  4. Switch your directory to the repository that you cloned.

    cd fabrikam-web
    

    Keep this command window open, because you use it in the following steps.

Your changes are now merged into the main branch, and your users/jamal/feature1 branch is deleted on the remote repository.

View history

  1. Switch back to the web portal, and select History from the Code tab to view your new commit. Two commits appear: the first commit, where the README and .gitignore were added upon repo creation, and the commit you just made.

    View commit history

  2. Switch to the Files tab, and select the README file to view your changes.

    View changed file

Clean up

Switch back to your Git Bash command prompt and run the following command to delete your local copy of the branch.

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

This action completes the following tasks:

  • The git checkout main command switches you to the main branch.
  • The git pull origin main command pulls down the latest version of the code in the main branch, including your changes and the fact that users/jamal/feature1 was merged.
  • The git branch -d users/jamal/feature1 command deletes your local copy of that branch.

Next steps