Push multiple files under a single commit through GitHub API

Intro

  1. This API needs atleast 1 commit to work. Empty repositories don’t work.

Git Database API

Creating blobs

POST https://api.github.com/repos/{user}/{repo}/git/blobs
{
"content":"print(\"hello world !\")",
"encoding": "utf-8"
}
{
"sha": "638eff25696b982124deeb1f3dfcceabfdc81a93",
"url": "https://api.github.com/repos/BRO3886/git-db-example/git/blobs/638eff25696b982124deeb1f3dfcceabfdc81a93"
}

Creating a tree

Get SHA for base_tree

GET https://api.github.com/repos/{user}/{repo}/git/trees/{branch}
{
"sha": "0d43a3b20104b4baa402c09a6c9c6c3298390e4a",
"url": "{url of tree}",
"tree": [
{
"path": "App",
"mode": "040000",
"type": "tree",
"sha": "{sha}",
"url": "{url of folder/tree}"
},
{
"path": "README.md",
"mode": "100644",
"type": "blob",
"sha": "{some SHA}",
"size": 885,
"url": "{some URL}"
},
...
],
"truncated": false
}

Create tree

POST https://api.github.com/repos/{user}/{repo}/git/trees
{
"tree":[
{
"path":"helloworld/main.py",
"mode":"100644",
"type":"blob",
"sha":"638eff25696b982124deeb1f3dfcceabfdc81a93"
},
{
"path":"helloworld/main2.py",
"mode":"100644",
"type":"blob",
"sha":"638eff25696b982124deeb1f3dfcceabfdc81a93"
}
...
],
"base_tree":"3c408bafa55eda6b1c51de5df0fc36304f37414c"
}
{
"sha": "a69117177bb067933189072b2b8799c63f388f32",
"url": "https://api.github.com/repos/BRO3886/git-db-example/git/trees/a69117177bb067933189072b2b8799c63f388f32",
"tree": [
{
"path": "README.md",
"mode": "100644",
"type": "blob",
"sha": "bc7b1321063b4075c97bf16e6f8130b6f9fa6537",
"size": 54,
"url": "https://api.github.com/repos/BRO3886/git-db-example/git/blobs/bc7b1321063b4075c97bf16e6f8130b6f9fa6537"
},
{
"path": "helloworld",
"mode": "040000",
"type": "tree",
"sha": "82a82f6788b44fe93774597ff2e76ac66ae1e657",
"url": "https://api.github.com/repos/BRO3886/git-db-example/git/trees/82a82f6788b44fe93774597ff2e76ac66ae1e657"
}
],
"truncated": false
}

Add a commit

POST https://api.github.com/repos/{user}/{repo}/git/commits
{
"tree":"a69117177bb067933189072b2b8799c63f388f32",
"message":"some commit msg",
"parents": ["3c408bafa55eda6b1c51de5df0fc36304f37414c"]
}
GET https://api.github.com/repos/BRO3886/git-db-example/git/refs/heads/{branch}
{
"ref": "refs/heads/main",
"node_id": "REF_kwDOG87gc69yZWZzL2hlYWRzL21haW4",
"url": "https://api.github.com/repos/BRO3886/git-db-example/git/refs/heads/main",
"object": {
"sha": "3c408bafa55eda6b1c51de5df0fc36304f37414c",
"type": "commit",
"url": "https://api.github.com/repos/BRO3886/git-db-example/git/commits/3c408bafa55eda6b1c51de5df0fc36304f37414c"
}
}
{
"sha": "544aa83c4d4a784c4c8490d6548c248b0e57d0ac",
"node_id": "C_kwDOG87gc9oAKDU0NGFhODNjNGQ0YTc4NGM0Yzg0OTBkNjU0OGMyNDhiMGU1N2QwYWM",
"url": "https://api.github.com/repos/BRO3886/git-db-example/git/commits/544aa83c4d4a784c4c8490d6548c248b0e57d0ac",
"html_url": "https://github.com/BRO3886/git-db-example/commit/544aa83c4d4a784c4c8490d6548c248b0e57d0ac",
...
}

Updating ref

PATCH https://api.github.com/repos/{user}/{repo}/git/refs/heads/{branch}
{
"sha":"544aa83c4d4a784c4c8490d6548c248b0e57d0ac"
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store