Search code examples
azure-devopsazure-devops-extensionsazure-devops-rest-api

VSTS Extension: Initialize or create a master branch from a new repo


Is it possible to use the Visual Studio Team Services REST API to init a newly created repository?


Solution

  • Cant believe i missed this from the VSTS REST api documentation, but here is how you can create and init a new git repository on vsts and also tested it with TFS 2015 on-prem

    Createing a new repository:

    Post: http://tfs:8080/tfs/DefaultCollection/_apis/git/repositories?api-version=3.0

    Body:

    {
      "name": "AnotherRepository",
      "project": {
        "id": "e7154789-27db-4ee5-a192-4d69594c6588"
      }
    }
    

    Initializing the new repository:

    Post: http://tfs:8080/tfs/DefaultCollection/_apis/git/repositories/4968177d-1f0b-4009-a635-272af892f536/pushes?api-version=3

    Body:

    {
      "refUpdates": [
        {
          "name": "refs/heads/master",
          "oldObjectId": "0000000000000000000000000000000000000000"
        }
      ],
      "commits": [
        {
          "comment": "Initial commit.",
          "changes": [
            {
              "changeType": "add",
              "item": {
                "path": "/readme.md"
              },
              "newContent": {
                "content": "My first file!",
                "contentType": "rawtext"
              }
            }
          ]
        }
      ]
    }
    

    VSTS REST Documentation: