出于可访问性的原因,我试图将一些本地未版本控制的代码推送到存储库中并将其发布到 GitHub 上。
我听从了建议here https://stackoverflow.com/questions/3311774/how-to-convert-existing-non-empty-directory-into-a-git-working-directory-and-pus
cd <local_dir>
git init
git add .
git commit -m "initial commit"
然后我在 github 上创建了一个新的存储库并执行了
git remote add origin https://github.com/...
git pull origin master --allow-unrelated-histories
git push --force-with-lease
但我现在在 github 上看到我有 2 个分支“main”和“master”
我猜 github 在创建存储库时创建了“main”,而我在从本地存储库同步时创建了“master”
我两者都需要吗?
我可以将 master 合并到 main 然后删除 master 吗?
目前这让我很困惑
Note: 我现在已经尝试过(本地)
git branch -m master main
git push origin HEAD
但我收到错误
To https://github.com/<the-repo>
! [rejected] HEAD -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/<the-repo>'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
我缺少什么?
2020 年美国发生事件后,GitHub 决定重命名默认 Git 分支main
(details https://www.theregister.com/2020/06/15/github_replaces_master_with_main)。 Git 和 GitHub 不强制要求任何分支名称,但存储库需要默认分支;该分支的命名方式取决于您。
至于你的第二个问题,解决main
很好。在进行任何更改之前,请检查您的提交位于哪个分支:
- 如果你做出了承诺
master
,你必须合并master
to main
,然后删除master
- 如果你做出了承诺
main
,上面没有任何东西master
和 可以将其删除。
你可以 :
- 在您的工作站上执行此操作并且
push
更改 GitHub(我的首选)
- 在 GitHub 上工作并且
pull
从那里
编辑回答额外的问题:
Git 的伟大之处在于它提供了相当丰富的错误消息和建议:
-
Updates were rejected because the tip of your current branch is behind its remote counterpart.
这意味着远程分支(在 GitHub 上)已更改您的本地分支未更改的内容,这就是 Git 拒绝更改的原因push
.
-
Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again.
这是典型的解决方案。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)