gitgit-merge

How to create a git workflow based on splitting your current branch into two, and later picking one to PR?


I've create a feature branch feature/CRM-123_some_feature, where CRM-123 is the Jira ticket ID.

I've tried doing it one way, and made a local commit. After testing, the performance was not great, so I've tried a different way, changing my code significantly and retesting.

Now I want to keep this experimental branch, switch back to my earlier code, and try modifying it.

So I kind of have two branches. Later, I want to do a PR for one of them and merge back into develop.

I'm on the original feature/CRM-123_some_feature branch, but with new changes. I assume I could create a new branch, which will be based on my original one, using git checkout -b feature/CRM-123_some_feature-experiment1, and checking my updated code.

I could then checkout my first branch, and try various modifications to that, checking in the results.

When I come to merge with develop, and I merge feature/CRM-123_some_feature-experiment1, but not feature/CRM-123_some_feature, will the changes in feature/CRM-123_some_feature be missing from develop?

Or maybe I need three branches:

feature/CRM-123_some_feature
-- feature/CRM-123_some_feature-experiment1
-- feature/CRM-123_some_feature-experiment2

and need to merge one of the two experiment branches back into the original feature/CRM-123_some_feature branch and PR that, if PR'ing the experiment2 branch on its own will lose the original branches changes?

What is the simplest solution?


Solution

  • If you have made further commits on feature/CRM-123_some_feature after creating feature/CRM-123_some_feature-experiment1, then the changes introduced by those commits won't be included when merging feature/CRM-123_some_feature-experiment1 into develop.

    So, to visually represent the situation described above, the changes introduced by commits G and H won't be merged into develop when doing a PR from feature/CRM-123_some_feature to develop.

    A -- B                       develop
          \
           C -- D -- G -- H      feature/CRM-123_some_feature 
                 \
                  E ----- F      feature/CRM-123_some_feature-experiment1
    

    But, If your situation is the following, then every change introduced by feature/CRM-123_some_feature will be brought into develop when creating a PR from feature/CRM-123_some_feature-experiment1.

    A -- B                       develop
          \
           C -- D                feature/CRM-123_some_feature 
                 \
                  E -- F         feature/CRM-123_some_feature-experiment1
    

    Keep in mind that, when you merge a branch (feature) into another branch (develop), Git first determines the set of commits to merge by going from the head of the other branch up to the merge base (the commit where the two branches have diverged). Then, applies the commits' changes on top of the target branch.

    So, in the first case, the commits that will concur to the merge will be F, E, D, C, and their changes will be applied on top of develop. Commits G and H will be excluded from the merge, because there is no path from F to B that can reach G and H (in Git commits only point to their parent(s)).