Ege Hurturk
Ege Hurturk

Reputation: 983

Create a sub-branch in Git

I want to create a sub-branch from an existing branch. My desired history would look like this:

--- master ---- 
               \-*--* model -*-*-
                                 \--*-- subbranch_model --*--

Does the subbranch_model will get the whole history from its parent branch model? If it is, how can I accomplish that?

Upvotes: 3

Views: 5419

Answers (2)

Michael Durrant
Michael Durrant

Reputation: 96584

Yes, the sub-branch will get the history from model.

To subsequently keep the branch up to date with newer changes to model, you can do

git rebase model

To subsequently keep the branch up to date with master, you can do

git rebase master

When you refer to history, you may want to use the term 'changes' instead. In some sense there is only one "history", however with branches there are many changes per branch. Note that all branches contain all the 'history' at the point that they diverged (the branch was made).

Note that git merge is another way to bring in changes but git rebase is generally preferred because when you get conflicts it's generally easier to resolve them under the rebase model.

Upvotes: 2

blami
blami

Reputation: 7431

Yes, that is possible. You just base subbranch_model on model (make model starting-point of history for subbranch_model). It can be done simply like this: git branch <branchname> [<start-point>]. You can also do it during checkout into a new branch: git checkout -b <branchname> [<start-point>].

git branch subbranch_model model
           ^-- branchname    ^
                             `-- start-point

See https://git-scm.com/docs/git-branch for details.

The new branch (subbranch_model) will have all history up to <start-point> (model) but there's no "subbranch" it is just "a branch" that shares common history to certain point.

If you intend making changes to model and subbranch_model at same time, keep both these branches going you will also need to carry over any new changes made to model back to subbranch_model somehow (e.g. continuously rebase subbranch_model on top of updated model). See https://git-scm.com/docs/git-rebase for more details on rebase.

Upvotes: 4

Related Questions