Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
610 views
in Technique[技术] by (71.8m points)

git - Why does GitHub not let us create our repositories using the command line out-of-the-box?

Wouldn't it be nice if GitHub built this into Git Shell?

Apparently Stack Overflow says we have to use cURL to do it.

curl -u 'USER' https://api.github.com/user/repos -d '{"name":"REPO"}'
# Remember replace USER with your username and REPO with your repository/application name!
git remote add origin git@github.com:USER/REPO.git
git push origin master

But it doesn't work for me. I get a 401 Unauthorized if I use this method: enter image description here

Note: --cacert is just to include a cert for SSL in my root directory

Hrm. Okay. So then I found out that the official docs do it with an authorization token:

$ curl -i -H 'Authorization: token 5199831f4dd3b79e7c5b7e0ebe75d67aa66e79d4' 
    -d '{ 
        "name": "blog", 
        "auto_init": true, 
        "private": true, 
        "gitignore_template": "nanoc" 
      }' 
    https://api.github.com/user/repos

Note: only "name" is required in the -d JSON http body according to developer.github.com/v3. We can leave out everything else and just type:

$ curl -i -H 'Authorization: token 5199831f4dd3b79e7c5b7e0ebe75d67aa66e79d4' 
    -d '{ 
        "name": "new_repo_name", 
      }' 
    https://api.github.com/user/repos

Problem is... typing in that long token (you can't CTRL-V in cmd) is going to defeat the purpose of using the CLI in the first place.

EDIT

I found hub to be a very good solution. Just install it, navigate to your github repo folder, and create a repo like this:

hub create new_repo_name

And we're done and ready for commits and pushes. No complaints so far.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Regarding your first question: This is not a “restriction” by github, but by git. Git is not specifically designed for github and thus has no support for pushing to non-existent repositories. I think github could hack that in on the server side, but that would just create a repo for every typo etc, as Ikke pointed out.

If you want nicer command-line support, you can use github’s hub tool: https://github.com/github/hub. Using that, you can create a repo with hub create.

Alternatively you could just create a small shell script yourself that contains all the required code and just takes the repository name as parameter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...