简体   繁体   中英

How do I create an orphan branch from the Github API?

I want to create the gh-pages branch from the Github API. Is there an easy way to do this?

If not, how would I create an orphan branch from the Github API?

It is possible to do this by:

  1. Check whether the branch already exists
  2. If not, then create a commit that refers to git's empty tree SHA
  3. Then create a ref to that commit

You can find some CoffeeScript example code for how to do that from https://github.com/noflo/noflo-github/blob/master/components/CreateOrphanBranch.coffee#L31

Here is one such branch created this way: https://github.com/the-domains/example.net/tree/branch_1403616324001

Update : this method only works if the git repository has previous orphan branches. If it is a newly-created repo created via GitHub's API using the auto_init option it won't work. I've contacted GitHub about this.

You can create a branch via the Create a Reference part of the API. I'm not sure, however, if you can create an orphaned branch with that or if the API would prevent that.

In fact, testing it out with curl doesn't work:

curl -X POST -u sigmavirus24 https://api.github.com/repos/sigmavirus24/github3.py/github3.py/refs -d '{"ref":"refs/heads/orphaned"}'
curl -X POST -u sigmavirus24 https://api.github.com/repos/sigmavirus24/github3.py/github3.py/refs -d '{"ref":"refs/heads/orphaned", "sha":""}'

Both return:

{"message": "Reference update failed"}

I tried with and without the Content-Type header ( -H "Content-Type: application/json" ) but neither work.

From that minor experimentation, it would appear you can not create an orphaned branch via the API.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM