Merging two very divergent branches with git?

I have a master branch and my verydifferentbranch they had the same ancestor ... about 300 commits ago. Now that the verydifferentbranch function verydifferentbranch completed, I would like to put it under the branch wizard. Running rebase causes each patch to have many merge conflicts, to the extent that it will be a big project for itself to go through all the conflicts.

I have no idea what to do except just push the verydifferentbranch head on the master branch. I would lose my whole story doing this, and that’s not what I really want to do.

What are my other options?

+6
source share
4 answers

It looks like your story looks like this:

 ...---o---A---o---...---o---o---B master \ o---o---...---o---C verydifferentbranch 

You say you worry about losing history if you click on a verydifferentbranch on master . Such an operation effectively discards everything after A and up to B

You can save the story by combining it, or simply drop the tag on the abandoned tip of the branch and leave it unaided.

Use merge

Merging will allow you to save both sides of the story:

 ...---o---A---o---...---o---o---B \ \ o---o---...---o---C---M master 

The type of merge you make will determine the content created for fixing M. Normal merging (using the merge strategy recursive ) looks like there will be a lot of conflict in your case. If you really need to enable the changes from the A..B commit, you have nothing more to do but work through conflicts represented by either merging or rebase. (In the future, you will probably have fewer problems if you can combine or reinstall more often to deal with conflicts as they arise.) But if you just want M have the same content as C (i.e. . you want to ignore the changes presented by A..B ), you can use ours merge strategy.

git -merge (1) describes our merge strategy:

This allows any number of goals, but the resulting merge tree always refers to the current branch branch, effectively ignoring all changes from all other branches. It is intended to replace the old history of the development of the lateral branches. Note that this is different from the -Xours option for a recursive merge strategy.

You can create an M with the message Merge commit 'abandoned/foo-features' as follows:

 git tag -m 'describe reason for abandonment here...' \ abandoned/foo-features master # tag abandoned branch tip git checkout verydifferentbranch # checkout dominant branch git branch -M verydifferentbranch master # replace master git merge -s ours abandoned/foo-features # merge only other history git tag -d abandoned/foo-features # optional: delete the tag git push wherever master tag abandoned/foo-features # publish them 

Variations of these commands will give you slightly different auto-commit messages for M (you can always specify your own git merge -m or change it with git commit --amend ).

The main thing is that the resulting master will have both parts of the history, but not one of the changes from the original side of the master (these changes are still in the history, they simply are not presented in the tree to which reference is made by fixing M ).

Leave it hanging

If it is permissible to “rewrite” master (that is, there is no other work based on any A..B A..B , or the involved users do not mind the work that it performs in recovering from overwriting ), then you can simply leave the tag on current end of master and replace master with a verydifferentbranch .

 ...---o---A---o---...---o---o---B (tag: abandoned/foo-features) \ o---o---...---o---C master 

Put it this way:

 git tag -m 'describe reason for abandonment here...' \ abandoned/foo-features master # tag abandoned branch tip git branch -M verydifferentbranch master # replace master git push wherever +master tag abandoned/foo-features # publish them 
+10
source

This is why the git merge function exists.

 $> git checkout master $> git merge verydifferentbranch 

Odds the first time you do this, there will be many conflicts (if they diverge as much as you claim). But if you keep the upper hand over things, after the initial merger it will not be so bad.

+2
source

Why do you do a rebase first? This will probably cause problems with your change sets, as your change sets will look for file versions, starting from a commit where you stray from the wizard.

Use git merge instead.

+1
source

I found that it is easier to reason and resolve merge conflicts, for example, by merging every tenth verydifferentbranch commit into master and thus handle conflicts. Yes, this means that you do 30 merges if you have 300 commits, but the discrepancy is much less significant with each of these merges than if you just tried to combine them all at once.

0
source

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


All Articles