是否可以在不打开浏览器的情况下通过 CLI 在 GitHub 上创建远程存储库?
我创建了一个新的本地 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 访问。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(25)
github API v3 的 CLI 命令(替换所有大写关键字):
CLI commands for github API v3 (replace all CAPS keywords):
您可以使用 GitHub API 通过命令行创建 GitHub 存储库。查看存储库 API。如果向下滚动大约三分之一,您将看到标题为 "Create"< 的部分/a> 解释了如何通过 API 创建存储库(正上方的部分也解释了如何使用 API 分叉存储库)。显然,您不能使用 git 来执行此操作,但您可以使用像 curl 这样的工具通过命令行来执行此操作。
在 API 之外,无法通过命令行在 GitHub 上创建存储库。正如您所指出的,GitHub 不允许 shell 访问等,因此除了 GitHub API 之外,创建存储库的唯一方法是通过 GitHub 的 Web 界面。
You can create a GitHub repo via the command line using the GitHub API. Check out the repository API. If you scroll down about a third of the way, you'll see a section entitled "Create" that explains how to create a repo via the API (right above that is a section that explains how to fork a repo with the API, too). Obviously you can't use
git
to do this, but you can do it via the command line with a tool likecurl
.Outside of the API, there's no way to create a repo on GitHub via the command line. As you noted, GitHub doesn't allow shell access, etc., so aside from the GitHub API, the only way to create a repo is through GitHub's web interface.
这可以通过三个命令来完成:(
针对 v3 Github API 进行更新)
这些命令的说明...
创建 github 存储库
定义推送位置以
将本地存储库推送到 github
This can be done with three commands:
(updated for v3 Github API)
Explanation of these commands...
Create github repo
Define where to push to
Push local repo to github
如果您安装 defunkt 优秀的 Hub 工具,那么这就变得像
hub create
一样简单,用作者的话来说,“hub 是 git 的命令行包装器,它使得你更擅长 GitHub。”
If you install defunkt's excellent Hub tool, then this becomes as easy as
hub create
In the words of the author, "hub is a command-line wrapper for git that makes you better at GitHub."
使用 Github 官方新的命令行界面:
请参阅其他详细信息和选项 和安装说明< /a>.
例如,要完成您的 git 工作流程:
With Github's official new command line interface:
See additional details and options and installation instructions.
For instance, to complete your git workflow:
简单步骤(使用
git
+hub
=> GitHub):安装 集线器 (GitHub)。
brew 安装中心
去 github.com/github/hub
否则(也Go):
转到您的存储库或创建空的存储库:
mkdir foo && cd foo && git init
。运行:
hub create
,它会第一次询问您有关 GitHub 凭据的信息。用法:
hub create [-p] [-d 描述] [-h 主页] [名称]
示例:
hub create -d Description -h example.com org_name/foo_repo
<块引用>
Hub 将提示输入 GitHub 用户名和密码第一次需要访问 API 时输入密码,并将其交换为
OAuth
令牌,并将其保存在~/.config/hub
中。要明确命名新存储库,请传入
NAME
,可选择以
ORGANIZATION/NAME
形式在组织下创建您是以下组织的成员。
使用
-p
,创建一个私有存储库,并使用-d
和-h
分别设置存储库的描述和主页URL
。为避免出现提示,请使用
GITHUB_USER
和GITHUB_PASSWORD
环境变量。然后照常提交和推送或检查
hub commit
/hub Push
。如需更多帮助,请运行:
hub help
。另请参阅:使用命令行导入 Git 存储库在 GitHub 上。
Simple steps (using
git
+hub
=> GitHub):Install Hub (GitHub).
brew install hub
go get github.com/github/hub
otherwise (having Go as well):
Go to your repo or create empty one:
mkdir foo && cd foo && git init
.Run:
hub create
, it'll ask you about GitHub credentials for the first time.Usage:
hub create [-p] [-d DESCRIPTION] [-h HOMEPAGE] [NAME]
Example:
hub create -d Description -h example.com org_name/foo_repo
Then commit and push as usual or check
hub commit
/hub push
.For more help, run:
hub help
.See also: Importing a Git repository using the command line at GitHub.
我认为有一个 官方 github gem 可以做到这一点。我会在学习过程中尝试添加更多信息,但我才刚刚发现这个宝石,所以我还不太了解。
更新:设置 API 密钥后,我可以通过
create
命令在 github 上创建一个新的存储库,但是我无法使用create-from-local
命令,该命令应该获取当前的本地存储库并在 github 上创建相应的远程版本。如果有人对此有一些见解,我很想知道我做错了什么。已有一个已提交问题。
更新:我最终确实让它发挥了作用。我不太确定如何重现该问题,但我刚刚从头开始(删除了 .git 文件夹)
现在这一行将创建远程存储库,甚至推送到它,但不幸的是我认为我不能指定我想要的存储库的名称。我希望它在 github 上被称为“dotfiles”,但 gh gem 仅使用当前文件夹的名称,即“jason”,因为我位于我的主文件夹中。 (我添加了 一张票,询问所需的行为)
此命令另一方面,确实接受一个参数来指定远程存储库的名称,但它旨在从头开始一个新项目,即在调用此命令后,您将获得一个新的远程存储库,该存储库正在跟踪本地存储库相对于当前位置新创建的子文件夹,两者的名称都指定为参数。
There is an official github gem which, I think, does this. I'll try to add more information as I learn, but I'm only just now discovering this gem, so I don't know much yet.
UPDATE: After setting my API key, I am able to create a new repo on github via the
create
command, however I am not able to use thecreate-from-local
command, which is supposed to take the current local repo and make a corresponding remote out on github.If anyone has some insight on this, I'd love to know what I'm doing wrong. There's already an issue filed.
UPDATE: I did eventually get this to work. I'm not exactly sure how to re-produce the issue, but I just started from scratch (deleted the .git folder)
Now this line will create the remote repo and even push to it, but unfortunately I don't think I can specify the name of the repo I'd like. I wanted it to be called "dotfiles" out on github, but the gh gem just used the name of the current folder, which was "jason" since I was in my home folder. (I added a ticket asking for the desired behavior)
This command, on the other hand, does accept an argument to specify the name of the remote repo, but it's intended for starting a new project from scratch, i.e. after you call this command, you get a new remote repo that's tracking a local repo in a newly-created subfolder relative to your current position, both with the name specified as the argument.
使用 Bash Shell 快速创建远程存储库
每次创建存储库时都键入完整的代码很麻烦
更简单的方法是:
githubscript.sh
文件中NB 这里
$1
是调用时作为参数
传递的存储库名称
脚本
在保存脚本之前更改
YOUR_GITHUB_USER_NAME
。script
文件所需的权限.bashrc
和.profile
文件。demo
:To Quickly Create the Remote Repository by Using a Bash Shell
It is cumbersome to type the complete code every time a repository is to be created
An easier approach is:
githubscript.sh
githubscript.sh
fileN.B. Here
$1
is therepository name
that is passed as anargument
when invoking thescript
Change
YOUR_GITHUB_USER_NAME
before saving the script.script
file.bashrc
and.profile
files in the terminal.demo
:接受的答案和到目前为止,得票最多的答案已经过时了。密码身份验证已已弃用,并将被删除世界标准时间 2020 年 11 月 13 日 16:00。
现在使用 GitHub API 的方式是通过个人访问令牌。
您需要(替换所有大写关键字):
,从一开始就将其设为私有:
这样做的缺点是您必须每次都键入令牌,并且它会出现在您的 bash 历史记录中。
为了避免这种情况,您可以
HEADER_FILE
)Both the accepted answer and the most-voted answer so far are now outdated. Password authentication is deprecated and it will be removed on November 13, 2020 at 16:00 UTC.
The way to use GitHub API now is via personal access tokens.
You need to (replace ALL CAPS keywords):
or, to make it private from the start:
This has the disadvantage that you have to type the token each time, and that it appears in your bash history.
To avoid this, you can
HEADER_FILE
)基于@Mechanical Snail 的另一个答案,除了不使用 python,我发现这太过分了。将其添加到您的
~/.gitconfig
中:Based on the other answer by @Mechanical Snail, except without the use of python, which I found to be wildly overkill. Add this to your
~/.gitconfig
:更新 20200714
Github 有一个新的官方 CLI。
与 hub 的核心区别在于它不会覆盖现有的 git 命令。因此,您不能像使用 hub 那样将 git 别名为 gh。
-- mislav(中心维护者)
原始答案< /strong>
您需要的是 hub。 Hub 是 git 的命令行包装器。它已使用别名与本机 git 集成。它尝试向 git 提供 github 操作,包括创建新存储库。
Update 20200714
Github has a new official CLI.
The core difference from hub is that this does not wrap over existing git commands. So, you can not alias git to gh like you can with hub.
-- mislav (Maintainer of hub)
Original answer
What you need is hub. Hub is a command-line wrapper for git. It has been made to integrate with native git using alias. It tries to provide github actions into git including creating new repository.
最后,GitHub 正式宣布了其所有核心功能的新 CLI。
检查此处:https://cli.github.com/
通过 HomeBrew 安装:
brew install gh
其他方式:https://github.com/cli/cli#installation然后是
其他可用功能。
现在您可以从终端创建存储库。
Finally, it happened GitHub has officially announced their new CLI for all the core features.
check here: https://cli.github.com/
To install via HomeBrew:
brew install gh
for other Ways : https://github.com/cli/cli#installationthen
Other available features.
So now you can create repo from your terminal.
不,您必须至少打开一次浏览器才能在 GitHub 上创建您的
用户名
,创建后,您就可以利用GitHub API 从命令行创建存储库,遵循以下命令:例如:
Nope, you have to open a browser atleast once to create your
username
on GitHub, once created, you can leverage GitHub API to create repositories from command line, following below command:For example:
对于使用双因素身份验证的用户,您可以使用 bennedich 的解决方案,但只需为第一个命令添加 X-Github-OTP 标头即可。将 CODE 替换为您从双因素身份验证提供商处获取的代码。将 USER 和 REPO 替换为存储库的用户名和名称,就像在他的解决方案中一样。
For users with two-factor authentication, you can use bennedich's solution, but you just need to add the X-Github-OTP header for the first command. Replace CODE with the code that you get from the two-factor authentication provider. Replace USER and REPO with the username and name of the repository, as you would in his solution.
我使用 GitHub 和 BitBucket 的 REST API 为此编写了一个名为 Gitter 的漂亮脚本:
https: //github.com/dderiso/gitter
BitBucket:
GitHub:
-c
= 创建新仓库-r< /code> = 存储库提供者(g = GitHub、b = BitBucket)
-n
= 命名存储库-l
= (可选)设置存储库中应用程序的语言I wrote a nifty script for this called Gitter using the REST APIs for GitHub and BitBucket:
https://github.com/dderiso/gitter
BitBucket:
GitHub:
-c
= create new repo-r
= repo provider (g = GitHub, b = BitBucket)-n
= name the repo-l
= (optional) set the language of the app in the repo对于 Rubyists:
根据提示输入用户名和密码
来源:Elikem Adadevoh
For Rubyists:
enter username and pw as prompted
Source: Elikem Adadevoh
对于所有 Python 2.7.* 用户。目前版本 3 上有一个围绕 Github API 的 Python 包装器,称为 GitPython。只需使用
easy_install PyGithub
或pip install PyGithub
进行安装即可。Repository
对象文档位于此处。For all the Python 2.7.* users. There is a Python wrapper around the Github API that is currently on Version 3, called GitPython. Simply install using
easy_install PyGithub
orpip install PyGithub
.The
Repository
object docs are here.我创建了一个 Git 别名来执行此操作,基于 Bennedich 的回答。将以下内容添加到您的
~/.gitconfig
中:要使用它,请
从本地存储库内的任何位置运行,并在出现提示时输入您的 Github 密码。
I've created a Git alias to do this, based on Bennedich's answer. Add the following to your
~/.gitconfig
:To use it, run
from anywhere inside the local repository, and enter your Github password when prompted.
有关创建令牌的说明,请访问此处< /a> 这是您将输入的命令(截至本答案之日。(替换所有大写关键字):
输入密码后,您将看到以下包含您的令牌的内容。
您可以随时通过< a href="https://github.com/settings/applications" rel="nofollow noreferrer">此处
For directions on creating a token, go here This is the command you will type (as of the date of this answer. (replace all CAPS keywords):
Once you enter your password you will see the following which contains your token.
You can revoke your token anytime by going here
免责声明:我是开源项目的作者
此功能由以下支持:https://github.com/chrissound/Human-Friendly-Commands 本质上是这个脚本:
Disclamier: I'm the author of the open source project
This functionality is supported by: https://github.com/chrissound/Human-Friendly-Commands essentially it is this script:
找到了我喜欢的解决方案: https://medium.com/@jakehasler/how-to-create-a-remote-git-repo-from-the-command-line-2d6857f49564
您首先需要创建一个 Github 个人访问令牌
打开在您最喜欢的文本编辑器中创建 ~/.bash_profile 或 ~/.bashrc 。在文件顶部附近添加以下行,其中导出的变量的其余部分是:
export GITHUB_API_TOKEN=
在下面的某个位置,通过其他 bash 函数,您可以可以粘贴类似以下内容:
现在,每当您创建新项目时,您都可以运行命令
$ new-git Awesome-repo
在您的 Github 用户帐户上创建新的公共远程存储库。Found this solution which I liked: https://medium.com/@jakehasler/how-to-create-a-remote-git-repo-from-the-command-line-2d6857f49564
You first need to create a Github Personal Access Token
Open up your ~/.bash_profile or ~/.bashrc in your favorite text editor. Add the following line near the top of your file, where the rest of the export ‘ed variables are:
export GITHUB_API_TOKEN=<your-token-here>
Somewhere below, by your other bash functions, you can paste something similar to the following:
Now, whenever you’re creating a new project, you can run the command
$ new-git awesome-repo
to create a new public remote repository on your Github user account.答案是:
对于那些想要 bash 脚本的人,这是我的 MACOS zsh 脚本
在项目创建过程的末尾添加此内容,以将项目放入 github
它假设所有安装均已完成或包含在您的脚本中,并且您已获得授权:
如果有人想要 PowerShell 脚本,只需询问,我将在此处添加:.. 。
不会有太大不同
The answer is:
For those who want a bash script here is my MACOS zsh script
add this at the end of your project create process to put your project into github
It assumes all installs are completed or included in your script and you are auth'd:
If anyone wants a PowerShell script just ask and I will add here:...
it wont be much different
出于代表原因,我无法将其添加为评论(最好与 bennedich's answer),但对于 Windows 命令行,正确的语法如下:
curl -u YOUR_USERNAME https:// api.github.com/user/repos -d "{\"name\":\"YOUR_REPO_NAME\"}"
基本形式相同,但必须使用双引号 (") 而不是单引号,并用反斜杠转义 POST 参数中发送的双引号(在 -d 标志之后)。我还删除了用户名周围的单引号,但如果您的用户名有空格(可能?),则可能需要双引号。
For rep reasons, I can't add this as a comment (where it would better go with bennedich's answer), but for Windows command line, here is the correct syntax:
curl -u YOUR_USERNAME https://api.github.com/user/repos -d "{\"name\":\"YOUR_REPO_NAME\"}"
It's the same basic form, but you have to use double quotes (") instead of single, and escape the double quotes sent in the POST parameters (after the -d flag) with backslashes. I also removed the single quotes around my username, but if your username had a space (possible?) it would probably need double quotes.
这是我最初的 git 命令(可能,此操作发生在
C:/Documents and Settings/your_username/
中):here is my initial git commands (possibly, this action takes place in
C:/Documents and Settings/your_username/
):在命令行上创建新存储库
从命令行推送现有存储库
create a new repository on the command line
push an existing repository from the command line