我创建了一个新的本地Git存储库:

~$ mkdir projectname
~$ cd projectname
~$ git init
~$ touch file1
~$ git add file1
~$ git commit -m 'first commit'

有没有任何git命令来创建一个新的远程回购,并从这里将我的提交推到GitHub ?我知道打开浏览器去创建一个新的存储库并不是什么大问题,但是如果有一种方法可以从CLI实现这一点,我会很高兴。

我读了大量的文章,但没有一篇提到如何使用git命令从CLI创建远程回购。Tim Lucas的一篇不错的文章“设置一个新的远程git存储库”是我找到的最接近的文章,但是GitHub不提供shell访问。


当前回答

更新20200714

Github有了一个新的官方CLI。

...gh是一个新项目,帮助我们探索官方GitHub CLI工具在完全不同的设计下可以是什么样子。虽然这两个工具都将GitHub带到终端,但hub充当git的代理,而gh是一个独立的工具。

与hub的核心区别在于,它不会覆盖现有的git命令。所以,你不能像使用hub一样,将git别名为gh。

我们没有将它添加到hub,因为我们决定将GitHub CLI设计成与git包装器不同的方式,即作为它自己的命令。事实证明,维护一个作为git代理的可执行文件是很难维护的,而且我们也不想被总是保持git兼容性所限制。我们不想在一个从一开始就很脆弱的范例上构建GitHub CLI。

——mislav(轮毂维护人员)

原来的答案

你需要的是枢纽。Hub是git的命令行包装器。它已经使用别名与本地git集成。它试图提供github操作到git,包括创建新的存储库。

→  create a repo for a new project
$ git init
$ git add . && git commit -m "It begins."
$ git create -d "My new thing"
→  (creates a new project on GitHub with the name of current directory)
$ git push origin master

其他回答

如果您安装了defunkt的优秀Hub工具,那么这将变得非常简单

中心创建

用作者的话来说,“hub是git的命令行包装器,可以让你更好地使用GitHub。”

github API v3的CLI命令(替换所有CAPS关键字):

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

不,你必须打开浏览器至少一次才能在GitHub上创建你的用户名,一旦创建,你可以利用GitHub API从命令行创建存储库,如下命令:

curl -u 'github-username' https://api.github.com/user/repos -d '{"name":"repo-name"}'

例如:

curl -u 'arpitaggarwal' https://api.github.com/user/repos -d '{"name":"command-line-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密码。

找到了我喜欢的解决方案:https://medium.com/@jakehasler/ howto -create-a-remote-git-repo- fromm-the-command -line-2d6857f49564

你首先需要创建一个Github个人访问令牌

打开你的~/。Bash_profile或~/。Bashrc在您最喜欢的文本编辑器。在文件顶部附近添加以下一行,这里是导出的其他变量所在的位置:

出口GITHUB_API_TOKEN = < your-token-here >

在下面的某个地方,通过你的其他bash函数,你可以粘贴类似于下面的东西:

function new-git() {
    curl -X POST https://api.github.com/user/repos -u <your-username>:$GITHUB_API_TOKEN -d '{"name":"'$1'"}'
}

现在,当你创建一个新项目时,你可以运行命令$ new-git awesome-repo在你的Github用户帐户上创建一个新的公共远程存储库。