Github错误“由于当前分支的尖端在后面,因此更新被拒绝”

时间:2020-03-25 01:51:50

标签: git github repository

我正在尝试在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

1 个答案:

答案 0 :(得分:0)

git init

那是你做错了。您应该已经从github克隆了仓库。


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

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

将其视为与通过SSH进入github.com并执行git init相同。这会初始化,即在master上为空白存储库创建第一个提交。

然后。在本地,您也进行过git init

这通过提交master初始化了一个新的本地仓库。

在此阶段,您有2个不同存储库(一个本地/一个远程),尽管blob的哈希值是相同的(假设两个存储库都是空的),但是树结构和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上初始化的那个认为它的提交是正确的。