Search code examples
gitrebase

git rebase branches with complex history


I currently have the following situation (simplified).

   master            C --+-- E
                     |   |   |
   hotfix            |   D --+
                     |       |
  develop    A - B - C ----- F - G - H - I
                                     |
  feature                            + J - K - L

I want to end up with:

   master            C --+-- E ------+
                     |   |   |       |
   hotfix            |   D --+       |
                     |       |       |
  develop    A - B - C ----- F - G - | --- H - I
                                     |
  feature                            + J - K - L

How would I go about doing this in a decent git-way? Everything in feature is non-dependend on G, since everything that's editted on feature is simply in a seperate folder.

I've tried the following (while on feature), but all these seem to leave traces of commits after F in feature:

 1. git rebase --onto master develop feature
 2. git rebase --onto E J~1
 3. git rebase --onto master develop

Solution

  • Since I could not wait any longer for a thorough solution, I decided to use the cherrypick suggestions. Though, I did not want to go ahead and cherrypick straight on the master branch, that would be bad practice in my git flow workflow. So I did the following:

    1. Start a new hotfix

      $ git flow hotfix start vx.x.x
      
    2. Cherry-pick the commits from feature

      $ git cherry-pick J^..L
      
    3. Finish the hotfix, essentially merging the cherry-picked commits into master and develop

      $ git flow hotfix finish vx.x.x
      
    4. Make sure I never get to merge the commits from the old feature branch by deleting them locally and potentially on origin.

      $ git branch -d feature
      $ git push origin :feature
      

    So, after this, I got the following:

    master            C --+-- E --+---------- O
                      |   |   |   |           |
    hotfix            |   D --+   J - K - L --+
                      |       |               |
    develop   A - B - C ----- F - G - H - I - M
    

    I'm still convinced I should've been able to solve this somewhat more elegantly with rebase, but I think this did the trick on a marginally elegant way.