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


当前回答

找到了我喜欢的解决方案: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用户帐户上创建一个新的公共远程存储库。

其他回答

如何使用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演示;

由于代表的原因,我不能将其作为注释添加(它最好与bennedich的答案一起使用),但对于Windows命令行,以下是正确的语法:

curl -u YOUR_USERNAME https://api.github.com/user/repos -d "{\"name\":\"YOUR_REPO_NAME\"}"

这是相同的基本形式,但必须使用双引号(")而不是单引号,并使用反斜杠转义POST参数中发送的双引号(在-d标志之后)。我还删除了我的用户名周围的单引号,但如果你的用户名有空格(可能吗?),它可能需要双引号。

找到了我喜欢的解决方案: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用户帐户上创建一个新的公共远程存储库。

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

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