Search code examples
gitmergefork

How to keep feature branch updated with master branch when both have updated sections of code


I have a master branch and a feature branch. the feature branch gets merged into the master branch with updated code every day or so. However, it does happen often that changes get merged directly into the master branch without going through the feature branch first.

This means that the master branch needs to be merged into the feature branch too. So the master branch contains some updated code and some outdated code and the feature branch contains some updated code and some outdated code.

When trying to merge the master branch into the feature branch sometimes there are cases where the master branch wants to revert changes made on the feature branch, even though the feature branch has the most up to date code.

How can this be prevented?


Solution

  • This means that the master branch needs to be merged into the feature branch too

    That is not a best practice:

    • you merge "upstream" (from feature to master) where the merge conflicts are more limited (unless, in your case, you have concurrent evolution to master from from other change branches)
    • you might not want to merge "downstream" (from master to feature), where merge conflicts are almost assured, because of concurrent changes coming from both master and other branches, both merging to feature.

    Ideally, you just delete feature, and recreate it on top of master, where you merge again the changes that need to be integrated together.

    The integration branch becomes a transient branch, which means it should not even been merge to master at all:

    • you merge your changes to feature, to test them together
    • you then select the changes you want to merge to master, because they are ready to be part of the next release
    • you delete feature, and recreate it.

    That is the idea behind gitworkflow (one word).