在 git 中强制合并的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9434959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:02:36  来源:igfitidea点击:

What's the best way to force a merge in git?

gitmerge

提问by Sam Fen

I have a project with a dev branch and a production branch. Recently I've been working on a big new set of features in dev, so I haven't merged into production for about two weeks. In the meantime, though, there were some bugs that needed to be fixed in production.

我有一个带有开发分支和生产分支的项目。最近我一直在开发中开发大量新功能,所以我已经有两周没有合并到生产中了。但与此同时,有一些错误需要在生产中修复。

For the most part I was able to make the fixes in dev and cherry-pick them into production. Sometimes, however, I would need to fix production by hand, as the fixes were substantially different in the two branches. Point is, the two branches have diverged a fair bit since they split.

在大多数情况下,我能够在 dev 中进行修复,并将它们挑选到生产中。然而,有时我需要手动修复生产,因为修复在两个分支中大不相同。重点是,这两个分支自分裂以来已经分道扬镳了。

Now I want to just push all of dev into production. I don't care about keeping any of the commits in production since the split, I just want production to look exactly like dev. [EDIT: I want production to look exactly like dev since the split, but don't want to rewrite history before the split] However, when I try to merge I get dozens of conflicts that I'd prefer not to fix by hand.

现在我只想将所有开发人员投入生产。自拆分以来,我不关心在生产中保留任何提交,我只希望生产看起来与开发完全一样。[编辑:我希望生产自拆分以来看起来与 dev 完全一样,但不想在拆分之前重写历史] 但是,当我尝试合并时,我遇到了许多我不想手动修复的冲突。

What's the best way to force a merge in git? Can I revert my production changes back to the split and then just fast-forward to the dev branch?

在 git 中强制合并的最佳方法是什么?我可以将我的生产更改恢复到拆分,然后快进到开发分支吗?

采纳答案by smparkes

You can just push your dev branch onto the master repo production branch:

您可以将您的开发分支推送到主存储库生产分支:

git push --force upstream-remote dev:production

upstream-remotemay just be originif you're on a default clone.

upstream-remote可能只是origin如果您使用默认克隆。

Updated for mod'ed question:

更新了修改过的问题:

You probably don't want to revertin the git sense but, yes, that's more or less what you want to do. Something like

revert在 git 意义上,您可能不想这样做,但是,是的,这或多或少是您想要做的。就像是

git checkout -b merge <split hash>
git merge dev
git push --force origin merge:production

<split hash> was the last commit on production that you want to keep.

<split hash> 是您要保留的最后一次生产提交。

回答by Pawan Maheshwari

This will merge your newBranch in existing baseBranch

这会将您的 newBranch 合并到现有的 baseBranch 中

git checkout <baseBranch> // checkout baseBranch
git merge -s ours <newBranch> // this will simply merge newBranch in baseBranch
git rm -rf . 
git checkout newBranch -- .