我建立了一个新的本地Git存储库: javascript
~$ mkdir projectname ~$ cd projectname ~$ git init ~$ touch file1 ~$ git add file1 ~$ git commit -m 'first commit'
是否有任何git命令来建立一个新的远程仓库并今后处将个人提交推送到GitHub? 我知道启动浏览器并转向建立新存储库没什么大不了的,但若是有办法从CLI实现这一点,我会很高兴。 java
我阅读了大量文章,但我没有提到如何使用git命令从CLI建立远程仓库。 Tim Lucas的好文章设置一个新的远程git存储库是我找到的最接近的, 但GitHub不提供shell访问 。 node
用于github API v3的CLI命令(替换全部CAPS关键字): python
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
有关建立令牌的说明,请转到此处这是您要键入的命令(截至本答复日期。(替换全部CAPS关键字): git
curl -u 'YOUR_USERNAME' -d '{"scopes":["repo"],"note":"YOUR_NOTE"}' https://api.github.com/authorizations
输入密码后,您将看到包含令牌的如下内容。 github
{ "app": { "name": "YOUR_NOTE (API)", "url": "http://developer.github.com/v3/oauth/#oauth-authorizations-api" }, "note_url": null, "note": "YOUR_NOTE", "scopes": [ "repo" ], "created_at": "2012-10-04T14:17:20Z", "token": "xxxxx", "updated_at": "2012-10-04T14:17:20Z", "id": xxxxx, "url": "https://api.github.com/authorizations/697577" }
你能够随时到这里撤销你的令牌 shell
若是您安装defunkt的优秀Hub工具,那么就变得如此简单 api
git create
浏览器
用做者的话说,“ hub是git的命令行包装器,能够让你在GitHub上更好。 ” app
我为GitHub和BitBucket使用REST API编写了一个名为Gitter的漂亮脚本:
https://github.com/dderiso/gitter
到位桶:
gitter -c -r b -l javascript -n node_app
GitHub的:
gitter -c -r g -l javascript -n node_app
-c
=建立新的回购 -r
= repo provider(g = GitHub,b = BitBucket) -n
=命名repo -l
=(可选)在repo中设置应用程序的语言 基于Bennedich的回答 ,我已经建立了一个Git别名来作这件事。 将如下内容添加到~/.gitconfig
:
[github] user = "your_github_username" [alias] ; Creates a new Github repo under the account specified by github.user. ; The remote repo name is taken from the local repo's directory name. ; Note: Referring to the current directory works because Git executes "!" shell commands in the repo root directory. hub-new-repo = "!python3 -c 'from subprocess import *; import os; from os.path import *; user = check_output([\"git\", \"config\", \"--get\", \"github.user\"]).decode(\"utf8\").strip(); repo = splitext(basename(os.getcwd()))[0]; check_call([\"curl\", \"-u\", user, \"https://api.github.com/user/repos\", \"-d\", \"{{\\\"name\\\": \\\"{0}\\\"}}\".format(repo), \"--fail\"]); check_call([\"git\", \"remote\", \"add\", \"origin\", \"git@github.com:{0}/{1}.git\".format(user, repo)]); check_call([\"git\", \"push\", \"origin\", \"master\"])'"
要使用它,请运行
$ git hub-new-repo
从本地存储库中的任何位置,并在提示时输入您的Github密码。