8

GitHub API docs says:

Git DB API functions will return a 409 if the git repo for a Repository is empty or unavailable.

My POST on $repo/git/trees returns 409 indeed.

How do I create initial commit in the repository then? Do I have to force user to commit some useless junk manually, just for the sake of creating root commit?

Almad
  • 5,753
  • 7
  • 35
  • 53

3 Answers3

7

Update September 28, 2012

According to this blogpost published on github blog, now it is possible to auto initialize a repository after creation.

Today we’ve made it easier to add commits to a repository via the GitHub API. Until now, you could create a repository, but you would need to initialize it locally via your Git client before adding any commits via the API.

Now you can optionally init a repository when it’s created by sending true for the auto_init parameter:

 curl -i -u pengwynn \
      -d '{"name": "create-repo-test", "auto_init": true}' \
      https://api.github.com/user/repos 

The resulting repository will have a README stub and an initial commit.

Jai Pandya
  • 2,129
  • 18
  • 29
  • Progress welcomed, but does not solve initial use-case. There, repository is always created, but has to be populated with initial commit. – Almad Feb 18 '13 at 23:01
  • @Almad I think this answers your original question, which is about creating a new commit for a new repository. In simple words, you need to make this commit at the time of creating the new repository. Previously there was no way to push initial commit via API. – Jai Pandya Feb 19 '13 at 12:33
6

I emailed github support with the same issue and here is their response:

From: Rick (GitHub Staff)
Subject: Creating an initial commit using the v3 API

Currently, you can't add the initial commit through the API.  Thanks for the
feedback. I've added your suggestion to the Feature Request List™ for the team
to see.

As a workaround, depending on your use case, you may be able to create forks rather than new repos.

Daniel X Moore
  • 14,637
  • 17
  • 80
  • 92
  • @daniel-x-moore i wanted to do the same, creating a new repository and pushing files into it (in java using api v3), so can't i still add the initial commit through the API? – cypronmaya Nov 09 '12 at 15:58
  • @cypronmaya You currently cannot add the initial commit through the API, but you can add it through the command line by shelling out manually. – Daniel X Moore Nov 12 '12 at 18:57
0

Updated July 8, 2021

I face a similar issue - I'm working on a Web App that can be integrated with GitHub. In the app's flow, a user can add a link to the GitHub repo and then commit and push files into the remote GitHub repository. I use GitHub v3 API for this.

The problem is to commit a file, you need to make a chain of requests:

  1. Get existing file tree from the repo
GET https://api.github.com/repos/USER/REPO/git/commits/COMMIT_SHA
  1. Create new file tree
  2. Create a commit

For the step 1 you need to know a commit's sha which does not exist if a repository is completelly empty.

If the repo is empty and it doesn't contain any files, you can add files there like this (with this API you can add a file without commit's sha):

PUT https://api.github.com/repos/USER/REPO/contents/YOUR_FILE_NAME

{
  "branch": "BRANCH_NAME",
  "message": "COMMIT_MESSAGE",
  "content": "ENCODED_FILE_CONTENT"
}

ENCODED_FILE_CONTENT can be get with btoa JS function, like this:

var encodedFileContent = btoa(fileString);

For example, the following request will create test.js file with content "123" and commit's message "test" in the branch with the name "main".

PUT https://api.github.com/repos/USER/REPO/contents/test.js

{
  "branch": "main",
  "message": "test",
  "content": "MTIz"
}
Anna Miroshnichenko
  • 1,143
  • 1
  • 7
  • 24