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

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

我正在尝试在 github 中启动一个新项目 这些是导致此错误的步骤:

在github 新建一个仓库,复制linkhttps://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.

克隆回购协议

详细说明。您在 github

上启动了一个新存储库

Create a new repository in github, copy link https://github.com/username/repository.git

将此视为 SSH 进入 github.com 并执行 git init。这会初始化,即在 master 上为空白回购创建第一个提交。

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

这初始化了一个新的本地 repo,并在 master 上进行了提交。

在这个阶段你有 2 个不同的 回购(一个本地/一个远程),虽然 blob 的哈希值是相同的(假设两个回购都是空的),树结构和参考文献可能不同。这意味着初始提交哈希值不同。这是我对您收到错误原因的猜测:

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 个回购协议(本地/远程),在初始提交时可能有不同的提交散列......你在本地初始化的那个你的机器认为它的提交哈希是正确的。您在 github 上初始化的那个认为它的提交是正确的。