Unable to create a new branch in a repo using Bitbucket API 2.0

Akshay Maldhure February 10, 2020

I want to create a new branch named test-branch-name from the master branch in a given repository. So as per the documentation provided here, I tried creating a new branch in the repository using Postman.

curl --location --request POST 'https://api.bitbucket.org/2.0/repositories/<workspace>/<repo_name>/refs/branches' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic <auth_token>' \
--data-raw '{
    "name" : "test-branch-name",
    "target" : {
        "hash" : "default"
    }
}'

With above-mentioned request, I get this response.

{
    "type": "error",
    "error": {
        "fields": {
            "target.hash": "Commit not found: default"
        },
        "message": "Bad request"
    }
}

If, instead of the default hash, I put the full commit hash of the latest commit from the master branch in the request payload, the API call creates the branch from develop branch and not from master.

What am I missing?

2 answers

1 accepted

1 vote
Answer accepted
Daniil Penkin
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 10, 2020

Hello @Akshay Maldhure,

Welcome to the Community!

A branch in Git is a pointer to a commit. Bitbucket needs to understand which commit you want to create the branch at. So the value of hash should be something identifying a commit. This can be its hash, a tag or a branch name. You get the error because your repository doesn't have any branches or tags called default, and obviously it's not a valid commit hash too.

As for master vs develop problem when using hash you mentioned, this shouldn't happen. Can you double check you used the right commit hash? The only other explanation I can think of is that master and develop point at the same commit, but I guess that's not the case.

If you're sure you used the right hash, and you still get the branch created at the wrong place, do you mind opening a support case with us and pointing there to the repo in question and dumping commands you ran with their outputs?

Cheers,
Daniil

Akshay Maldhure February 11, 2020

If, by making sure about using the right hash, you mean going to https://bitbucket.org/libertywireless/<repository_name>/commits/branch/master, clicking on the latest commit and using the full commit has which appears in the browser's address bar, then yes, I'm sure I'm using the right hash. I've created a support request https://getsupport.atlassian.com/servicedesk/customer/portal/11/BBS-128678 for this issue.

Like Daniil Penkin likes this
Karthikraj November 15, 2022

@Daniil Penkin ,

 

How to define the target --> hash for newly created repo

Please guide me to do the same.

 

Thanks&Regards

Karthikraj.M

0 votes
Karthikraj November 15, 2022

@Daniil Penkin ,

 

How to define the target --> hash for newly created repo

Please guide me to do the same.

 

Thanks&Regards

Karthikraj.M

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events