简体   繁体   English

通过基于另一个分支创建新的签入来“合并” Git

[英]Git 'merge' by creating a new checkin based on another branch

I have a git branch, dev , that is exactly where I want my other git branch, master to be. 我有一个git分支dev ,这正是我想要另一个git分支master的位置。 master has some hotfixes on it that are further in the future than the common ancestor of both so this command chain creates complicated and silly merge conflicts: master有一些修补程序,它们在将来都比两者的共同祖先更远,因此此命令链会产生复杂而愚蠢的合并冲突:

git checkout master
git merge dev # Complicated merge conflicts. Boo hiss.

What I would like to do is make a new commit on master that has the state of dev HEAD on it. 我想做的是在具有dev HEAD状态的master上进行新提交。 From then on things can proceed in a more sane manner 从那时起,事情可以以更加理智的方式进行

Example: 例:

dev:    A ---> B ---> C ----> D
master: A ---> Y ---> Z ----> *

A is my common ancestor. A是我的共同祖先。 Y and Z are conflicting hotfix changes, so the three-way merge between D, A, and Z is a mess. Y和Z是冲突的修补程序更改,因此D,A和Z之间的三向合并是一团糟。

In the end I want this result: 最后我想要这个结果:

  • HEAD of master contains the exact same code as the HEAD on dev master的HEAD包含与dev上的HEAD完全相同的代码
  • History is not rewritten, since both branches have been pushed to remotes 由于两个分支都已推送到远程,因此历史记录不会被重写
  • A nice commit message that explains this mergeish commit 一个不错的提交消息,解释了这种合并提交

You could try using the ours merge strategy: 您可以尝试使用ours合并策略:

git checkout dev
git merge -s ours master
git checkout master
git merge dev

The merge to master will create a merge commit, but the result of the commit will ignore all changes made in master . 与master的合并将创建一个合并提交,但是提交的结果将忽略master所做的所有更改。 Then you simply fast-forward master to match dev . 然后,您只需简单地将master匹配dev

From the documentation for merge strategies : 有关合并策略文档中

 ours 

This resolves any number of heads, but the resulting tree of the merge is always that of the current branch head, effectively ignoring all changes from all other branches. 这样可以解析任意数量的head,但是合并的结果树始终是当前分支head的树,有效地忽略了所有其他分支的所有更改。 It is meant to be used to supersede old development history of side branches. 它旨在取代侧支的旧开发历史。 Note that this is different from the -Xours option to the recursive merge strategy. 请注意,这与递归合并策略的-Xours选项不同。

To get a new commit on master that gives an exact match to what is on dev you can do the following: 要在master上进行新提交,使其完全匹配dev上的内容,可以执行以下操作:

git checkout dev                         # Get the working directory we want.
git symbolic-ref HEAD refs/heads/master  # Move HEAD to master without changing 
                                         # index or working directory.
git commit                               # Create the new commit on master.

This creates a completely new commit separate from the dev branch. 这将创建一个与dev分支分开的全新提交。

My inspiration comes from here: https://stackoverflow.com/a/6070417/2348315 我的灵感来自这里: https : //stackoverflow.com/a/6070417/2348315

If you follow this with a merge commit between dev and master would need to resolve these separate histories. 如果遵循此步骤,则开发人员和管理员之间的合并提交将需要解决这些单独的历史记录。 Creating a merge commit immediately after this operation should be conflict free but it will have to be a merge commit rather than a fast-forward merge. 在此操作之后立即创建合并提交应该没有冲突,但是必须是合并提交而不是快速合并。

This approach would make sense if you were cherry-picking changes across and had got into a conflict situation that you want to resolve in the way you describe. 如果您正在寻找变化,并陷入了您要以描述方式解决的冲突情况,则此方法将很有意义。 If your workflow though is to merge changes across then @Cupcake 's merge -s ours approach seems a better fit. 如果您的工作流是要合并更改,那么@Cupcake的merge -s ours方法似乎更合适。

  1. fork marster into a new branch hotfix 将marster分支到新的分支修补程序中
  2. reset master to the ancestor you want (A) 将主重置为您想要的祖先(A)
  3. merge D (== head/dev) into master 合并D(== head / dev)到master

for details on reset the great book gitpro : http://git-scm.com/2011/07/11/reset.html 有关重置很棒的书gitpro的详细信息: http ://git-scm.com/2011/07/11/reset.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM