Andrea Loprieno
Andrea Loprieno

Reputation: 55

Git: Handle sub branches

I've tried for almost a day to find out the best way to handle this situation, but even if I found and understood many things about how git works, I was not able to find any 'conventional' way of handle and solve the following situation on git (because of my lack of experience). In our project we have a develop branch and from this branch we need to create a new big feature branch that will be used to put together other small functionalities that will be developed in other sub branches, starting from this feature branch.

A--B--C--D (develop)
          \
           E--F--G (feature-branch)
                  \
                   H (sub-branch)

The reason for doing that is that we don't want to touch our develop branch until the entire feature is completed and, at the same time, if some bug fixes will be needed into the develop branch, we want to be able to apply them. At the same time we want to keep up to date our feature-branch if some changes (or bug fixes) will be applied to the develop branch. So from this situation

A--B--C--D--I--J--K (develop)
          \
           E--F--G (feature-branch)
                  \
                   H (sub-branch)

We want to have the following

A--B--C--D--I--J--K (develop)
                   \
                    E--F--G (feature-branch)
                           \
                            H (sub-branch)

Usually when creating a simple feature branch from develop I use to rebase my branch onto develop to move my commits and keep the history clean. In the explained scenario instead, me and my collegues we are sharing the feature-branch and we are creating sub branches from there, so how to keep the feature-branch up to date with the new commits added to the develop branch? Is it possible to rebase in the same way as we do for our local private branches without any side effects? Any example with git commands will be appreciated

Thanks in advance for every hint/resource or suggestion.

Upvotes: 4

Views: 3862

Answers (2)

Marina Liu
Marina Liu

Reputation: 38096

When there has new changes pushed to develop branch (as commits I, J and K in below graph):

A--B--C--D--I--J--K (develop)
          \
           E--F--G (feature-branch)
                  \
                   H (sub-branch)

You can use below steps to keep feature branches up to date:

1. Rebase sub-branch on the top of develop branch

Execute below commands to rebase sub-branch:

git checkout sub-branch
git pull origin develop --rebase
git push origin sub-branch -f

Then the commit history will be:

                    E'---F'---G'---H' (sub-branch)
                   /
A--B--C--D--I--J--K (develop)
          \
           E--F--G (feature-branch)

2. Reset feature-branch

Use below commands to reset feature-branch:

git checkout feature-branch
git reset --hard G'
git push origin feature-branch -f

Then the commit history will be:

A--B--C--D--I--J--K (develop)
                   \
                    E'--F'--G' (feature-branch)
                             \
                              H' (sub-branch)

Upvotes: 1

Richard
Richard

Reputation: 108975

Firstly "sub-branch" is purely your concept, in git all branches are equal, there is no tracking of how branches relate to each other (only how commits relate when do operations like finding a common merge base).

All you need to do is merge from develop into feature-branch.

Assuming changes have been pushed by others into one or other of these branches (using git fetch and git merge remote/branch can avoid network round trips, but here I'll keep it simple):

git checkout develop
git pull
git checkout feature-branch
git pull
git merge develop
# Resolve any merge issues... which may need an explicit git commit
git push

feature-branch is now up to date with all changes committed to develop.

If you have only some changes in develop you want to merge then you may need to cherry pick some commits.

If any topic branches live for more than a short period the same process can be used to update a topic branch with the latest merges into feature-branch.

Upvotes: 4

Related Questions