Github API - create branch?

后端 未结 3 554
遥遥无期
遥遥无期 2020-12-13 04:14

Seems like it\'s missing from the \"Repos\" docs for v1, v2, and v3...how do I create a branch using the Github API?

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-13 04:29

    Struggled to do this for Private repos thus answering the question for same cases:

    For private repos you need to have the requests authenticated via "username" & "password" or OAUTH. Steps below;

    1. Find the Personal access token for your account in github developer settings.

    2. Make an authenticated GET request with base branch name.

    3. From the response of GET request filter the commit SHA using jq.

    4. Post the new branch name and commit SHA as body in POST request to github.

    In Action:

    TOKEN=
    Previous_branch_name=ABC
    New_branch_name=XYZ
    
    SHA=$(curl -H "Authorization: token $TOKEN" https://api.github.com/repos//git/refs/heads/$Previous_branch_name | jq -r '.object.sha')
    
    curl -X POST -H "Authorization: token $TOKEN" \
    -d  "{\"ref\": \"refs/heads/$New_branch_name\",\"sha\": \"$SHA\"}" \ "https://api.github.com/repos//git/refs"
    

提交回复
热议问题