Using GIT CMD prompt with Visual Studio Team Services

You will need to go online to your visual studio online subscription and create a new Visual Studio Team Services www.visualstudio.com.

First we need to create alternative credentials in such way we can login to the GIT repository in visual studio online from the command line. 

With Oauth your users don't have to provide their Visual Studio Team Services credentials to use when the APIs are called. To get started on your app, though, you can authenticate using personal access tokens.

Create personal access tokens to authenticate access

  1. Sign in to either your Visual Studio Team Services account (https://{youraccount}.visualstudio.com) or your Team Foundation Server web portal (https://{server}:8080/tfs/).

  2. From your home page, open your profile. Go to your security details.

    Account home page, open your profile, go to My security

    TFS home page, open your profile, go to Security

  3. Create a personal access token.

    Add a personal access token

  4. Name your token. Select a lifespan for your token.

    If you're using Team Services, and you have more than one account, you can also select the Team Services account where you want to use the token.

    Name your token, select a lifespan. If using Team Services, select an account for your token

  5. Select the scopes that this token will authorize for your specific tasks.

    For example, to build and release apps with a Windows, OSX, or Linux agent, limit your token's scope to Agent Pools (read, manage) .

  6. When you're done, make sure to copy the token. You'll use this token as your password.

    Use token as the password for your git tools or apps

    Note: Remember that this token is your identity and acts as you when it's used. Keep your tokens secret and treat them like your password.

    Tip: To keep your token more secure, use credential managers so that you don't have to enter your credentials every time. Here are some recommended credential managers:

    For example, if you use the Git command prompt to run a Git command in Team Services, you'll be prompted for a username and password.

     git clone https://{account}.visualstudio.com/DefaultCollection/_git/{team project}
    

    Enter a username that does not contain an @ character (for example, Jamal, not fabrikamfiber4@hotmail.com). Use the token that you created as your password.

     Username for 'https://fabrikam-inc.visualstudio.com': Jamal
    Password for 'https://fabrikam-inc.visualstudio.com': [COPY THE TOKEN HERE]
    

Revoke personal access tokens to remove access

When you don't need your token anymore, just revoke it to remove access.

  1. Go to your team project's home page and open your profile.

    Account home page, open your profile, go to My security

    TFS home page, open your profile, go to Security

  2. Revoke access.

    Revoke a token or all tokens

Here's a sample that gets a list of builds using curl.

 curl -u username[:{personalaccesstoken}] https://{account}.visualstudio.com/DefaultCollection/_apis/build/builds

Here it is in C# using the HttpClient class.

 public static async void GetBuilds()
{
    try
    {
        var username = "username";
        var password = "password";

        using (HttpClient client = new HttpClient())
        {
            client.DefaultRequestHeaders.Accept.Add(
                new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                Convert.ToBase64String(
                    System.Text.ASCIIEncoding.ASCII.GetBytes(
                        string.Format("{0}:{1}", username, password))));

            using (HttpResponseMessage response = client.GetAsync(
                        "https://{account}.visualstudio.com/DefaultCollection/_apis/build/builds").Result)
            {
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();
                Console.WriteLine(responseBody);
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

When your code is working, it's a good time to switch from basic auth to OAuth.

What is the Url of my GIT repository?

So imagine your visual studio online account is called “MyAccount” and you created a new project “MyProject, then the entry point is:

https://MyAccount.visualstudio.com/DefaultCollection/MyProject

But, the URL of the GIT repository in visual studio will have following format.

https://MyAccount.visualstudio.com/DefaultCollection/_git/MyProject

We’ll need this last URL when we will clone our newly created remote repository locally.

Basic GIT commands

Following commands are the strict minimal you need when working with GIT

Clone the remote repository locally

Git Clone

https://MyAccount.visualstudio.com/DefaultCollection/_git/MyProject

So, make sure to use and Url in the correct format (with the _git subPath). When cloning a remote repository, you will be asked your credentials that created earlier. Note that when using GitHub you can clone public repositories without specifying userName/password. Quite obvious, given they are public.

Note also that the process of cloning a repository will also take care of creating a parent folder for your project. So, since the above repo is called “MyProject”, it’s not necessary to first create yourself a parent folder for this. Just go the folder where you store all your GIT local repositories and execute over there the git clone, which will create the “MyProject” folder for you. What is very important is that after cloning the repository you need to step into that newly created folder by typing

CD “MyProject”.

Create an empty Git repository

Git Init

You’ll not need to do a git init when you cloned a remote repository.

Add file contents to the index

Git Add –A

This command will figure out what changed locally and prepare things for the next step, the actual local commit.

Record changes to the repository

Git commit –m “here goes my comment”

So, note that it’s a local commit. In the next step we’ll push things to visual studio online.

Update the remote repository

Git push origin master