Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

how to create master branch on empty repo by bitbucket api v2

Huy_Tran
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 9, 2020

Hi, I tried to use 

POST https://api.bitbucket.org/2.0/repositories/user/my_repo/refs/branches

{
  "name" : "master",
  "target" : {
    "hash" : "default"
  }
}

but I got 

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

 

I knew we need a commit, but in the new empty repo, there is no commit

1 answer

1 accepted

0 votes
Answer accepted
Daniil Penkin
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 9, 2020

Hello @Huy_Tran,

Welcome to the Community!

You need to use this endpoint, specifically note branch parameter:

When a branch name is specified and the repo is empty, the new commit will become the repo's root commit and also define the repo's main branch going forward.

Hope this helps.

Cheers,
Daniil

Huy_Tran
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 9, 2020

okay thanks @Daniil Penkin , got it

I solved by this src

request({

    url: `https://api.bitbucket.org/2.0/repositories/${user_name}/${repo_name}/src`,

    method: 'post',

    headers: { 'User-Agent': 'Request-Promise', 'content-type': 'application/x-www-form-urlencoded', Authorization: `Bearer ${access_token}` },

    form: { author: 'NewsBie <support@newbee.cccc>', message: 'empty commit' }

});

Then it auto created `master` branch.

Ajay Chandra
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 8, 2022

@Daniil Penkin I am trying to create a branch in an empty repository 
the solution given by @Huy_Tran  is working but I want my branch name as mainline instead master ..! 

is there a way to change?

Thanks, 
Ajay.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events