5

How can I create a new branch in Bitbucket repository using REST API?

I'm using Postman client.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
snir.tur
  • 461
  • 3
  • 7
  • 14
  • Don't know why this was closed. It's specific enough that it's the exact question and answer I was looking for – Dan Oct 02 '19 at 09:10
  • @Dan It doesn't specify is it BitBucket Server, or online flavor, which version and few more details that could narrow the answer – Ewoks Mar 25 '21 at 14:31

4 Answers4

7

I will show you how to create a branch in Bitbucket using postman and programmatically.

Using Postman

Select method type as POST

Add url: https://example.com/git/rest/api/1.0/projects/{projectKey}/repos/{repoName}/branches

Add Authorization to Basic Auth.

Username and password.

Select Body as raw

Select JSON(application/json)

add this to the body as JSON

{
    "name": "feature/my-feature-branch",
    "startPoint": "refs/heads/master"
}

Click on Send

Now Same programmatically

String authToken = "xyzxyzabcabcabcxyzxyzabcabcabcxyzxyzabcabcabc";

 public boolean createBranchProgrammatically(String projectKey, String repoName, String branchPrefix,String branchName,
            String headStart) {
        Map branches = new HashMap();
        JSONObject json = new JSONObject();
            try {
                String branch = branchPrefix + "/" + branchName;
                json.put("name", branch);
                json.put("startPoint", headStart);
                branches = restTemplate.exchange(myBitbuketUrl + "git/rest/api/1.0/projects/"
                        + projectKey + "repos" + repoName + "/branches",
                        HttpMethod.POST, postRequestEntityForBitbuket(json.toString()), Map.class).getBody();

                break;
            } catch (RestClientException e) {
                logger.error("Branches could not be created from bitbucket for "  , e);            

                return false;
            }

            } 
        return true;
    }



public HttpEntity<String> postRequestEntityForBitbuket(String jsonAsString) {

        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "Basic " + authToken);
        headers.add("content-type", "application/json");

        return new HttpEntity<String>(jsonAsString, headers) ;
    }
RishiKesh Pathak
  • 2,122
  • 1
  • 18
  • 24
5

Via curl

curl -u name:password -H "Content-Type:application/json" https://SERVER_ADDRESS/rest/api/1.0/projects/INF1/repos/mariaivanovatest/branches -X POST --data '{"name": "new_branch","startPoint": "refs/heads/master"}'

or if you have json file for example test.json

curl -u name:password -H "Content-Type:application/json" https://SERVER_ADDRESS/rest/api/1.0/projects/INF1/repos/mariaivanovatest/branches -X POST --data @test.json

Bogdan Mozgovyi
  • 171
  • 2
  • 1
  • Perfect, the curl command is precisely what I was looking for. Thank you! – PixelMaster Jan 17 '20 at 15:32
  • is this BitBucket (online) or BitBucket Server (self hosted / on premise) API? Most probably is the not self hosted flavor, but SERVER_ADDRESS in example brought confusion. Any reference to documentation would help. Thanks – Ewoks Mar 25 '21 at 14:27
2

You can create a branch in specific repo by /rest/branch-utils/1.0/projects/{projectKey}/repos/{repositorySlug}/branches . Please take a look at this document for further information.

Saleh Parsa
  • 1,415
  • 13
  • 22
0

I think what saleh has shared is for stash not bitbucket.

As far as this issue says, bitbucket yet not support API for creating branch

https://bitbucket.org/site/master/issues/12295/add-support-to-create-delete-branch-via

  • 1
    FYI, Stash was rebranded under the Bitbucket name in September 2015, and these days the products are named Bitbucket Cloud (bitbucket.org) and Bitbucket Server (Stash/self-hosted) – daveruinseverything May 07 '17 at 11:22