Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

merging to branches, doesn't matter which one you merge into?

Tags:

git version 1.7.5.4

I have about 5 branches. All from the same initial branch.

I want to merge 2 branches together. say, branch1 and branch2. These branches have a lot of differences.

I am currently working on branch1 and have just realized there are some changes I implemented in branch2 that I want in branch1.

what is the best way to merge?

checkout branch2 and merge branch1 

or

checkout branch1 and merge branch2 

Or doesn't it matter which branch you need to checkout to merge with another branch?

like image 370
ant2009 Avatar asked Oct 04 '11 06:10

ant2009


People also ask

Does merging affect both branches?

So merging one branch into another has a secondary effect: it moves the merge base of those two branches. In particular, the new merge base of the two branches is now the second parent of the merge commit.

Does merge order matter?

The only difference that comes from merging order should be the order of the parents of the merge commit. Edit: If you are interested in more details, torek's answer is what you are looking for.

What happens when merging branches?

When you perform a merge, you effectively merge one branch into another—typically a feature branch or bug fix branch into a main branch such as master or develop. Not only will the code changes get merged in, but also all the commits that went into the feature branch.

Should you keep merged branches?

So basically the only reason to keep hotfix branch after a merge is if you plan to make any more changes to the same hotfix, which doesn't make much sense once you release the hotfix. So you should feel perfectly safe deleting the branch after the merge.


1 Answers

Usually it does not matter if both branches are topic or feature branches.

However, if you have an integration branch or a branch that marks what's been published, you definitely want to use the long lived integration branch as the one that's checked out and merge the other one into it.

The reason for this is that the merge commit will mark the first parent commit as the one coming from the main branch. Your tree-ish specification for the history for that branch is easy now. To find the commit that was the 4th last in this branch, you simply

git show head~4 

If you merged from the other branch somewhere in between, you would have to explicitly switch to the second commit wherever the merge was done the other way:

git show head^^2^^ 

This can cause problems with major branches for another reason; merging them into topic or feature branches is referred to as "back merging" and is not a good idea. I recall Linus Torvalds blowing his top when contributors did that. It would not allow him to cleanly separate what features he wanted to merge for a major revision as the feature branches would bring in an old test merge that included things he does not want anymore.

So in the end, if one branch is more significant and is more than just a feature, check it out and merge from there. You'll be able to keep it's history viewed easily as you know that it's first parent is always where that branch was before. If you don't, you'll have to rely on reading the merge commit messages and it's just not as fun. :)

I've written an article about BpF which shows a rigorous method of keeping branches organized: http://dymitruk.com/blog/2012/02/05/branch-per-feature/

like image 95
Adam Dymitruk Avatar answered Dec 02 '22 04:12

Adam Dymitruk