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

时间:2010-03-11 09:25:00

标签: git github ssh version-control command-line-interface

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

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

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

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

25 个答案:

答案 0 :(得分:306)

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

答案 1 :(得分:200)

您可以使用GitHub API通过命令行创建GitHub存储库。查看repository API。如果向下滚动大约三分之一,您会看到一个标题为"Create"的部分,该部分解释了如何通过API创建回购(上面是一个解释如何使用API​​分叉回购的部分) ,也)。显然你不能使用git来做这件事,但你可以通过命令行使用像curl这样的工具来完成。

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

答案 2 :(得分:64)

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

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

(针对v3 Github API更新)


这些命令的说明......

创建github repo

    curl -u 'nyeates' https://api.github.com/user/repos -d '{"name":"projectname","description":"This project is a test"}'
  • curl是一个unix命令(上面也适用于mac),它检索URL并与之交互。它通常已经安装。
  • “ -​​ u”是一个curl参数,指定用于服务器身份验证的用户名和密码。
    • 如果您只是提供用户名(如上例所示),curl将提示输入密码。
    • 如果您不想输入密码,请参阅Authentication上的githubs api文档
  • “ -​​ d”是一个curl参数,允许您使用请求发送POST数据
  • “name”是唯一需要的POST数据;我还想包括“描述”
  • 我发现用单引号“
  • 引用所有POST数据是好的

定义推送到

的位置
git remote add origin git@github.com:nyeates/projectname.git
  • 在github上添加连接(远程)仓库的位置和存在的定义
  • “origin”是git用于源来源的默认名称
    • 技术上没有来自github,但现在github repo将成为记录的来源
  • “git@github.com:nyeates”是一个ssh连接,假设您已经使用github设置了一个受信任的ssh密钥对。

将本地仓库推送到github

git push origin master
  • 从主本地分支推送到原始远程(github)

答案 3 :(得分:50)

如果您安装了defunkt's优秀的Hub工具,那么这就像

一样简单

git create

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

答案 4 :(得分:15)

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

  1. 安装HubGitHub)。

    • OS X:brew install hub
    • Gogo 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会提示输入GitHub用户名&密码第一次需要访问API并将其换成OAuth令牌,并将其保存在~/.config/hub中。

         

    要明确命名新存储库,请传入NAME,   可选地以ORGANIZATION/NAME形式在组织下创建   你是。的成员。

         

    使用-p创建一个私有存储库,并使用   -d-h分别设置了存储库的说明和主页URL

         

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

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

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

    另见:GitHub上的Importing a Git repository using the command line

答案 5 :(得分:11)

我认为有official github gem这样做。我会尝试在学习时添加更多信息,但我现在才刚刚发现这个宝石,所以我还不太了解。

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

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

如果有人对此有所了解,我很想知道我做错了什么。已经有issue filed

更新:我最终确实让它发挥作用。我不确定如何重新产生问题,但我刚从头开始(删除.git文件夹)

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

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

gh create-from-local

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

gh create dotfiles

答案 6 :(得分:9)

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

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

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

更简单的方法是:

  1. 在目录中创建一个shell脚本,即名为githubscript.sh的/ home / USER_NAME / Desktop / my_scripts
  2. 修改以下代码并将其保存到githubscript.sh文件中
  3. #!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;

    N.B。 此处$1repository name,在调用argument 时以script传递 保存脚本之前更改YOUR_GITHUB_USER_NAME

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

    2. 在环境配置文件中包含scripts目录。 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. 现在创建一个新的存储库,即demo githubrepo demo;

答案 7 :(得分:4)

对于使用双因素身份验证的用户,您可以使用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 git@github.com:USER/REPO.git
git push origin master

答案 8 :(得分:4)

Nope,您必须至少打开一次浏览器才能在GitHub上创建username,创建后,您可以利用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"}'

答案 9 :(得分:4)

使用Github的官方新command line interface

gh repo create

另见details and optionsinstallation instructions


例如,要完成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

答案 10 :(得分:3)

对于Rubyists:

gem install githubrepo
githubrepo create *reponame*

根据提示输入用户名和密码

git remote add origin *ctrl v*
git push origin master

来源:Elikem Adadevoh

答案 11 :(得分:3)

我已根据Bennedich's answer创建了一个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密码。

答案 12 :(得分:3)

基于@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 git@github.com:$GHUSER/$REPO.git; git push origin master"

答案 13 :(得分:3)

我使用针对GitHub和BitBucket的REST API为这个名为 Gitter 编写了一个漂亮的脚本:

https://github.com/dderiso/gitter

BitBucket:

gitter -c -r b -l javascript -n node_app

GitHub:

gitter -c -r g -l javascript -n node_app
  • -c =创建新的回购
  • -r = repo provider(g = GitHub,b = BitBucket)
  • -n =命名回购
  • -l =(可选)在repo中设置应用的语言

答案 14 :(得分:2)

Disclamier:我是开源项目的作者

此功能受以下人员支持:https://github.com/chrissound/Human-Friendly-Commands基本上就是这个脚本:

#!/usr/bin/env bash

# Create a repo named by the current directory
# Accepts 1 STRING parameter for the repo description
# Depends on bin: jq
# Depends on env: GITHUB_USER, GITHUB_API_TOKEN
github_createRepo() {
  projName="$(basename "$PWD")"
  json=$(jq -n \
    --arg name "$projName" \
    --arg description "$1" \
    '{"name":$name, "description":$description}')

  curl -u "$GITHUB_USER":"$GITHUB_API_TOKEN" https://api.github.com/user/repos -d "$json"
  git init
  git remote add origin git@github.com:"$GITHUB_USER"/"$projName".git
  git push origin master
};

答案 15 :(得分:2)

到目前为止,the accepted answerthe most-voted answer均已过时。密码认证为deprecated,并将于2020年11月13日世界标准时间16:00删除。

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

您需要(替换所有CAPS关键字):

  1. Create a personal access token通过网站。是的,您必须使用浏览器,但是以后所有访问都只能使用一次。安全地存储令牌。
  2. 通过以下方式创建存储库
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"}'
  1. 添加新的原点并将其推入:
git remote add origin git@github.com:USER/REPO.git
git push origin master

这样做的缺点是,您每次必须输入令牌,并且令牌会显示在bash历史记录中。

为避免这种情况,您可以

  1. 将标头存储在文件中(让我们称之为HEADER_FILE
Authorization: token MY_ACCESS_TOKEN
  1. 已从文件中读取卷发
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
  1. 为使事情更加安全,您可以将访问权限设置为400,并将用户设置为root用户
chmod 400 HEADER_FILE
sudo chown root:root HEADER_FILE
  1. 现在将需要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

答案 16 :(得分:2)

对于所有Python 2.7。*用户。 Github API周围有一个Python包装器,目前在版本3上,名为GitPython。只需使用easy_install PyGithubpip install PyGithub安装。

from github import Github
g = Github(your-email-addr, your-passwd)
repo = g.get_user().user.create_repo("your-new-repos-name")

# Make use of Repository object (repo)

Repository对象文档为here

答案 17 :(得分:2)

出于代表原因,我不能将其添加为注释(最好使用bennedich's answer),但对于Windows命令行,这是正确的语法:

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

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

答案 18 :(得分:2)

您需要的是hub。 Hub是git的命令行包装器。它已经使用别名与本机git集成。它试图向git提供github动作,包括创建新的存储库。

→  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

答案 19 :(得分:2)

有关创建令牌的说明,请转到here这是您要输入的命令(截至本答复日期。(替换所有CAPS关键字):

curl -u 'YOUR_USERNAME' -d '{"scopes":["repo"],"note":"YOUR_NOTE"}' https://api.github.com/authorizations

输入密码后,您将看到包含令牌的以下内容。

{
  "app": {
    "name": "YOUR_NOTE (API)",
    "url": "http://developer.github.com/v3/oauth/#oauth-authorizations-api"
  },
  "note_url": null,
  "note": "YOUR_NOTE",
  "scopes": [
    "repo"
  ],
  "created_at": "2012-10-04T14:17:20Z",
  "token": "xxxxx",
  "updated_at": "2012-10-04T14:17:20Z",
  "id": xxxxx,
  "url": "https://api.github.com/authorizations/697577"
}

您可以随时转到here

撤消令牌

答案 20 :(得分:1)

最后,确实发生了GitHub正式宣布了其所有核心功能的新CLI。

在此处检查:https://cli.github.com/

要通过HomeBrew安装:brew install gh其他方式:https://github.com/cli/cli#installation

然后

gh repo create

其他可用功能。

$ gh --help

Work seamlessly with GitHub from the command line.

USAGE
  gh <command> <subcommand> [flags]

CORE COMMANDS
  gist:       Create gists
  issue:      Manage issues
  pr:         Manage pull requests
  release:    Manage GitHub releases
  repo:       Create, clone, fork, and view repositories

ADDITIONAL COMMANDS
  alias:      Create command shortcuts
  api:        Make an authenticated GitHub API request
  auth:       Login, logout, and refresh your authentication
  completion: Generate shell completion scripts
  config:     Manage configuration for gh
  help:       Help about any command

FLAGS
  --help      Show help for command
  --version   Show gh version

EXAMPLES
  $ gh issue create
  $ gh repo clone cli/cli
  $ gh pr checkout 321

ENVIRONMENT VARIABLES
  See 'gh help environment' for the list of supported environment variables.

LEARN MORE
  Use 'gh <command> <subcommand> --help' for more information about a command.
  Read the manual at https://cli.github.com/manual

FEEDBACK
  Open an issue using 'gh issue create -R cli/cli'

因此,现在您可以从终端创建存储库。

答案 21 :(得分:0)

找到我喜欢的解决方案:https://medium.com/@jakehasler/how-to-create-a-remote-git-repo-from-the-command-line-2d6857f49564

首先需要创建Github Personal Access Token

在您喜欢的文本编辑器中打开〜/ .bash_profile或〜/ .bashrc。在文件顶部附近添加以下行,其余的导出变量为:

export 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用户帐户上创建新的公共远程存储库。

答案 22 :(得分:-1)

我最近发现了create-github-repo。从自述文件:

安装:

$ npm i -g create-github-repo

用法:

$ export CREATE_GITHUB_REPO_TOKEN=<access_token>
$ create-github-repo --name "My coolest repo yet!"

或者:

$ create-github-repo <access_token> --name "My coolest repo yet!"

答案 23 :(得分:-1)

这是我的初始git命令(可能,此操作发生在C:/Documents and Settings/your_username/):

mkdir ~/Hello-World
# Creates a directory for your project called "Hello-World" in your user directory
cd ~/Hello-World
# Changes the current working directory to your newly created directory
touch blabla.html
# create a file, named blabla.html
git init
# Sets up the necessary Git files
git add blabla.html
# Stages your blabla.html file, adding it to the list of files to be committed
git commit -m 'first committttt'
# Commits your files, adding the message 
git remote add origin https://github.com/username/Hello-World.git
# Creates a remote named "origin" pointing at your GitHub repository
git push -u origin master
# Sends your commits in the "master" branch to GitHub

答案 24 :(得分:-5)

在命令行上创建一个新的存储库

echo "# <RepositoryName>" >> README.md

git init

git add README.md

git commit -m "first commit"

git remote add origin https://github.com/**<gituserID>/<RepositoryName>**.git

git push -u origin master

从命令行推送现有存储库

git remote add origin https://github.com/**<gituserID>/<RepositoryName>**.git

git push -u origin master