Search code examples
gitmergedevopsgit-submodules

Git merging strategy with different branches of submodules


My current environment branches

  • master which contains submodule SMMaster
  • dev which contains submodule SMDev

My Current Merging Strategy when developing a feature

  1. Create branch feature from dev
  2. Add commits to feature
  3. Merge feature to dev and submit for review
  4. Once accepted, Merge feature to master
  5. Now master contains submodule SMDEV
  6. So I change the submodule to SMMaster and commit to the master

Issue

  • Everytime I have to build a feature, I have to make another commit on the master branch just for swithching submodules. Any better merge strategy? Thanks

Solution

  • The problem with your workflow is, you use the SMDev branch for submodule. Behavior of git is correct and expected.

    If you are creating the feature branch from dev, so then you have all changes from dev to merge to the master.

    Solution

    cherry-pick

    First solution if you are gonna stay with your workflow is cherry-pick instead of merging, but it is not how git is designed to work...

    update workflow

    Do you need the SMDev branch for submodule work with this workflow:

    1. Create new feature branch off master
    2. Update submodule, but never commit it. Skip the git add command for submodule.
    3. Merge the feature to dev
    4. Test properly in dev
    5. Merge the feature to master

    It introduced some manual work for developers

    prepare-commit-msg hook

    You can setup some hooks which can change your submodule branch when you are doing merge to master. For more information see this page: https://mirrors.edge.kernel.org/pub/software/scm/git/docs/githooks.html#_prepare_commit_msg