Skip to content

Instantly share code, notes, and snippets.

@simonjodet
Created December 18, 2012 07:48
Show Gist options
  • Save simonjodet/4325917 to your computer and use it in GitHub Desktop.
Save simonjodet/4325917 to your computer and use it in GitHub Desktop.
Edit a file on Github through the API
curl --user "simonjodet" -i --data '{"content": "new test","encoding": "utf-8"}' https://api.github.com/repos/simonjodet/sandbox/git/blobs
HTTP/1.1 201 Created
Server: nginx
Date: Tue, 18 Dec 2012 08:13:45 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Status: 201 Created
Location: https://api.github.com/repos/simonjodet/sandbox/git/blobs/6e6ab611ec78d425b37fcb26c3d817d71e58afcb
ETag: "4774fa91e9a71a16564b43460b72d649"
Cache-Control: max-age=0, private, must-revalidate
X-Content-Type-Options: nosniff
Content-Length: 167
X-RateLimit-Remaining: 4998
X-GitHub-Media-Type: github.beta
X-RateLimit-Limit: 5000
{
"url": "https://api.github.com/repos/simonjodet/sandbox/git/blobs/6e6ab611ec78d425b37fcb26c3d817d71e58afcb",
"sha": "6e6ab611ec78d425b37fcb26c3d817d71e58afcb"
}
curl --user "simonjodet" -i --data '{"base_tree":"master","tree":[{"path":"test_file.txt","mode":"100644","type":"blob","sha":"6e6ab611ec78d425b37fcb26c3d817d71e58afcb"}]}' https://api.github.com/repos/simonjodet/sandbox/git/trees
HTTP/1.1 201 Created
Server: nginx
Date: Tue, 18 Dec 2012 08:14:16 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Status: 201 Created
X-GitHub-Media-Type: github.beta
Cache-Control: max-age=0, private, must-revalidate
Content-Length: 733
X-Content-Type-Options: nosniff
Location: https://api.github.com/repos/simonjodet/sandbox/git/trees/d29c0b78b989f0903d5ec86cecdbc07e6c3078b0
X-RateLimit-Limit: 5000
X-RateLimit-Remaining: 4997
ETag: "08896c9c4b7f43586d933d7d2d8e5774"
{
"url": "https://api.github.com/repos/simonjodet/sandbox/git/trees/d29c0b78b989f0903d5ec86cecdbc07e6c3078b0",
"tree": [
{
"type": "blob",
"path": "README",
"mode": "100644",
"url": "https://api.github.com/repos/simonjodet/sandbox/git/blobs/4e16b518b4d7482fb964a59cac1487d005c71b5c",
"sha": "4e16b518b4d7482fb964a59cac1487d005c71b5c",
"size": 20
},
{
"type": "blob",
"path": "test_file.txt",
"mode": "100644",
"url": "https://api.github.com/repos/simonjodet/sandbox/git/blobs/6e6ab611ec78d425b37fcb26c3d817d71e58afcb",
"sha": "6e6ab611ec78d425b37fcb26c3d817d71e58afcb",
"size": 8
}
],
"sha": "d29c0b78b989f0903d5ec86cecdbc07e6c3078b0"
}
curl --user "simonjodet" -i --data '{"message":"Creating a file from scratch","parents":["cc5627cb7451dc9fd564b1f05e8c47faaa40a4eb"],"tree":"d29c0b78b989f0903d5ec86cecdbc07e6c3078b0"}' https://api.github.com/repos/simonjodet/sandbox/git/commits
HTTP/1.1 201 Created
Server: nginx
Date: Tue, 18 Dec 2012 08:17:06 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Status: 201 Created
Content-Length: 830
X-Content-Type-Options: nosniff
ETag: "677b7308d2bdaeb1bbaebc7a6d362c01"
X-GitHub-Media-Type: github.beta
X-RateLimit-Remaining: 4994
X-RateLimit-Limit: 5000
Cache-Control: max-age=0, private, must-revalidate
Location: https://api.github.com/repos/simonjodet/sandbox/git/commits/abdb3709aa93bbfadbb9f63d35bb4b8e13fed491
{
"author": {
"date": "2012-12-18T08:17:06Z",
"name": "simonjodet",
"email": "[email protected]"
},
"url": "https://api.github.com/repos/simonjodet/sandbox/git/commits/abdb3709aa93bbfadbb9f63d35bb4b8e13fed491",
"tree": {
"url": "https://api.github.com/repos/simonjodet/sandbox/git/trees/d29c0b78b989f0903d5ec86cecdbc07e6c3078b0",
"sha": "d29c0b78b989f0903d5ec86cecdbc07e6c3078b0"
},
"message": "Creating a file from scratch",
"committer": {
"date": "2012-12-18T08:17:06Z",
"name": "simonjodet",
"email": "[email protected]"
},
"sha": "abdb3709aa93bbfadbb9f63d35bb4b8e13fed491",
"parents": [
{
"url": "https://api.github.com/repos/simonjodet/sandbox/git/commits/cc5627cb7451dc9fd564b1f05e8c47faaa40a4eb",
"sha": "cc5627cb7451dc9fd564b1f05e8c47faaa40a4eb"
}
]
}
curl --user "simonjodet" -i --request POST --data '{"sha":"abdb3709aa93bbfadbb9f63d35bb4b8e13fed491"}' https://api.github.com/repos/simonjodet/sandbox/git/refs/heads/master
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 18 Dec 2012 08:17:37 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Status: 200 OK
X-Content-Type-Options: nosniff
Cache-Control: max-age=0, private, must-revalidate
ETag: "847612c913ac66d86c1459211424e4a1"
X-GitHub-Media-Type: github.beta
X-RateLimit-Remaining: 4993
X-RateLimit-Limit: 5000
Content-Length: 325
{
"url": "https://api.github.com/repos/simonjodet/sandbox/git/refs/heads/master",
"ref": "refs/heads/master",
"object": {
"type": "commit",
"url": "https://api.github.com/repos/simonjodet/sandbox/git/commits/abdb3709aa93bbfadbb9f63d35bb4b8e13fed491",
"sha": "abdb3709aa93bbfadbb9f63d35bb4b8e13fed491"
}
}
* get the current commit object
curl -u "simonjodet" -i https://api.github.com/repos/simonjodet/sandbox/git/refs/
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 18 Dec 2012 07:09:24 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Status: 200 OK
X-GitHub-Media-Type: github.beta
Content-Length: 347
Cache-Control: private, s-maxage=60, max-age=60
X-Content-Type-Options: nosniff
X-RateLimit-Limit: 5000
Last-Modified: Tue, 18 Dec 2012 06:50:24 GMT
X-RateLimit-Remaining: 4997
Vary: Accept, Authorization, Cookie
ETag: "14649e370ac8ba78c8842393c66d6f7c"
[
{
"url": "https://api.github.com/repos/simonjodet/sandbox/git/refs/heads/master",
"ref": "refs/heads/master",
"object": {
"type": "commit",
"url": "https://api.github.com/repos/simonjodet/sandbox/git/commits/bad896a654678eb33505a49b29904690deeb4eaf",
"sha": "bad896a654678eb33505a49b29904690deeb4eaf"
}
}
]
-----------------------------------------------------------------------------------------------------------------------------
* retrieve the tree it points to
curl -u "simonjodet" -i https://api.github.com/repos/simonjodet/sandbox/git/trees/bad896a654678eb33505a49b29904690deeb4eaf
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 18 Dec 2012 07:10:31 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Status: 200 OK
Last-Modified: Tue, 18 Dec 2012 06:50:24 GMT
Content-Length: 458
ETag: "14649e370ac8ba78c8842393c66d6f7c"
X-RateLimit-Limit: 5000
X-GitHub-Media-Type: github.beta
Vary: Accept, Authorization, Cookie
X-RateLimit-Remaining: 4996
X-Content-Type-Options: nosniff
Cache-Control: private, max-age=60, s-maxage=60
{
"url": "https://api.github.com/repos/simonjodet/sandbox/git/trees/bad896a654678eb33505a49b29904690deeb4eaf",
"tree": [
{
"type": "blob",
"path": "README.md",
"mode": "100644",
"url": "https://api.github.com/repos/simonjodet/sandbox/git/blobs/7725eaf9c0d10e37cd68ee2bde4532b5260c702a",
"sha": "7725eaf9c0d10e37cd68ee2bde4532b5260c702a",
"size": 15
}
],
"sha": "bad896a654678eb33505a49b29904690deeb4eaf"
}
-----------------------------------------------------------------------------------------------------------------------------
* retrieve the content of the blob object that tree has for that particular file path
curl -u "simonjodet" -H "Accept: application/vnd.github.beta.raw+json" -i https://api.github.com/repos/simonjodet/sandbox/git/blobs/7725eaf9c0d10e37cd68ee2bde4532b5260c702a
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 18 Dec 2012 07:06:42 GMT
Content-Type: application/vnd.github.beta.raw+json
Connection: keep-alive
Status: 200 OK
ETag: "708e924f8f9ea5d7c7d4187396ebdc90"
Cache-Control: max-age=0, private, must-revalidate
X-Content-Type-Options: nosniff
Content-Length: 15
X-RateLimit-Remaining: 4998
X-RateLimit-Limit: 5000
sandbox
=======
-----------------------------------------------------------------------------------------------------------------------------
* change the content somehow and post a new blob object with that new content, getting a blob SHA back
curl --user "simonjodet" -i --data '{"content": "sandbox\n=======\ntest","encoding": "utf-8"}' https://api.github.com/repos/simonjodet/sandbox/git/blobs
HTTP/1.1 201 Created
Server: nginx
Date: Tue, 18 Dec 2012 07:22:10 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Status: 201 Created
Cache-Control: max-age=0, private, must-revalidate
X-GitHub-Media-Type: github.beta
X-Content-Type-Options: nosniff
Location: https://api.github.com/repos/simonjodet/sandbox/git/blobs/4e16b518b4d7482fb964a59cac1487d005c71b5c
X-RateLimit-Remaining: 4994
Content-Length: 167
ETag: "1d0a68fdc8c7e53f65099e82e7eeb82f"
X-RateLimit-Limit: 5000
{
"url": "https://api.github.com/repos/simonjodet/sandbox/git/blobs/4e16b518b4d7482fb964a59cac1487d005c71b5c",
"sha": "4e16b518b4d7482fb964a59cac1487d005c71b5c"
}
-----------------------------------------------------------------------------------------------------------------------------
* post a new tree object with that file path pointer replaced with your new blob SHA getting a tree SHA back
curl --user "simonjodet" -i --data '{"tree":[{"path":"README","mode":"100644","type":"blob","sha":"4e16b518b4d7482fb964a59cac1487d005c71b5c"}]}' https://api.github.com/repos/simonjodet/sandbox/git/trees
HTTP/1.1 201 Created
Server: nginx
Date: Tue, 18 Dec 2012 07:30:44 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Status: 201 Created
Location: https://api.github.com/repos/simonjodet/sandbox/git/trees/c141617095364ba8badb8c443f5997d5578a1a16
ETag: "47f483189dac65adc5236df24e060e43"
Cache-Control: max-age=0, private, must-revalidate
X-Content-Type-Options: nosniff
Content-Length: 455
X-RateLimit-Remaining: 4993
X-GitHub-Media-Type: github.beta
X-RateLimit-Limit: 5000
{
"url": "https://api.github.com/repos/simonjodet/sandbox/git/trees/c141617095364ba8badb8c443f5997d5578a1a16",
"tree": [
{
"type": "blob",
"path": "README",
"mode": "100644",
"url": "https://api.github.com/repos/simonjodet/sandbox/git/blobs/4e16b518b4d7482fb964a59cac1487d005c71b5c",
"sha": "4e16b518b4d7482fb964a59cac1487d005c71b5c",
"size": 20
}
],
"sha": "c141617095364ba8badb8c443f5997d5578a1a16"
}
-----------------------------------------------------------------------------------------------------------------------------
* create a new commit object with the current commit SHA as the parent and the new tree SHA, getting a commit SHA back
curl --user "simonjodet" -i --data '{"message":"my first commit with the API","parents":["bad896a654678eb33505a49b29904690deeb4eaf"],"tree":"c141617095364ba8badb8c443f5997d5578a1a16"}' https://api.github.com/repos/simonjodet/sandbox/git/commits
HTTP/1.1 201 Created
Server: nginx
Date: Tue, 18 Dec 2012 07:37:49 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Status: 201 Created
Content-Length: 830
ETag: "49ec3a2812e92ac6f4382f21d54cdbbc"
X-RateLimit-Limit: 5000
X-GitHub-Media-Type: github.beta
Location: https://api.github.com/repos/simonjodet/sandbox/git/commits/80a11ce1e116c3c5fb24c39e6cf8f1cb94098eb8
X-RateLimit-Remaining: 4992
Cache-Control: max-age=0, private, must-revalidate
X-Content-Type-Options: nosniff
{
"author": {
"date": "2012-12-18T07:37:49Z",
"name": "simonjodet",
"email": "[email protected]"
},
"url": "https://api.github.com/repos/simonjodet/sandbox/git/commits/80a11ce1e116c3c5fb24c39e6cf8f1cb94098eb8",
"tree": {
"url": "https://api.github.com/repos/simonjodet/sandbox/git/trees/c141617095364ba8badb8c443f5997d5578a1a16",
"sha": "c141617095364ba8badb8c443f5997d5578a1a16"
},
"message": "my first commit with the API",
"committer": {
"date": "2012-12-18T07:37:49Z",
"name": "simonjodet",
"email": "[email protected]"
},
"sha": "80a11ce1e116c3c5fb24c39e6cf8f1cb94098eb8",
"parents": [
{
"url": "https://api.github.com/repos/simonjodet/sandbox/git/commits/bad896a654678eb33505a49b29904690deeb4eaf",
"sha": "bad896a654678eb33505a49b29904690deeb4eaf"
}
]
}
-----------------------------------------------------------------------------------------------------------------------------
* update the reference of your branch to point to the new commit SHA
curl --user "simonjodet" -i --request POST --data '{"sha":"80a11ce1e116c3c5fb24c39e6cf8f1cb94098eb8"}' https://api.github.com/repos/simonjodet/sandbox/git/refs/heads/master
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 18 Dec 2012 07:45:00 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
Status: 200 OK
ETag: "ecf9f71b485e0c7863349133957eabc2"
Cache-Control: max-age=0, private, must-revalidate
X-Content-Type-Options: nosniff
Content-Length: 325
X-RateLimit-Remaining: 4990
X-GitHub-Media-Type: github.beta
X-RateLimit-Limit: 5000
{
"url": "https://api.github.com/repos/simonjodet/sandbox/git/refs/heads/master",
"ref": "refs/heads/master",
"object": {
"type": "commit",
"url": "https://api.github.com/repos/simonjodet/sandbox/git/commits/80a11ce1e116c3c5fb24c39e6cf8f1cb94098eb8",
"sha": "80a11ce1e116c3c5fb24c39e6cf8f1cb94098eb8"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment