I'm using the v3 API and managed to list repos/trees/branches, access file contents, and create blobs/trees/commits. I'm now trying to create a new repo, and managed to do it with "POST user/repos"
But when I try to create blobs/trees/commits/references in this new repo I get the same error message. (409) "Git Repository is empty.". Obviously I can go and init the repository myself through the git command line, but would rather like if my application did it for me.
Is there a way to do that? What's the first thing I need to do through the API after I create an empty repository?
Thanks
If you want to create an empty initial commit (i.e. one without any file) you can do the following:
auto_init
option as in Jai Pandya's answer; or, if the repository already exists, use the create file endpoint to create a dummy file - this will create the branch:PUT https://api.github.com/repos/USER/REPO/contents/dummy { "branch": "master", "message": "Create a dummy file for the sake of creating a branch", "content": "ZHVtbXk=" }
This will give you a bunch of data including a commit SHA, but you can discard all of it since we are about to obliterate that commit.
POST https://api.github.com/repos/USER/REPO/git/commits { "message": "Initial commit", "tree": "4b825dc642cb6eb9a060e54bf8d69288fbee4904" }
This time you need to take note of the returned commit SHA.
PATCH https://api.github.com/repos/USER/REPO/git/refs/heads/master { "sha": "<the SHA of the commit>", "force": true }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With