我创建了一个新的本地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访问。


当前回答

如何使用Bash Shell快速创建远程存储库

每次创建存储库时输入完整的代码是很麻烦的

curl -u 'USER' https://api.github.com/user/repos -d '{"name":"REPO"}' git远程添加源git@github.com:USER/REPO.git Git push origin master

一个更简单的方法是:

在/home/USER_NAME/Desktop/my_scripts目录下创建一个shell脚本githubscript.sh 修改并保存以下代码到githubscript.sh文件中

#!bin/bash
curl -u 'YOUR_GITHUB_USER_NAME' https://api.github.com/user/repos -d "{\"name\":\"$1\"}";
git init;
git remote add origin git@github.com:YOUR_GITHUB_USER_NAME/$1.git;

注意:这里$1是在调用脚本时作为参数传递的存储库名称 在保存脚本之前更改YOUR_GITHUB_USER_NAME。

为脚本文件设置所需的权限 Chmod 755 githubscript.sh 在环境配置文件中包含脚本目录。 纳米~ / . profile; 导出路径= " $路径:$ HOME /桌面/ my_scripts” 还要设置一个别名来运行githubscript.sh文件。 纳米~ / . bashrc; 别名githubrepo="bash githubscript.sh" 现在在终端中重新加载.bashrc和.profile文件。 ~ /来源。bashrc ~ / . profile (; 现在创建一个新的存储库,即demo: githubrepo演示;

其他回答

你可以使用GitHub API通过命令行创建一个GitHub repo。检查存储库API。如果你向下滚动大约三分之一的方式,你会看到一个名为“创建”的部分,解释如何通过API创建一个回购(正上方是一个部分,解释如何用API派生一个回购)。显然你不能使用git来做这件事,但是你可以使用curl这样的工具通过命令行来做这件事。

在API之外,没有办法通过命令行在GitHub上创建一个repo。正如你所注意到的,GitHub不允许shell访问等,所以除了GitHub API,创建回购的唯一方法是通过GitHub的web界面。

基于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密码。

更新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

目前公认的答案和投票最多的答案都已经过时了。密码身份验证已弃用,并将于2020年11月13日16:00 UTC移除。

现在使用GitHub API的方式是通过个人访问令牌。

你需要(替换ALL CAPS关键字):

通过网站创建个人访问令牌。是的,您必须使用浏览器,但以后所有访问都只能使用一次。安全地存储令牌。 创建回购通道

curl -H 'Authorization: token MY_ACCESS_TOKEN' https://api.github.com/user/repos  -d '{"name":"REPO"}'

或者,从一开始就设置为私有:

curl -H 'Authorization: token MY_ACCESS_TOKEN' https://api.github.com/user/repos -d '{"name":"REPO", "private":"true"}'

添加新的原点并将其推入:

git remote add origin git@github.com:USER/REPO.git
git push origin master

这样做的缺点是每次都必须输入令牌,并且它会出现在bash历史记录中。

为了避免这种情况,你可以

将头文件存储在一个文件中(我们称它为HEADER_FILE)

Authorization: token MY_ACCESS_TOKEN

curl从文件中读取了吗

curl -H @HEADER_FILE https://api.github.com/user/repos -d '{"name":"REPO"}' # public repo
curl -H @HEADER_FILE https://api.github.com/user/repos -d '{"name":"REPO", "private":"true"}' # private repo

为了更加安全,您可以将访问权限设置为400,并将用户设置为root

chmod 400 HEADER_FILE
sudo chown root:root HEADER_FILE

现在需要sudo来访问头文件

sudo curl -H @HEADER_FILE https://api.github.com/user/repos -d '{"name":"REPO"}' # public repo
sudo curl -H @HEADER_FILE https://api.github.com/user/repos -d '{"name":"REPO", "private":"true"}' # private repo

不,你必须打开浏览器至少一次才能在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"}'