Git - 在从先前使用的源拉取时拒绝合并不相关的历史记录

3

我在我们的服务器上将先前使用的源设置为新的URL,如下:

git remote set-url origin [new_url]

现在我尝试拉取时出现错误

git pull origin dev
fatal: refusing to merge unrelated histories

我将新的URL设置为原地址,以便不必创建新的来源进行操作。 我该如何解决这个问题?
编辑: - 当我添加"git pull origin dev --allow-unrelated-histories"时,出现了一个错误,并显示了一些未跟踪的文件。
error: The following untracked working tree files would be overwritten by merge:

我想要覆盖这些文件。


2
重复问题: https://dev59.com/sloT5IYBdhLWcg3wtBRz#40959920 https://dev59.com/ploU5IYBdhLWcg3wAjYs ? - Ripp_
当执行 git pull origin dev --allow-unrelated-histories 命令时,会再次出现错误,并显示一些未跟踪的文件。错误信息如下:error: The following untracked working tree files would be overwritten by merge:我想覆盖这些文件,只需手动删除它们即可。 - Timothy Truckle
这可能听起来像是重复的,因为第一个错误消息是相同的,但是使用 git remote set-url 命令的情况有些不同。 - Jaap
1个回答

1
git fetch origin dev followed by git reset --hard origin/dev.
This is because a git pull is essentially a combination of git fetch and git merge, but the problem usually arises during the merge part. If the merge is just a fast-forward, we can simply do a reset instead of a pull. Assuming that you have no modified files when starting and your dev branch contains no un-published commits, you can use the above commands to replace the git pull origin dev command.
git branch backup-of-dev-just-in-case dev
git fetch origin
git checkout dev
git reset --hard origin/dev   # This command is like "rm -rf", you should always double
                              # check conditions every time you use `--hard` so that you
                              # do not accidentally delete too much.

如果您的开发分支包含未推送的提交,请执行以下附加步骤:

git branch local-dev-changes backup-of-dev-just-in-case
git rebase --rebase-merges dev local-dev-changes
git checkout dev
git merge local-dev-changes
git branch -d local-dev-changes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接