繁体   English   中英

从命令行创建一个 GitHub 存储库

[英]Create a GitHub repository from command line

我一直在尝试从命令行将我的本地回购更改推送到 github。 我已经离开 git 一段时间了,所以我不记得一些事情了。 在过去的一个小时里,我一直在尝试在不在 Github.com 上创建远程仓库的情况下推送仓库。 据我所知,git push origin master/ git push 足以推送本地更改,并在必要时在远程服务器上创建一个 repo。 但是 git 推送不会让我推送并自动创建回购协议。

因此,为了节省时间,我在 github.com 上创建了远程仓库,并使用以下命令添加了远程仓库 url

git remote add origin https://mygithubrepoUrl.com

它奏效了。

是否有必要在 Github 上创建远程仓库,然后从命令行添加这个 url 以推送更改? Git 不能自动创建 repo 并推送更改吗?

您需要在推送之前创建存储库,但是有一个hub可以为您自动执行此操作:

git init newRepo
cd newRepo
hub create

使用-p开关来hub create以创建私有存储库。 要推送本地master分支,请发出:

git push -u origin HEAD

该工具还可以创建拉取请求、打开项目页面、检查 CI 状态、通过仅指定username/repo来克隆现有的 repos 等等。

项目页面建议将git别名为hub (因为后者将未知命令转发到git ),但我不建议这样做,即使只是为了将“裸”Git 命令与hub糖果区分开来。

cli.github.com现在是 hub 的继任者。

它允许从命令行创建存储库,因为cli 0.6PR 547

创建一个新的 GitHub 存储库。

用法:

创建一个新的 GitHub 存储库。

使用“ ORG/NAME ”语法在您的组织内创建存储库。

 gh repo create [<name>] [flags]

标志:

 -d, --description string Description of repository --enable-issues Enable issues in the new repository (default true) --enable-wiki Enable wiki in the new repository (default true) -h, --homepage string Repository home page URL --public Make the new repository public -t, --team string The name of the organization team to be granted access

全局标志:

 --help Show help for command -R, --repo OWNER/REPO Select another repository using the OWNER/REPO format

正如itsgus.dev评论中指出的那样:

不以交互方式运行时,需要--public--private--internal标志。

Github API 应该可以工作。

首先使用curl和 API 创建 repo https://developer.github.com/v3/repos/#create

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

然后您可以像之前描述的那样添加远程和推送:

git remote add origin git@github.com:user/repository_name.git && git push origin master

mickiewicz通过curl使用 REST API 的答案有许多缺陷,这些缺陷在这个答案中得到了解决。 值得注意的是,这个答案:

  1. 使用必要的令牌授权对 GitHub 进行授权,而不是过时的密码身份验证
  2. 在发生错误时使用非零代码退出curl (通过-f
  3. 参数化存储库名称
  4. 进行私人回购(默认为公开)

首先,获取一个可以访问repo范围的令牌

REPO_NAME=foo1
GITHUB_TOKEN=0000000000000000000000000000000000000000  # Enter your own.

curl -f -X POST \
  -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.v3+json" \
  https://api.github.com/user/repos -d "{\"name\": \"${REPO_NAME}\", \"private\": true}"

此答案仅与在user下创建存储库有关。 组织下创建存储库的请求略有不同。

如果您不介意安装 GitHub CLI,请参阅VonC 的答案

首先安装GitHub cli

  1. gh repo create - 按照说明操作
  2. 如果你选择不在本地克隆然后继续下面假设你在工作文件夹中否则 repo 已经被克隆到当前文件夹中。
  3. git 初始化
  4. git git 远程添加源https://github.com/<user>/<repo>
  5. git支线-M主线
  6. 现在本地文件夹和 repo 已连接,您已准备好 go。
  7. 想删除回购? 如果您没有管理员权限,请先获得管理员权限。
  8. gh auth refresh -h github.com -s delete_repo
  9. gh repo delete <repo> --确认

gh repo create ,从gh 2.21.0(2022 年 12 月)开始,在涉及到新的存储库所有者时更加精确:

以交互方式创建新的存储库时,系统只会提示用户输入存储库名称,并且尚不清楚是否可以为名称添加前缀以便在组织中创建存储库。

建议的解决方案

如果我输入一个没有前缀的名称,我希望看到一个 select 提示,其中包含我的个人帐户名(默认设置)和我所属的所有组织:

所有者选择——https://user-images.githubusercontent.com/245879/200487503-cef84b03-964f-4c31-b502-158f16444219.png

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM