Git merging hotfix to multiple branches

后端 未结 4 1008
-上瘾入骨i
-上瘾入骨i 2021-02-01 16:48

I\'ve been trying to wrap my head around git branching models. I\'ve been looking at http://nvie.com/posts/a-successful-git-branching-model/ for some ideas and coming from Subv

相关标签:
4条回答
  • 2021-02-01 17:28

    Really, your answer is dependant on if you want your trees to be based on the same history... For example, 4.0 is based on the latest 3.X + all of the changes in 4.0...

    Personally, I don't recommend it once you decide to start a new branch(s) for a new version(s). At a give point of time, the software is taking a different direction, so your branches should also.

    This leaves git cherry-pick as your ideal solution. Make the change in whatever branch makes the most sense, and then cherry pick it to the older versions. This is the same as if you had checked out the old branch and manually applied the same change, and made a new commit. It keeps it clean and to the point.

    Git merge or rebase are going to try to integrate the branches history together, each in their own way, which I suspect you don't want when backporting bug fixes, etc...

    0 讨论(0)
  • 2021-02-01 17:36

    In the case, you are working on branch "4.0" and have to make a fix on "3.1", you may rebase "4.0" after you commit "3.1":

    Make sure you are on the feature branch 4.0:

    git checkout 4.0
    

    Save current work so you can check out other branch:

    git stash  
    git checkout 3.1  
    

    Do editing and commit:

    git commit -a -m "bug fix"  
    git checkout 4.0  
    

    Get back your changes:

    git stash apply  
    

    Change 4.0 so it branches of the current head of "3.1":

    git rebase "3.1"
    
    0 讨论(0)
  • 2021-02-01 17:38

    I've been struggling with this question, too, and I think if you're willing to change your versioning strategy a little (i.e., depart from the -SNAPSHOT versions that Maven encourages), this could be solved by using a fixed version (like SNAPSHOT or 0.0.0-SNAPSHOT) on master (or whatever your development branch is). (The SNAPSHOT suffix is important, if you're using Maven, since Maven treats SNAPSHOT-versioned artifacts differently than others.)

    In fact, I think you'd want to institute a policy of only ever changing the version number on your production branch (the one from which you build releases) or on branches which are for release purposes only (e.g., changing version numbers) and which you never intend to merge back to the development branch.

    I haven't actually used this strategy yet, but was just thinking about it, and I think I'll start trying it.

    0 讨论(0)
  • 2021-02-01 17:47

    If you wanted to get super-technical about it, you could create the hotfix from a common ancestor:

    git merge-base v3 master
    git checkout -b hotfix <whatever you got from merge-base>
    # make your fix
    git checkout v3 && git merge --no-ff hotfix
    git checkout master && git merge --no-ff hotfix
    
            v3--------v3 (hotfixed)
           /         /
    ancestor----hotfix
           \         \
            master----master (hotfixed)
    

    The --no-ff flag is there to highlight that Git will create a merge commit, keeping the hotfix branch label at the hotfix tip, instead of pulling the label to v3 or master. (You can omit the flag and get the same behavior, since the hotfix branch has one commit that isn't in master or v3. More info in the docs.)

    Personally, I think that's overkill. I'd go with gahooa: make the hotfix on the branch that makes sense, then merge or cherry-pick depending on how you want the branches to relate to each other.

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