使用自述文件初始化存储库是否意味着无法从本地存储库推送?



我刚刚在Github上创建了一个新的repo,README.md作为初始提交。

现在,我想把我的项目推到Github上,所以我这么做了:

git push -u origin master
But I get the error: ```error: failed to push some refs to ...
updates were rejected because the tip of your current branch is behind its remote counterpart

所以,我认为这是因为远程repo有README.md文件,而我的本地repo没有?

我尝试了git pull origin master,但随后我得到了另一个错误:fatal: refusing to merge unrelated histories

是的,updates were rejected ...错误的确切原因是远程回购上有一个提交,而您的本地历史记录不包含此提交。

为了解决这种情况,事情比它们应该涉及的要多一些,因为在这种情况下,两个提交没有共同的历史记录(它们都是"第一个提交"(,所以mergerebase操作需要额外的选项才能工作。


  1. 如果您的README.md当前为空(或包含基本信息,很容易重新键入(;"修复";您的情况是覆盖此提交:
git push --force -u origin master
  1. 组合两个提交的一种干净方法是使用git rebase --root
git fetch
# replay your local commit on top of origin/master :
# the --root option allows you to include a root commit (commit with no parent)
# in the list of rebased commits
git rebase --root origin/master master
# you can now push without '--force'
git push -u origin master
  1. 最后,可以使用git mergegit pull上的--allow-unrelated-histories选项显式合并两个不相关的历史:
git fetch origin
git merge --allow-unrelated-histories origin/master
# equivalent to:
git pull --allow-unrelated-histories origin master

使用第三个选项,您的历史记录的开头看起来有点不寻常:

$ git log --oneline --graph
* cc1234 (HEAD, master) Merge 'origin/master' into master
|
| * bb1234 (origin/master) add README.md
* aa1234 first commit on master

您将有一个合并提交,它合并了两个根提交(没有父级的提交(
但之后一切都会正常进行。

相关内容

最新更新