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

其他回答

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

如何使用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官方的新命令行界面:

gh repo create

请参阅其他详细信息、选项和安装说明。


例如,要完成git工作流:

mkdir project
cd project
git init
touch file
git add file
git commit -m 'Initial commit'
gh repo create
git push -u origin master

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

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

中心创建

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