根据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密码。
curl -F 'login=username' -F 'token=API Token' https://github.com/api/v2/yaml/repos/create -F name=reponame
。您可以在GitHub站点上找到您的API令牌,单击帐户设置,查找管理信息和API令牌(长32个字符的字符串)。