How To Manage A GitHub Repository Using API
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
GitHub provides full set of API endpoints to manage all your GitHub repository using API. In this blog post, we will discuss how to manage a GitHub repository using API.
As more and more projects adopting Cloud and DevOps and entire stack from application to infrastructure is managed through CI/CD pipeline, it’s the need to the hour to able to manage everything programmatically and GitHub is no exception.
How To Manage A GitHub Repository Using API
Prerequisites:
How To Create A New GitHub Repository Using API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
## I am using Linux Shell for this demo ## Replace github_personal_access_token ## github_repository_name, github_user_name ## and github_branch_name ## Create a new GitHub repository curl -i -H "Authorization: token -d '{ "name": " "description": "GitHub repo created through API", "homepage": "http:// "auto_init": true, "private": true, "gitignore_template": "nanoc" }' \ https://api.github.com/user/repos ## Get a GitHub repository details in your account curl \ -H "Authorization: token https://api.github.com/repos/ ## List all GitHub repositories in your account curl \ -H "Authorization: token https://api.github.com/users/ |
How To Update An Existing GitHub Repository Using API
1 2 3 4 5 6 |
## Update a GitHub repository using API curl \ -X PATCH \ -H "Authorization: token https://api.github.com/repos/ -d '{"private":"false"}' |
Note: Here we have changed the repository from private to public.
How To Upload A File In GitHub Repository Using API
1 2 3 4 5 6 7 8 9 10 11 12 13 |
## Create a new file in GitHub echo 'hello world' | base64 # returns aGVsbG8gd29ybGQK curl \ -X PUT \ -H "Authorization: token https://api.github.com/repos/ -d '{"message":"file myfile1.txt added","content":"aGVsbG8gd29ybGQK"}' ## Get all commits in GitHub using API curl \ -H "Authorization: token https://api.github.com/repos/ |
How To Create A New Branch In GitHub Using API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
## Create a new branch in GitHub curl -H "Authorization: token -d '{ "ref": "refs/heads/ "sha": "44e5949f55599f486fe92e1677b6c95b019e0335" }' \ https://api.github.com/repos/ ## You can get the commit sha like below ## curl \ ## -H "Authorization: token ## https://api.github.com/repos/ ## List all branches in GitHub curl -H "Authorization: token https://api.github.com/repos/ ## Create a new file in GitHub branch echo 'welcome to curl \ -X PUT \ -H "Authorization: token https://api.github.com/repos/ -d '{"message":"file myfile2.txt added","content":"d2VsY29tZSB0byBjbG91ZGFmZmFpcmUK","branch":" |
How To Merge Branches In GitHub Using API
1 2 3 4 5 6 |
## Merge two branches in GitHub curl \ -X POST \ -H "Authorization: token https://api.github.com/repos/ -d '{"base":"main","head":" |
How To Delete A Branch In GitHub Using API
1 2 3 4 5 |
## Delete the feature branch in GitHub using API curl \ -X DELETE \ -H "Authorization: token https://api.github.com/repos/ |
How To Delete A Repository In GitHub Using API
1 2 3 4 5 |
## Delete the GitHub repository curl \ -X DELETE \ -H "Authorization: token https://api.github.com/repos/ |
Hope you have enjoyed this article. To know more about GitHub, please refer below official documentation
https://docs.github.com/en/rest/reference/repos