我刚刚在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 ...
错误的确切原因是远程回购上有一个提交,而您的本地历史记录不包含此提交。
为了解决这种情况,事情比它们应该涉及的要多一些,因为在这种情况下,两个提交没有共同的历史记录(它们都是"第一个提交"(,所以merge
或rebase
操作需要额外的选项才能工作。
- 如果您的
README.md
当前为空(或包含基本信息,很容易重新键入(;"修复";您的情况是覆盖此提交:
git push --force -u origin master
- 组合两个提交的一种干净方法是使用
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
- 最后,可以使用
git merge
或git 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
您将有一个合并提交,它合并了两个根提交(没有父级的提交(
但之后一切都会正常进行。