[英]How to create GitHub repository under an organization from the command-line?
[英]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 的继任者。
创建一个新的 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 的答案有许多缺陷,这些缺陷在这个答案中得到了解决。 值得注意的是,这个答案:
curl
(通过-f
) 首先,获取一个可以访问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 的答案。
https://github.com/<user>/<repo>
<repo>
--确认 gh repo create
,从gh 2.21.0(2022 年 12 月)开始,在涉及到新的存储库所有者时更加精确:
以交互方式创建新的存储库时,系统只会提示用户输入存储库名称,并且尚不清楚是否可以为名称添加前缀以便在组织中创建存储库。
建议的解决方案
如果我输入一个没有前缀的名称,我希望看到一个 select 提示,其中包含我的个人帐户名(默认设置)和我所属的所有组织:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.