Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 3
Message 1 of 15

Cannot change default branch

Solved! Go to Solution.

Hi

 

I am having trouble with changing the default branch from master to new_branch using the api.

The error that I am getting is

{
"message": "Validation Failed",
"errors": [
{
"message": "The branch new_branch was not found. Please push that ref first or create it via the Git Data API.",
"resource": "Repository",
"field": "default_branch",
"code": "invalid"
}
],
"documentation_url": "https://developer.github.com/enterprise/2.15/v3/repos/#edit"
}

 

But I can change it on website. Is there a reason why I cannot do that using the API?

If I can change it on website, it should be the same when I change it using API, right?

I' also tried to fetch the the branches of the repo using API and my target branch is there. then, there is no protection on those branches. 

JSON data passed:

{
"name" : "git_practice_1",
"default_branch" : "devel"
}
PATCH request

Please let me know if you need more information.

I hope somebody can address my issue.

 

Thanks and more power.

14 Replies
Community Manager
Message 2 of 15

Re: Cannot change default branch

I can't say why you are running into this problem without being able to look at the repository in question. I would suspect that there is a typo somewhere that is messing things up.

 

I'm not sure what you mean by "there is no protection on those branches", can you please give some more details?

Highlighted
Copilot Lvl 3
Message 3 of 15

Re: Cannot change default branch

I am having the same exact problem. This is on a github enterprise, but I can copy/paste some output here:

 

Looking at existing branches:

$ hub api repos/rm-you/testrepo/branches | jq
[
  {
    "name": "master",
    "commit": {
      "sha": "5e1b32a925e4288b1e173d2e33e549baed3136f6",
      "url": "https://mygithub/api/v3/repos/rm-you/testrepo/commits/5e1b32a925e4288b1e173d2e33e549baed3136f6"
    }
  },
  {
    "name": "new_branch",
    "commit": {
      "sha": "ade39bea2d561bfb87a3e40cf55a0d4079540bef",
      "url": "https://mygithub/api/v3/repos/rm-you/testrepo/commits/ade39bea2d561bfb87a3e40cf55a0d4079540bef"
    }
  }
}

Which one is the default now?

$ hub api repos/rm-you/testrepo | jq .default_branch
"master"

Ok, let's change it to "new_branch":

$ hub api repos/rm-you/testrepo -X PATCH -F default_branch="new_branch" | jq
{
  "message": "Validation Failed",
  "errors": [
    {
      "message": "The branch new_branch was not found. Please push that ref first or create it via the Git Data API.",
      "resource": "Repository",
      "field": "default_branch",
      "code": "invalid"
    }
  ],
  "documentation_url": "https://developer.github.com/enterprise/2.15/v3/repos/#edit"
}

So, it failed to find the branch that it just listed. That's ... not great. :(

As far as branch protections, I think they were implying that possibly branch protections could prohibit a branch from becoming the default, or from changing the default? I don't think that's the case however, so it shouldn't matter. They were just adding some extra info. I actually do have protections set up on both the existing default branch and the new branch, but I don't believe it should make a difference.

Ground Controller Lvl 1
Message 4 of 15

Re: Cannot change default branch

Same problem here  (branch not found, but it exists). And I'm unable to change even on the GitHub website :-(

 

On the Chrome Developer Console, it appears:

An invalid form control with name='name' is not focusable.
Ground Controller Lvl 1
Message 5 of 15

Re: Cannot change default branch

If you're experiencing this in the UI, open the branch selection element and check if there are two options simultaneously picked. This happened to me when I typed the branch name and pressed ENTER: two branches were selected at the same time.

 

Manually clicking the branch name wil de-select the other and you'll be able to update the branch.

Copilot Lvl 3
Message 6 of 15

Re: Cannot change default branch

Following up, I decided to test this on public Github (really should have done this immediately, not sure why I didn't) and the issue appears there as well.

 

For example, on this repo: https://github.com/rm-you/devstack_deploy

The default branch is `master`, and there exists a branch `centos_new`.

 

I tried setting `centos_new` as the default branch, but got the same error.

$ hub api repos/rm-you/devstack_deploy -X PATCH -F default_branch=centos_new
{"message":"Validation Failed","errors":[{"message":"The branch centos_new was not found. Please push that ref first or create it via the Git Data API.","resource":"Repository","field":"default_branch","code":"invalid"}],"documentation_url":"https://developer.github.com/v3/repos/#edit"}

I *was* able to set it as the default in the UI though, and then the API shows it as the default branch.

$ hub api repos/rm-you/devstack_deploy | jq .default_branch
"centos_new"

What happens when I try to switch back to `master` via the API?

$ hub api repos/rm-you/devstack_deploy -X PATCH -F default_branch=master
{"message":"Validation Failed","errors":[{"message":"The branch master was not found. Please push that ref first or create it via the Git Data API.","resource":"Repository","field":"default_branch","code":"invalid"}],"documentation_url":"https://developer.github.com/v3/repos/#edit"}

This is clearly broken. Is this the wrong place to be asking about it? Should we file an issue somewhere else?

Copilot Lvl 3
Message 7 of 15

Re: Cannot change default branch

Could someone else possibly test this and confirm? If this API call is broken for everyone (as I expect it is), I wonder if it was recent, or if it is just so uncommonly used that no one noticed yet and actually bothered to complain. :D

Ground Controller Lvl 1
Message 8 of 15

Re: Cannot change default branch

I can confirm that this API endpoint is indeed malfunctioning. I'm using requests library for Python to do the following:

u = "https://api.github.com/repos/<>/<>"
h =  {'Authorization': 'Token <>'}
requests.patch(u, headers=h, json={'default_branch': 'master'}).json()


This is what I'm getting as a result:

{'message': 'Validation Failed',

'errors': [{'message': 'The branch master was not found. Please push that ref first or create it via the Git Data API.',

   'resource': 'Repository',

   'field': 'default_branch',

   'code': 'invalid'}],

'documentation_url': 'https://developer.github.com/v3/repos/#edit'}

Branch with that name does exist

 

Copilot Lvl 3
Message 9 of 15

Re: Cannot change default branch

Thanks for taking your time to verify! Appreciate it. That's enough datapoints that I'm feeling confident it's not just some weird edge case. :)

Copilot Lvl 3
Message 10 of 15

Re: Cannot change default branch

Adding up to the conversation, I also tried to do this on enterprise and on public but the issue remains the same.

To summarize,

Given:

 - setting the default branch on website is working fine

 - the desired branch is existing

 - in my case, that branch has no protections or any rules that was set

Problem:

 - setting the default branch using API is returning an error

 

my repo: https://github.com/rhobinjay/git_practice_1

default branch: master

 

How I did it?

app used to execute the request: Postman

action: PATCH

link: https://api.github.com/repos/rhobinjay/git_practice_1

json data: { "default_branch" : "devel" }

 

 

I'd like to add another test case where I executed the api with the same default branch currently setup on the repo. It returned fine and expected but not when I intented to change the default branch to a different one that is existing.