Last Updated: February 25, 2016
·
1.609K
· klj613

Branching off a branch

If your workflow consists of branching off master then a pull request/code review. Then you may get in a situation where your next branch requires the non-merged branch.

At this point you would try and branch off a branch. Which would look like:

o-o-o master
     \
      -o-o-o-o-o feature-x
                \
                 -o-o-o-o-o feature-y

However if you add a commit to feature-x it would look like:

o-o-o master
     \
      -o-o-o-o-o-o feature-x
                \
                 -o-o-o-o-o feature-y

This means feature-y will not have the new commit. Therefore you would need to git checkout feature-y and git rebase feature-x which will end up like:

o-o-o master
     \
      -o-o-o-o-o-o feature-x
                  \
                   -o-o-o-o-o feature-y

However if feature-x has completely changed (rebase / rewrite history), it would look like:

o-o-o master
     |
     |-x-x-x-x-x feature-x
     \
      -o-o-o-o-o
                \
                 -o-o-o-o-o feature-y

At this point you would need to rebase feature-y onto feature-x and remove the duplicated commits (yes, you have to remember which commits are which). To do this you'd git checkout feature-y git rebase feature-x -i and remove the commits you do not want. Which will end up like:

o-o-o master
     \
      -x-x-x-x-x feature-x
                \
                 -o-o-o-o-o feature-y

When feature-x is finally merged into master. Assuming feature-y is up to date with feature-x it would look like:

o-o-o------------o master
     \          /
      -x-x-x-x-x feature-x
                \
                 -o-o-o-o-o feature-y

At this point we would just simply git checkout feature-y and git rebase master which would look like:

o-o-o-------------o master
     \           /|
      -x-x-x-x-x- | feature-x
                  \
                   -o-o-o-o-o feature-y

Note

If you forget to remove the duplicated commits when rebasing you may end up with conflicts during the rebase and after you've resolved the conflicts if git rebase --continue isn't working, it means there are actually no changes so it will not apply the commit. At this point you would simply git rebase --skip.