Get the repository default branch of a GitHub repository

时间:2015-11-12 10:38:38

标签: git github github-api

I'm trying to use the GitHub API to get the default branch of a specified repository (owner/repo).

I didn't find any docs regarding that. To get all branches I use:

curl https://api.github.com/repos/owner/owner/branches

This outputs the following json:

[
  {
    "name": "docs",
    "commit": {
      "sha": "a3...",
      "url": "https://api.github.com/repos/owner/repo/commits/a3..."
    }
  },
  {
    "name": "master",
    "commit": {
      "sha": "8c...",
      "url": "https://api.github.com/repos/owner/repo/commits/8c..."
    }
  }
]

This is already useful because in my case, the default branch is either master (if exists) or gh-pages. But I still want to know what's the correct way to get the default branch of a GitHub repository.

1 个答案:

答案 0 :(得分:3)

The repository object contains the default_branch field.

So, I can do:

curl https://api.github.com/repos/owner/repo

{
  "id": 29...,
  "name": "repo",
  "full_name": "owner/repo",
  "owner": {...},
  "private": false,
  ...
  "default_branch": "master",
  ...
}

In this case, the default_branch has the master value.