在不打开浏览器的情况下,是否可以通过命令行在GitHub上创建远程存储库?

397

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

25个回答

5

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的维护者)

原始回答

你需要用的是hub。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

5

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

1
然后使用 git remote add origin https://github.com/github用户名/仓库名.git 将您的本地项目与GitHub关联。例如,命令应该长这样:git remote add origin https://github.com/arpitaggarwal/command-line-repo.git - SherylHohman

5

终于,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'

现在你可以从终端创建仓库。


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

3

我为此编写了一个称为Gitter的巧妙脚本,使用了GitHub和BitBucket的REST API:

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 = 存储库提供商(g = GitHub,b = BitBucket)
  • -n = 命名存储库
  • -l = (可选)设置存储库中应用程序的语言

3

对于Ruby程序员:

gem install githubrepo
githubrepo create *reponame*

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

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

来源:Elikem Adadevoh

3

针对所有使用Python 2.7.*的用户。现在有一个Python包装器可以调用版本为3的Github API,名为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对象的文档在这里


2

免责声明:本人是开源项目的作者

此功能由以下项目支持: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
};

这确实是使用GitHub个人访问令牌的方式。(通过?access_token=${ACCESSTOKEN}添加它的旧方法不再起作用。) - not2qubit

2

关于如何创建令牌,请参见此处。以下是您需要键入的命令(截至本答案发布日期,将所有大写关键字替换为相应内容):

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"
}

您可以随时通过访问此处撤销您的令牌。


2
我已经创建了一个Git别名来完成这个任务,基于Bennedich的回答。将以下内容添加到你的~/.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密码。

这对我没有用。它返回“没有这样的文件或目录”。 - adamwong246
这对我也没用。它返回 curl: (22) The requested URL returned error: 401 Traceback (most recent call last): File "<string>", line 1, in <module> File "/usr/lib64/python3.2/subprocess.py", line 488, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command '['curl', '-u', 'myusername', 'https://api.github.com/user/repos', '-d', '{"name": "reponame"}', '--fail']' returned non-zero exit status 22 - Da Frenk
1
使用Python有点过头了,会增加很多噪音,例如额外的反斜杠和其他标点符号。我制作了一个只用Bash的版本:https://dev59.com/k3E95IYBdhLWcg3wKq2q#28924077 - robru

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接