如何在本地强制合并两个远程分支?

How to force merge two remote branches locally?

我正在尝试将我的本地分支从不同的存储库合并到我的上游代码。我想在我的开发系统中将 priv/dev 分支合并到 origin/master。我想将我的更改优先于 origin master。但是它失败了,我的代码有错误吗?

git clone https://github.com/production public_code
cd public_code
git remote add priv https://gitlab.com/tmv/development
git merge -Xours priv/dev origin/master

输出

error: Merging is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.

git merge 将当前本地分支与您在命令行中提供的任何分支合并。你不能真正合并上游分支。因此,在您的示例中,您将 priv/devorigin/master 合并到本地 master 分支。相反,您需要合并这些分支的本地副本,然后将最终结果推送到上游仓库:

git clone <repo URL>
cd <repo dir>
git checkout <branch you want to merge into>
git merge <branch you want to merge>
git push

因为你只是克隆了一个repo,你只需要做一个常规的合并:

git clone https://github.com/production public_code
cd public_code
git remote add priv https://gitlab.com/tmv/development
git merge -Xtheirs priv/dev
git push origin HEAD

克隆完成后,master分支已经被检出,你已经拥有了master中最新的分支。