Get started using Git in Visual Studio

Last Update: 3/30/2017

Team Services | TFS 2015

Whether your software project is large, small, or brand new, in most cases you'll be better off if you use version control as early as possible. Here, we'll show you how to get started with Git, a distributed version control system. If you want to work in a centralized version control system, you can instead use Team Foundation Version Control (TFVC).

Create a team project for your repositories

Team projects hold your repositories, backlogs, and builds. When you create a team project, a new repository will be automatically created for you.

If you don't already have a team project create a new team project

Install Git client tools

To use Git, you'll need to have the client tools installed on your computer.

Clone

To get started, you'll first need to clone your repo to your local machine. Once you have a local clone, you can start adding code to your repo.

  1. Go to your team's project page (https://{youraccount}.visualstudio.com/DefaultCollection/{yourteamproject}) and then open Visual Studio to connect to your team project. Click Allow if prompted to open Visual Studio.

    Click Home, then Open in Visual Studio

  2. Sign in to Visual Studio Team Services from Visual Studio.

  3. Clone the repository to your computer.

    Click the Clone Repository link

  4. The clone URL is automatically populated and a default local path is provided. Change the local path to the location where you want to store your repo. Click Clone to start copying the repo locally.

    Click the Clone button

Add code to your repo

Let's add a new app to the repo so we can make some changes.

  1. Create a new project in Visual Studio from the team explorer.

    New solution link in team explorer

  2. Create a new Console Application. Notice that the location is set to the location on disk where the empty repo was cloned and the Add to source control option is checked.

    Notice the location is the path to the local repo

  3. In Team Explorer, navigate to the Changes page. Notice that your new solution is showing up on the Home page under Solutions.

    Navigate to the Changes page

  4. The new app appears under the Included Changes section. Enter a commit message and click the Commit and Push button to commit the changes to your local repo then push your changes to Visual Studio Team Services. If you haven't previously used Git on this computer, you may have to configure your username and email address.

    Enter a message and click Commit

  5. In your browser, view the changes pushed to the server by clicking on CODE.

    See the recently pushed changes under CODE

Create a topic branch

In Git, branches are very lightweight and enable a lot of useful functionality. A common best practice is to perform all development in a branch (even minor changes and bug fixes). These types of branches are often called "topic branches" as they are created for a single task or topic of work.

Let's make a change in a topic branch to explore some additional features.

  1. In Team Explorer, navigate to the Branches page.

    Click the Branches tile

  2. On the Branches page, right-click on the master branch and choose New Local Branch From... to create a new topic branch.

    Right-click and choose New Local Branch From

  3. Enter a name for your branch and click Create Branch. When naming your branches, use slashes to organize your branches.

    Enter a branch name and click Create Branch

Make a change to your app

Now that you have a new topic branch, you're ready to start making changes to your app.

  1. Add a "hello, world" message to the console app.

    Add a "Hello, world" message

Commit

  1. Right-click on the file in Solution Explorer and choose Commit.

    Right-click and choose Commit

  2. On the Changes page, enter a commit message and click Commit to commit the changes to your topic branch.

    Enter a comment and click Commit

Publish

To share the changes in your topic branch, you'll need to publish it to the server.

  1. Click on the Sync link in the successful commit notification to open the Synchronization page.

    Click Sync to open the Sync page

  2. On the Sync page, click on the Publish link to push the changes on your topic branch to the server.

    Click Publish to push your changes

Conduct a pull request

Pull Requests are a common workflow for reviewing code created in a topic branch and merging changes. Let's create a pull request to see how it works.

  1. In your browser, open the CODE hub and click Pull Requests to view the Pull Requests hub.

    Click CODE, then Pull Requests

  2. Click on New Pull Request to create a new pull request.

    Click New Pull Request

  3. Select your topic branch from the first drop-down. The default branch (master) is the default target branch.

    Select your topic branch

  4. Verify that the preview contains the changes you want to review and click Create Pull Request.

    Click Create Pull Request

Now you can add reviewers to get their feedback on your changes before you merge your changes into master.

Pull Request with feedback

Q&A

Q: How do I create a team project on Visual Studio Team Services?

Q: How do I create a team project on an on-premises Team Foundation Server?

Q: How do I get started using Command line tools?

First you'll need to:

For the Fabrikam project on VSTS, the command to clone would look like this:

git clone https://fabrikam.visualstudio.com/DefaultCollection/_git/Fabrikam

Once you have the repo cloned, create a new topic branch:

git checkout -b users/jamal/new_branch

When you have changes to add to the repo, stage the files using:

git add .

Then commit to the local repo:

git commit -a -m "Initial commit"

When you're ready to publish your topic branch to the repo on VSTS, push the changes:

git push origin users/jamal/new_branch

Now you can return to the web portal to complete the pull request experience.

Q: How do I configure my Git username and email address?

Before you can commit in Visual Studio, you must configure the username and email address that Git will use to identify commits that you create.

On the Changes page, you will be prompted if you haven't configured your username or email address. You can also access the Git Settings page from the Settings page in Team Explorer, under the Git section, Global Settings.

Click configure to enter your settings

Enter your preferred username and email address on the Git Settings page.

Enter your username and email