Table of contents

Get Started with Git and Team Services

Last Updated: 8/4/2016
Team Services | TFS 2015 | Visual Studio 2015 | Previous version

This guide will cover the basics so you can get up and running using Git with your code in Team Services quickly.



Get your code

To get a copy of the source code, you will clone a Team Services Git repository. Cloning creates both a copy of the source code for you to work with and all the version control information so Git can manage the source code.

If you don't have a Git repository yet, you can create one using your own code and then clone the newly created Team Services repository.

Team Services | TFS 2015 | Visual Studio 2015
  1. In Team Explorer, open up the Connect page by choosing Projects and My Teams then Manage Connections
    Cloning Visual Studio Team Services Git repositories in Visual Studio
  2. Choose Connect, select your Team Services account, choose the projects you want to work on, then click Connect.
  3. Right click on the project and click Clone.... Then enter a local folder on your drive to store the downloaded code.
    Cloning a Git Repository from a Connected Visual Studio Team Services Account

  1. Download and install Git and the Git Credential Manager for your platform.
  2. Open the Team Services web portal in your browser and find your Git repository. Copy the command line instructions from the Clone pop-up.
    Finding the Clone URL for your Git Repository in Visual Studio Team Services
  3. Navigate to the folder where you want the code stored on your local computer using the command line.
  4. From the command line, paste the command you copied from the command line instructions. It will look like this on the command prompt:
    > git clone https://fabrikam.visualstudio.com/DefaultCollection/Fabrikam/_git/FabrikamFiber
    Git will download and create your own copy of the code in a new folder for you to work with.

Commit your work

Git branches isolate your changes from other work being done in the project. The recommneded Git workflow uses a new branch for every feature or fix you work on. You make commits in your local Git repository to save your changes on that branch.

Visual Studio 2015
  1. In Team Explorer, click the drop down and choose Branches. Right click the master branch and choose New Local Branch From...
    Creating a Local Branch off the Master branch in Visual Studio
    Choose a descriptive branch name for your work to remind you and others what kind of work is in the branch.
  2. Make changes to the project. Git will keep track of changes made to the code both inside and outside of Visual Studio.
  3. When you are satisfied with the changes, save them in Git using a commit. Open up the Changes view from Team Explorer. Verify the included changes, add a message describing the changes, and click Commit.
    Committing changes to a Git branch in Visual Studio
  1. Create a branch where you will work with the code and make your changes.
    > git checkout -b my-feature
    Choose a descriptive branch name for your work to remind you and others what kind of work is in the branch. You can also use this command to start working on a branch that other team members are already working in.
  2. Make changes using your favorite tools on the code.
  3. When you're satisfied with the changes-even if you aren't ready to share the work-save them in Git using a commit.
    > git commit -A -m "descriptive message"
    This will save your changes locally to a new commit in Git. Make sure to give the commit a short message describing your changes after the -m flag.

Share your changes

When you are ready to share your changes with the team, you push those changes so that others can reach them. You can only push changes after you add commits to a branch.

Once you push the changes, you can create a pull request to let others know you'd like to have the changes reviewed and added to the master branch of the code.

Visual Studio 2015
  1. Open up the Synchronization view in Team Explorer. You can see the outgoing commits and share them by clicking Push if you are working with a branch that is already shared, or Publish if you are working with a newly created local branch.
    Sharing Code in Git using Push in Visual Studio
  2. Create a pull request so that others can review your changes. Open Pull Requests in Team Explorer, and click New Pull Request. Verify the remote branch to merge the changes into, such as my-feature.
    Create a Pull Request in Visual Studio
  3. You can review comments made in your pull request in a web browser on the Visual Studio Team Services project page. Once all changes are approved by the team, you complete the pull request through the web browser.
  1. Push your branch so that others can see the changes you've saved.
    > git push -u origin dev
  2. Open up the Team Services project in the web portal and browse to your repository under the "Code" tab. Click the Create Pull Request button to create a pull request for the branch that you pushed.
    Creating a new Pull Request in Visual Studio Team Services
  3. Create the pull request, adding in team members to review and approve the code changes.
  4. Once the changes are approved, complete the pull request in Visual Studio Team Services. This will pull your changes from the branch into the master branch of the code.

Sync with others

To get changes from others and keep your code up to date, you pull commits made by others and merge them into your branch. Git is very good about merging multiple changes even in the same file, but sometimes you might have to resolve a merge conflict. It's a good idea to pull your branches regularly to keep them up to date with the changes from others. This makes sure that your feature branches from your main branch are using the latest version of the code.

Visual Studio 2015
  1. Open up the Sync view in Team Explorer. You can download the latest changes to the branch you are on using the "Pull" link.
    Using Pull to download changes in Git in Visual Studio
  1. Switch to the branch where you want to download the changes others have made
    > git checkout dev
  2. Pull in the changes made by others to your local branch.
    > git pull
    
Git will download the changes and merge them with your own changes into your local branch.
© 2016 Microsoft