How to find a Github file 's SHA blob

Kiddo picture Kiddo · Nov 26, 2013 · Viewed 11.2k times · Source

I'm using this API to update a file on my repo, it requires me to have a valid SHA blob for a file that I want to update:

http://developer.github.com/v3/repos/contents/

How do I find the SHA blob for the specific file? Supposed in my testrepo in the test account here, what is the SHA blob for the test.txt file?

https://github.com/testacc01/testrepo01

Thank you so much!

Answer

user3033893 picture user3033893 · Nov 26, 2013

The docs for updating a file specify that you will need to provide the SHA for the file you will be replacing. The easiest way would be to query github for that, too. For example:

> curl https://api.github.com/repos/testacc01/testrepo01/contents/test.txt
{
  "name": "test.txt",
  "path": "test.txt",
  "sha": "4f8a0fd8ab3537b85a64dcffa1487f4196164d78",
  "size": 13,
 …

So, you can see what the SHA is in the "sha" field of the JSON response. Use that when you formulate your request to update the file with a new version. After you have successfully updated the file, the file will have a new SHA that you will need to request before it can be updated again. (Unless, I guess, your next update goes on a different branch.)