Merge Git branches based on the specified commit pair as a common base

Is it possible to merge branches in Git based on the specified pair of commits as a common base?

Scenario:

Two separate Git repositories, "ours" and "them."

"Theirs" was created using a working copy (only files) from "ours" at a known point in time. The initial commit to "theirs" adds a snapshot of "our" files.

Now we want to combine "ours" into "ours":

git remote add theirs <their repository path>
git pull theirs <their branch> --allow-unrelated-histories

git mergewill look for a common ancestor on which the merger will be based. However, there is no common fixation between “ours” and “them”.

As we can find the commit in 'ours', where the working copy corresponded to the original commit in 'them', I think Git should be able to reasonably start the merge, but we will need to tell Git to handle two specific commits as a common base.

+4
source share
2 answers

"Theirs" was created using a working copy (only files) from "ours" at a known point in time. The initial commit to "theirs" adds a snapshot of "our" files.

Probably, in your case, the best option would be to add a replacement with a fake parent for the corresponding "our" message:

git replace --edit <THEIRS_INIT>

add the string " parent <OURS_COMMIT>" to the headers, then save

You can remove the replacement after the merge is complete.

+1

, ,

git fetch theirs

,

A -- B -- C -- D -- E -- F -- G <--(master)

d -- H -- I -- J <--(theirs/master)

(TREE) d d.

H J master, d . , , , - , . .

, . - H J d. , / - , reparent H . ( d d , .)

H, git filter-branch --parent-filter. (SHA) d d, , , sed script, d d. . filter-branch; , .

, , , . , d - , master~3 , - , d - theirs/master~3 .

git checkout theirs/master
git checkout -b temp
git rebase --onto master~3 theirs/master~3

A -- B -- C -- D -- E -- F -- G <--(master)
                \
                 H' -- I' -- J' <--(temp)

d -- H -- I -- J <--(theirs/master)

temp master theirs/master; , H', I' J' - ( ID ..), , theirs , .

, theirs , :

theirs, . " " theirs/master temp temp master.

git checkout temp
git merge -s ours theirs/master

A -- B -- C -- D -- E -- F -- G <--(master)
                \
                 H' -- I' -- J' -- JJ <--(temp)
                                  /
                  d -- H -- I -- J <--(theirs/master)

JJ " " - / J' J. , , , .. , "/ " . .

, J, J' JJ , temp - master

A -- B -- C -- D --- E --- F --- G --- M <--(master)
                \                     /
                 H' -- I' -- J' -- JJ <--(temp)
                                  /
                  d -- H -- I -- J <--(theirs/master)

temp.

d, H, I, J JJ, , theirs.

A -- B -- C -- D --- E --- F --- G --- M -- N -- O -- P <--(master)
                \                     /
                 H' -- I' -- J' -- JJ 
                                  /
                  d -- H -- I -- J -- K -- L <--(theirs/master)

theirs/master master, git , J .

0

Source: https://habr.com/ru/post/1695700/


All Articles