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

前端 未结 2 1235
情话喂你
情话喂你 2021-02-05 06:31

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

相关标签:
2条回答
  • 2021-02-05 07:02

    I'm assuming that you really do wish to merge the whole branch, rather than just cherry-picking the odd commit. Also, what I'm saying below is heavily based on this very helpful blog post by Junio C. Hamano, the git maintainer, which I would strongly recommend reading if you want to know more about the philosophy of branching.

    Unfortunately, there isn't really enough information in your question to give sound advice about this, because the question depends critically on the purpose of each branch. In one common scenario, for example, one might have a master branch that one should always be able to generate a stable release from. When someone wants to add a new feature to the software, they might create a topic branch from master called awesome-feature which they work on, possibly rebase, test thoroughly, and so on. When everyone is happy with that branch, it then only makes sense to merge awesome-feature into master, not the other way round. (The other way round would mean, approximately, that everything in master helps to implement the "awesome feature" that the topic branch is for.) You can only tell which way round is right, however, because we know the purpose of each branch.

    Branching and merging is so easy in git that it supports many different workflows, from highly structured, to rather simpler, to totally unstructured. By "totally unstructured", I mean that there are several different branches of development, and people merge between them in whatever ways seem to include the features they want in a particular branch - if you're in such a situation, where there's no clearly defined purpose for each branch, it might not matter whether you merge branch1 into branch2 or the other way round. However, I find it much more helpful to have a clearer purpose for each branch, in which case the way you merge two branches does matter.

    0 讨论(0)
  • 2021-02-05 07:04

    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/

    0 讨论(0)
提交回复
热议问题