Git merge master into feature branch

Lets say we have the following situation in git:

  • A created repository:

    mkdir GitTest2
    cd GitTest2
    git init
    
  • Some modifications in the master take place and get committed.

    echo "On Master" > file
    git commit -a -m "Initial commit"
    
  • Feature1 branched off master and some work is done:

    git branch feature1
    git checkout feature1
    echo "Feature1" > featureFile
    git commit -a -m "Commit for feature1"
    
  • Meanwhile, a bug is discovered in the master-code and a hotfix-branch is established

    git checkout master
    git branch hotfix1
    git checkout hotfix1
    
  • The bug is fixed in the hotfix branch and merged back into the master (perhaps after a pull request/code review):

    echo "Bugfix" > bugfixFile
    git commit -a -m "Bugfix Commit"
    git checkout master
    git merge --no-ff hotfix1
    
  • Development on feature1 continues:

    git checkout feature1
    
  • Now my question: Say I need the hotfix in my feature branch, maybe because the bug also occurs there. How can I achieve this without duplicating the commits into my feature branch? I want to prevent to get two new commits on my feature branch which have no relation to the feature implementation. This especially seems important for me if I use Pull Requests: All these commits will also be included in the Pull Request and have to be reviewed although this has already been done (as the hotfix is already in the master).

    I can not do a git merge master --ff-only : "fatal: Not possible to fast-forward, aborting.", but I am not sure if this helped me.


    You should be able to rebase your branch on master:

    git checkout feature1
    git rebase master
    

    Manage all conflicts that arise. When you get to the commits with the bugfixes (already in master), git will say that there were no changes and that maybe they were already applied. You then continue the rebase (while skipping the commits already in master) with

    git rebase --skip
    

    If you perform a git log on your feature branch, you'll see the bugfix commit appear only once, and in the master portion.

    For a more detailed discussion, take a look at the Git book docs on git rebase (https://git-scm.com/docs/git-rebase) which cover this exact use case.


    How to merge the master branch into the feature branch? Easy:

    git checkout feature1
    git merge master
    

    There is no point in forcing a fast forward merge here, as it cannot be done. You committed both into the feature branch and the master branch. Fast forward is impossible now.

    Have a look at gitflow. It is a branching model for git that can be followed, and you unconsciously already did. It also is an extension to git which adds some commands for the new workflow steps that do things automatically which you would otherwise need to do manually.

    So what did you do right in your workflow? You have two branches to work with, your feature1 branch is basically the "develop" branch in the gitflow model.

    You created a hotfix branch from master and merged it back. And now you are stuck.

    The gitflow model asks you to merge the hotfix also to the devel branch, which is "feature1" in your case.

    So the real answer would be:

    git checkout feature1
    git merge --no-ff hotfix1
    

    This adds all the changes that were made inside the hotfix to the feature branch, but ONLY those changes. They might conflict with other development changes in the branch, but they will not conflict with the master branch should you merge the feature branch back to master eventually.

    Be very careful with rebasing. Only rebase if the changes you did stayed local to your repository, eg you did not push any branches to some other repository. Rebasing is a great tool for you to arrange your local commits into a useful order before pushing it out into the world, but rebasing afterwards will mess up things for the git beginners like you.


    Basing on this article you should:

  • create new branch which is based upon new version of master
  • merge your old feature branch into new one
  • resolve conflict on new feature branch
  • This way your history stays clear because you don't need back merges. And you don't need to be so super cautious since you don't need git rebase

    链接地址: http://www.djcxy.com/p/45058.html

    上一篇: 什么是git快

    下一篇: 将主人合并到功能分支