git 是否可以在不打开浏览器的情况下从 CLI 在 GitHub 上创建远程存储库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2423777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 08:00:28  来源:igfitidea点击:

Is it possible to create a remote repo on GitHub from the CLI without opening browser?

gitgithubsshcommand-line-interfacegithub-cli

提问by anddoutoi

I created a new local Git repository:

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

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

Is there any git command to create a new remoterepo and push my commit to GitHub from here?I know it's no big deal to just fire up a browser and head over to Create a New Repository, but if there is a way to achieve this from the CLI I would be happy.

是否有任何 git 命令来创建新的远程存储库并将我的提交从这里推送到 GitHub?我知道启动浏览器并转到Create a New Repository没什么大不了的,但如果有办法从 CLI 实现这一点,我会很高兴。

I read a vast amount of articles but none that I found mention how to create a remote repo from the CLI using git commands. Tim Lucas's nice article Setting up a new remote git repositoryis the closest I found, but GitHub does not provide shell access.

我阅读了大量文章,但没有一篇文章提到如何使用 git 命令从 CLI 创建远程存储库。Tim Lucas 的好文章Setting up a new remote git repository是我找到的最接近的文章,但 GitHub 不提供 shell 访问

采纳答案by mipadi

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 gitto do this, but you can do it via the command line with a tool like curl.

您可以使用 GitHub API 通过命令行创建 GitHub 存储库。查看存储库 API。如果向下滚动大约三分之一,您将看到一个标题为“创建”的部分,该部分解释了如何通过 API 创建存储库(该部分的正上方是解释如何使用 API 分叉存储库的部分,也是)。显然你不能git这样做,但你可以通过命令行使用像curl.

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.

在 API 之外,无法通过命令行在 GitHub 上创建存储库。正如您所指出的,GitHub 不允许 shell 访问等,因此除了 GitHub API 之外,创建 repo 的唯一方法是通过 GitHub 的 Web 界面。

回答by bennedich

CLI commands for github API v3 (replace all CAPS keywords):

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

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 [email protected]:USER/REPO.git
git push origin master

回答by Nay

This can be done with three commands:

这可以通过三个命令来完成:

curl -u 'nyeates' https://api.github.com/user/repos -d '{"name":"projectname","description":"This project is a test"}'
git remote add origin [email protected]:nyeates/projectname.git
git push origin master

(updated for v3 Github API)

(针对 v3 Github API 更新)



Explanation of these commands...

这些命令的解释...

Create github repo

创建 github 仓库

    curl -u 'nyeates' https://api.github.com/user/repos -d '{"name":"projectname","description":"This project is a test"}'
  • curl is a unix command (above works on mac too) that retrieves and interacts with URLs. It is commonly already installed.
  • "-u" is a curl parameter that specifies the user name and password to use for server authentication.
    • If you just give the user name (as shown in example above) curl will prompt for a password.
    • If you do not want to have to type in the password, see githubs api documentation on Authentication
  • "-d" is a curl parameter that allows you to send POST data with the request
  • "name" is the only POST data required; I like to also include "description"
  • I found that it was good to quote all POST data with single quotes ' '
  • curl 是一个 unix 命令(上面也适用于 mac),它检索 URL 并与 URL 交互。它通常已经安装。
  • “-u”是一个 curl 参数,用于指定用于服务器身份验证的用户名和密码。
    • 如果您只提供用户名(如上例所示),curl 将提示输入密码。
    • 如果您不想输入密码,请参阅有关身份验证的githubs api 文档
  • “-d”是一个 curl 参数,它允许您随请求发送 POST 数据
  • “名称”是唯一需要的 POST 数据;我也喜欢包括“描述”
  • 我发现用单引号 ' ' 引用所有 POST 数据很好

Define where to push to

定义推送到的位置

git remote add origin [email protected]:nyeates/projectname.git
  • add definition for location and existance of connected (remote) repo on github
  • "origin" is a default name used by git for where the source came from
    • technically didnt come from github, but now the github repo will be the source of record
  • "[email protected]:nyeates" is a ssh connection that assumes you have already setup a trusted ssh keypair with github.
  • 在 github 上添加已连接(远程)存储库的位置和存在的定义
  • “origin”是 git 使用的默认名称,表示源来自哪里
    • 技术上不是来自 github,但现在 github repo 将是记录的来源
  • [email protected]:nyeates”是一个 ssh 连接,它假设您已经使用 github 设置了一个可信的 ssh 密钥对。

Push local repo to github

将本地 repo 推送到 github

git push origin master
  • push to the origin remote (github) from the master local branch
  • 从主本地分支推送到源远程(github)

回答by cavalcade

If you install defunkt'sexcellent Hubtool, then this becomes as easy as

如果你安装了defunkt优秀的Hub工具,那么这就变得很简单了

git create

git create

In the words of the author, "hub is a command-line wrapper for git that makes you better at GitHub."

用作者的话来说,“ hub 是 git 的命令行包装器,可以让你在 GitHub 上做得更好。

回答by kenorb

Simple steps (using git+ hub=> GitHub):

简单步骤(使用git+ hub=> GitHub):

  1. Install Hub(GitHub).

    • OS X: brew install hub
    • having Go: go get github.com/github/hub
    • otherwise (having Goas well):

      git clone https://github.com/github/hub.git && cd hub && ./script/build
      
  2. Go to your repo or create empty one: mkdir foo && cd foo && git init.

  3. 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

    Hub will prompt for GitHub username & password the first time it needs to access the API and exchange it for an OAuthtoken, which it saves in ~/.config/hub.

    To explicitly name the new repository, pass in NAME, optionally in ORGANIZATION/NAMEform to create under an organization you're a member of.

    With -p, create a private repository, and with -dand -hset the repository's description and homepage URL, respectively.

    To avoid being prompted, use GITHUB_USERand GITHUB_PASSWORDenvironment variables.

  4. Then commit and push as usual or check hub commit/hub push.

  1. 安装集线器( GitHub)。

    • 操作系统: brew install hub
    • go get github.com/github/hub
    • 否则(也有Go):

      git clone https://github.com/github/hub.git && cd hub && ./script/build
      
  2. 转到您的存储库或创建一个空的:mkdir foo && cd foo && git init

  3. 运行:hub create,它会第一次询问你关于 GitHub 凭证的信息。

    用法: hub create [-p] [-d DESCRIPTION] [-h HOMEPAGE] [NAME]

    例子: hub create -d Description -h example.com org_name/foo_repo

    Hub 将在第一次需要访问 API 时提示输入 GitHub 用户名和密码并将其交换为OAuth令牌,并将其保存在~/.config/hub.

    要明确命名新存储库,请传入NAME,可选择在ORGANIZATION/NAME表单中创建,以在您所属的组织下创建。

    -p,创建一个私有仓库,用 -d和 分别-h设置仓库的描述和主页URL

    为了避免被提示,使用GITHUB_USERGITHUB_PASSWORD环境变量。

  4. 然后像往常一样提交和推送或检查hub commit/ hub push

For more help, run: hub help.

如需更多帮助,请运行:hub help

See also: Importing a Git repository using the command lineat GitHub.

另请参阅:在 GitHub 上使用命令行导入 Git 存储库

回答by Jason Marcell

There is an official github gemwhich, 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.

我认为有一个官方的 github gem可以做到这一点。随着我的学习,我会尝试添加更多信息,但我只是现在才发现这个宝石,所以我还不太了解。

UPDATE: After setting my API key, I am able to create a new repo on github via the createcommand, however I am not able to use the create-from-localcommand, which is supposed to take the current local repo and make a corresponding remote out on github.

更新:设置我的 API 密钥后,我可以通过create命令在 github 上创建一个新的 repo ,但是我无法使用该create-from-local命令,该命令应该获取当前本地 repo 并在 github 上创建相应的远程。

$ gh create-from-local
=> error creating repository

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)

更新:我最终让这个工作。我不太确定如何重现这个问题,但我刚从头开始(删除了 .git 文件夹)

git init
git add .emacs
git commit -a -m "adding emacs"

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 ticketasking for the desired behavior)

现在这一行将创建远程仓库,甚至推送到它,但不幸的是,我认为我无法指定我想要的仓库的名称。我希望它在 github 上被称为“dotfiles”,但是 gh gem 只是使用了当前文件夹的名称,因为我在我的主文件夹中,所以它是“jason”。(我添加了一张要求所需行为的票

gh create-from-local

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.

另一方面,此命令确实接受一个参数来指定远程仓库的名称,但它旨在从头开始一个新项目,即在您调用此命令后,您将获得一个正在跟踪本地仓库的新远程仓库在相对于您当前位置的新创建的子文件夹中,两者都将名称指定为参数。

gh create dotfiles

回答by Nasif Md. Tanjim

To Quickly Create the Remote Repository by Using a Bash Shell

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

It is cumbersome to type the complete code every time a repository is to be created

每次创建repository都要输入完整的代码很麻烦

curl -u 'USER' https://api.github.com/user/repos -d '{"name":"REPO"}' git remote add origin [email protected]:USER/REPO.git git push origin master

curl -u 'USER' https://api.github.com/user/repos -d '{"name":"REPO"}' git remote add origin [email protected]:USER/REPO.git git push origin master

An easier approach is:

一个更简单的方法是:

  1. create a shell script in a directory i.e. /home/USER_NAME/Desktop/my_scripts named githubscript.sh
  2. Modify and save the following code to the githubscript.shfile
  1. 在名为 /home/USER_NAME/Desktop/my_scripts 的目录中创建一个 shell 脚本 githubscript.sh
  2. 修改以下代码保存到githubscript.sh文件中
#!bin/bash
curl -u 'YOUR_GITHUB_USER_NAME' https://api.github.com/user/repos -d "{\"name\":\"\"}";
git init;
git remote add origin [email protected]:YOUR_GITHUB_USER_NAME/.git;

N.B.Here $1is the repository namethat is passed as an argumentwhen invoking the scriptChange YOUR_GITHUB_USER_NAMEbefore saving the script.

注意这里$1repository nameargumentscriptYOUR_GITHUB_USER_NAME在保存脚本之前调用Change作为 an 传递的。

  1. Set required permissions to the scriptfile chmod 755 githubscript.sh

  2. Include the scripts directory in the environment configuration file. nano ~/.profile; export PATH="$PATH:$HOME/Desktop/my_scripts"

  3. Also set an alias to run the githubscript.sh file. nano ~/.bashrc; alias githubrepo="bash githubscript.sh"

  4. Now reload the .bashrcand .profilefiles in the terminal. source ~/.bashrc ~/.profile;

  5. Now to create a new repository i.e. demo: githubrepo demo;

  1. 设置script文件 所需的权限chmod 755 githubscript.sh

  2. 在环境配置文件中包含脚本目录。 nano ~/.profile; export PATH="$PATH:$HOME/Desktop/my_scripts"

  3. 还要设置别名来运行 githubscript.sh 文件。 nano ~/.bashrc; alias githubrepo="bash githubscript.sh"

  4. 现在在终端中重新加载.bashrc.profile文件。 source ~/.bashrc ~/.profile;

  5. 现在创建一个新的存储库,即demogithubrepo demo;

回答by robru

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:

基于@Mechanical Snail 的另一个答案,除了不使用 python,我发现它太过分了。将此添加到您的~/.gitconfig

[github]
    user = "your-name-here"
[alias]
    hub-new-repo = "!REPO=$(basename $PWD) GHUSER=$(git config --get github.user); curl -u $GHUSER https://api.github.com/user/repos -d {\\"name\\":\\"$REPO\\"} --fail; git remote add origin [email protected]:$GHUSER/$REPO.git; git push origin master"

回答by Arpit Aggarwal

Nope,you have to open a browser atleast once to create your usernameon GitHub, once created, you can leverage GitHub APIto create repositories from command line, following below command:

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

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

For example:

例如:

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

回答by user886596

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.

对于双因素认证的用户,可以使用 bennedich 的解决方案,但只需要为第一个命令添加 X-Github-OTP 头即可。将 CODE 替换为您从双因素身份验证提供商处获得的代码。将 USER 和 REPO 替换为存储库的用户名和名称,就像在他的解决方案中一样。

curl -u 'USER' -H "X-GitHub-OTP: CODE" -d '{"name":"REPO"}' https://api.github.com/user/repos
git remote add origin [email protected]:USER/REPO.git
git push origin master