2

I want to be able to create an initialize the main branch of a repository without it having any files in it through the Github API. Normally this would be done like so with git:

git init
git remote add origin <url>
git commit --allow-empty
git push origin main

However with the Github API I am able to easily create the repository but I am unable to commit an empty commit to it. Using the empty tree and committing that only causes the Github API to say that the repository is empty, and then do nothing. Ideally I would like to not have to create a dummy file and then delete it, but that appears to be the only way currently. Please let me know if there is any way to create an empty commit without the use of a dummy file.

tl;dr: Is there any ways to emulate git's --allow-empty commit using the Github API?

Asra
  • 151
  • 1
  • 11

1 Answers1

1

There is no native way to include an --allow-empty option.

As documented in "GitHub v3 API - how do I create the initial commit in a repository?", you can use the create commit endpoint to create a commit that points to the empty tree, after having created a dummy file.

The update reference endpoint can then make the branch point to the commit you just created.

curl \
  -X PATCH \
  -H "Accept: application/vnd.github+json" \ 
  -H "Authorization: token <TOKEN>" \
  https://api.github.com/repos/OWNER/REPO/git/refs/main \
  -d '{"sha":"aa218f56b14c9653891f9e74264a383fa43fefbd","force":true}'

With aa218f56b14c9653891f9e74264a383fa43fefbd the SHA of the empty commit created before.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I actually took a look at that before making this question, the problem is when trying to commit something that points to the empty tree I get no response, here is the request I am making: `curl -X POST -H "Accept: application/vnd.github+json" -H "Authorization: token $(cat )" https://api.github.com/repos/USER/REPO/git/commits -d '{"message":"Initial Commit","tree":"4b825dc642cb6eb9a060e54bf8d69288fbee4904"}'` I know that the token does work because I am able to send a create file request and it works just fine. – Asra Jul 23 '22 at 17:58