为了节省时间,我在 Github.com 上创建了远程 repo,并使用以下命令添加了远程 repo 的 URL:
git remote add origin https://mygithubrepoUrl.com
这样就成功了。在命令行中需要先创建 Github 上的远程 repo 并添加 URL 才能推送更改吗?Git 不能自动创建 repo 并推送更改吗?
git remote add origin https://mygithubrepoUrl.com
这样就成功了。在推送之前,您需要创建存储库,但是有一个hub
可以自动化此过程:
git init newRepo
cd newRepo
hub create
使用-p
开关来创建私有仓库,使用hub create
。要推送本地的master
分支,请执行以下操作:
-p开关用于创建私有仓库。
使用hub create命令创建仓库。
要推送本地的master分支,请执行以上操作。
git push -u origin HEAD
这个工具还可以创建拉取请求,打开项目页面,检查CI状态,通过仅指定用户名/仓库
来克隆现有的存储库,以及其他一些功能。
项目页面建议将 git
别名设为 hub
(因为后者将未知命令转发到 git
),但我不建议这样做,即使只是为了区分“裸”Git命令和 hub
的功能。
cli.github.com现在是hub的继承者。
自cli 0.6以来,它允许从命令行创建存储库,并支持PR 547。
创建一个新的GitHub存储库。
Usage:
Create a new GitHub repository.
Use the "
ORG/NAME
" syntax to create a repository within your organization.
gh repo create [<name>] [flags]
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
如itsgus.dev在评论中指出:Global Flags:
--help Show help for command -R, --repo OWNER/REPO Select another repository using the OWNER/REPO format
当非交互式运行时,必须使用
--public
、--private
或--internal
标志。
git
,而不是下载并开始使用不同的GitHub特定工具来处理我的git工作呢? - NeilGgh
所涵盖的操作是 GitHub API 调用的包装器,而不是本地 git
命令所涵盖的。如果没有 gh
,则需要使用 curl
。 - VonCgit
创建存储库。您需要使用GitHub特定命令。 - NeilGcurl
或gh
,而不是git
。 - VonCGithub API应该能够正常工作。
首先使用curl
和API创建存储库,详情请参见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
curl --user 'username' https://api.github.com/username/repos --request POST --data '{"name":"newreponame"}' --verbose
返回 404 Not Found
,而 curl --user 'username' https://api.github.com/orgs/orgname/repos -d '{"name":"newreponame"}'
返回 201 Created
。建议使用 hub。 - Luke Schoenhttps://github.com/<user>/<repo>
<repo>
--confirm这篇回答解决了使用 REST API 通过 curl
存在的多个问题,其中 mickiewicz 的 回答存在一些不足。值得注意的是,这篇回答:
curl
返回一个非零代码(通过使用参数 -f
)。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}"
此回答仅适用于在用户下创建存储库的情况。创建组织存储库的请求略有不同。
如果您不介意安装GitHub CLI,请参考VonC的答案。
gh repo create
,从gh 2.21.0(2022年12月)开始更加精确,特别是在创建新存储库的所有者方面:
当交互式地创建新的存储库时,用户只被提示输入存储库名称,而不清楚可以添加前缀以便在组织中创建存储库。
建议的解决方案
如果我输入没有前缀的名称,希望出现一个选择提示,其中包含我的个人帐户名称(默认值)和我所属的所有组织: