Github错误"Updates were rejected because the tip of your current branch is behind"



我正试图在github中初始化一个新项目以下是我导致此错误的步骤:

在github中创建一个新的存储库,复制链接https://github.com/username/repository.git

>cd to project folder

>git init

>git remote add origin https://github.com/username/repository.git

>git add -A

>git status

>git commit -m "adding files"

>git push origin master

错误:

! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/username/repository.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

我试着做一个>git pull,但我得到了一个不同的错误

There is no tracking information for the current branch.
Please specify which branch you want to merge with.

接下来我尝试了上面的建议

>git pull origin master
From https://github.com/username/repository.git
* branch            master     -> FETCH_HEAD
fatal: refusing to merge unrelated histories

我在网上找到了一个解决方案,这是一个修复程序,但它并没有告诉我在git命令的序列中做错了什么

>git pull origin master --allow-unrelated-histories

git init

那就是你做错的地方。你应该从github克隆repo。


详细说明。您在github 上启动了一个新的存储库

在github中创建一个新的存储库,复制链接https://github.com/username/repository.git

将此视为通过SSH连接到github.com并执行git init。这将初始化,即在master上为空白repo创建第一个提交。

然后。在本地,你也做了git init

这通过在master上提交初始化了一个新的本地repo。

在这个阶段,您有两个不同的repo(一个本地/一个远程(,尽管Blob的哈希是相同的(假设两个repo都是空的(,但树结构和refs可能不同。这意味着初始提交哈希不同。这是我对你为什么会出现错误的猜测:

hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref.

这也是为什么拉动不会有任何作用的原因。拉取只是提取/合并操作的组合。

但是,如果您考虑您的场景,即使您在添加文件并在本地提交之前进行了提取,在初始提交时仍然有2个repo(本地/远程(具有潜在的不同提交哈希。。。您在机器上本地初始化的那个认为它的提交哈希是正确的。您在github上初始化的那个认为它的commit has是正确的。

相关内容

最新更新