Git bi-directional merging

£可爱£侵袭症+ 提交于 2019-12-06 03:32:38

Suppose your repo has such a structure:

... --- A1(branch_A)
         \
          \
... ------ B1(branch_B)

My suggestion is:

(1) Just in case, backup your branches first by git branch bak_A branch_A and git branch bak_B branch_B.

(2) git checkout branch_A, do the modifications (by cherry-picking or by merging, whatever you want) then commit, you will get a A2 like this (the edge between B1 and A2 may not exists, and it doesn't matter):

... --- A1 --- A2(branch_A)
         \    /
          \  /
... ------ B1(branch_B)

(3) git checkout branch_B, then git commit-tree branch_B^{tree} -p branch_B -p branch_A -m "<commit messages>" | xargs -I {} git merge {}, you will get:

... --- A1 --- A2(branch_A)
         \    / \
          \  /   \
... ------ B1 --- B2(branch_B)

in which B1 and B2 have the same content.

(4) Now you can continue your development as before. If anything goes wrong, you can restore your branches by git branch -f branch_A bak_A and git branch -f branch_B bak_B.

For keeping things in the future as painless as possible make sure that B is up to date with the changes from a via merge or rebase and then do the process in reverse unto A. cherry-pick is not a good idea as that creates a new commit sha that won't be shared in the histories and could cause trouble down the road.

Doing a merge from B to A is the simplest way and will keep things easy going forward. The history will show the commit coming from B and it shouldn't cause any problems.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!