gitgit-workflow

What's the best way of keeping changes that should never move to main branch?


Project sources have multiple conditionals that used to only test many different scenarios, but such logic should never slip to production-ready state of the code (it's a requirement).

Thus, when I need to develop new "feature", I take "production", apply "test" changes on top of it, implement the "feature" by typing in the new logic and when tests pass (some auto, some manual), remove "test" changes and then commit "feature" to "production".

Are there any Git practices that might assist in that?
Everything I've tried: worktree, git merge --no-commit, patch.


Solution

  • I'm not quiet sure I understand your question correctly. But if I do you should be able to utilize git rebase for this.

    So, if I understand correctly you want to track some code for testing that should never be included in the branch which is used for production. While developing a new feature you need that code to be present but when merging the new feature to the production branch it should not include the (commits with) test code.

    For this you would need three branches:

    In the beginning these branches can simply build on top of each other:

    o---o---o  <-- production
             \
              T1---T2---T3  <-- tests
                          \
                           F1---F2---F3  <-- feature1
    

    Where the commits T1-T3 introduce the test code you don't want to be part of production. When starting a new feature branch feature1 it is first the same as tests and then you add the commits F1-F3 for the new feature to it.


    When you are done with development you can now rebase the commits of frature1 on top of tests back on production, see git rebase --onto:

    # while on branch feature1
    git rebase tests --onto production
    

    which will result in

    o---o---o  <-- production
            |\
            |  F1'---F2'---F3'  <-- feature1
             \
              T1---T2---T3  <-- tests
    

    Now you can just merge/fast-forward production to feature1 with

    git checkout production
    git merge feature1
    

    which will result in

    o---o---o---F1'---F2'---F3'  <-- production, feature1
             \
              T1---T2---T3  <-- tests
    

    So now you have all the new feature commits of feature1 included in production but the commits T1-T3 are still in their own branch.


    For the next feature you can then do the same with a new branch feature2 branching from tests again. But first you need to include the new feature commits in tests which you can either do with git rebase tests on production or git merge prodcution:

    # on branch tests
    git rebase production
    

    which will result in

    o---o---o---F1'---F2'---F3'  <-- production
                              \
                               T1'---T2'---T3'  <-- tests
                                             \
                                              F4---F5---F6  <-- feature2
    

    or

    # on branch tests
    git merge production
    

    which will result in

    o---o---o---F1'---F2'---F3'  <-- production, feature1
             \                \
              T1---T2---T3-----o  <-- tests
                                \
                                 F4---F5---F6  <-- feature2