You can't do it without an Access Token. Also, please feel free to look at my GitHub open source project Git-Captain.
I created a web-application with a Node.js back-end and HTML/JS front-end that you can setup to have an API do many of these calls for you. It has a step-by-step for a windows server and I'll be adding a Linux step-by-step soon.
It would only take a slight tweak to the project to add a new end-point to the source to do this for you.
To answer your question,
The GitHub API documentation explains exactly how to do what you are requesting on this link.
Giving this example:
as you requested in CURL and obviously replace the token "5199..." with your own:
curl -i -H "Authorization: token 5199831f4dd3b79e7c5b7e0ebe75d67aa66e79d4" \
-d '{ \
"name": "blog", \
"auto_init": true, \
"private": true, \
"gitignore_template": "nanoc" \
}' \
https://api.github.com/user/repos
OR
Not in CURL and according to this StackOverflow question you can do the following:
https://api.github.com/orgs/<organisation_name>/repos?access_token=<generated token>
or
https://api.github.com/users/<username>/repos?access_token=<generated token>
In body, pass this as a payload:
{
<br/>"name": "<Repo Name>",<br/>
"description": "<Whateveryour description is>",<br/>
"homepage": "https://github.com",<br/>
"private": false,<br/>
}
You can get a "personal access token in GitHub" by going to Settings->Developer Settings-> Personal Access Tokens->Generate new token

OR do all of the following
- Write a script (let's call this script #1) that takes the username,password, and repoName as a parameter.
- That script will call script #2, which is
curl -u ' USER-NAME-HERE' https://api.github.com/user/repos -d '{"name": "REPO-NAME-HERE"}'which will prompt for your user password, - have your script #1 listen for script #2's response and then have it enter in the password which the user passed in as a parameter in script#1
- Finally programmatically hit enter which fires off the curl to create your repo.
UPDATE*
So for some reason, the CURL won't work at all, but the Git-Hub API end point https://api.github.com/user/repos does indeed work. Using POSTMAN, I was able to create a new POST with the URL being https://api.github.com/user/repos and the BODY set to:
{
"name": "Hello-World",
"description": "This is your first repository",
"homepage": "https://github.com",
"private": false,
"has_issues": true,
"has_projects": true,
"has_wiki": true
}
Then I went to the 'Authorization' section of postman and under 'Type' I selected "Basic Auth" entered my username and password. Clicked update request and then send and my repo was created!
Answer from JoeTavarez on Stack OverflowI put my working curl here, if someone need this.
For easy push, it's better make your file in base64 format (base64 {filename} or online tools) and then:
curl -X PUT -H "Authorization: token xxxxxxxxxxxxxxxxxxxx" https://api.github.com/repos/{owner}/{repo}/contents/{path}/{fileName} -d "{\"message\":\"your commit message\",\"content\":\"your file in BASE64\"}"
It should be the "Create or update file contents" endpoint API:
PUT /repos/{owner}/{repo}/contents/{path}
(I don't see contents in your https://api.github.com/repos/username/repo/newFile.txt URL)
Make sure to use a recent PAT (Personal Access Token), as seen here.
And an example:
#! /bin/bash
cartella= " /var/myfolder "
# update the file
curl -i -X โโPUT -H ' Authorization: token 4d013330xxxxxxxxxxxxxxx ' -d " { \" path \" : \" mattei.csv \" , \
\" message \" : \" update \" , \" content \" : \" $( openssl base64 -A -in $cartella /mattei.csv ) \" , \" branch \" : \" master \" , \
\" sha \" : $( curl -X GET https://api.github.com/repos/username/repo/contents/mattei.csv | jq .sha ) } " \
https://api.github.com/repos/username/repo/contents/mattei.csv
You were close:) Lets assume, that
1) your login: YourUsername
2) your access token: 123a321
3) repository to be updated: YourRepo
4) file to be created: file.txt
5) folder that will contains new file: f1/f2
According to those assumptions your request should be following:
type : PUT
url : https://api.github.com/repos/YourUsername/YourRepo/contents/f1/f2/file.txt
headers :
{
"Content-Type" : "application/vnd.github.v3+json",
"Authorization" : "token 123a321"
}
body :
{
"message": "my commit message",
"committer": {
"name": "My name",
"email": "my email"
},
"content": "base64encoded"
}
UPD
If you write in java, you can use GitHubFileAPI library, that I recently pushed to the maven central repository.
Solution: In order to perform action on github api you can use curl according to Github doc:
- first make sure you have specific privileges and authentication token generated to your account in order to interact with Github api:
profile Settings -> Developer Settings -> Personal access tokens -> Generate new token
- the command should be in http
PUTmethod on path/repos/:owner/:repo/contents/:path - The required params for editing or adding new files (according to api) message (String), Content (String) and sha(String), additional params are branch(String) commiter(String) and author(String)
Lets perform some curl message in order to perform your action, the simple formula for this case would be:
curl --user <userName>:<token>
--data '{"key":"value"}'
--header <HeaderType>
--request <HTTPMethod>
<fullURL>
for demonstration lets fill some details:
curl --user johnDoe:abc123!@#
--data '{"message":"my message","content":"my content","sha":"abcjfhtenf736gd843ji43903"}'
--header Content-Type:application/json
--request PUT
https://api.github.com/repos/MyOrganization/MyCoolApp/contents/app/models
- Verify on Github the content has been inserted correctly to your branch