mgilson
mgilson

Reputation: 310167

Organizing git branches

I have a large-ish project that I'm working on which uses git as the VCS. At any given time I'm working on implementing a few features/bugfixes, etc. For any given feature/bug, It would be nice to create a hierarchy of branches -- e.g.

$ git branch
  feature1
       sub-branch1
       sub-branch2
       sub-branch3
  feature2
       sub-brancha
      *sub-branchb  #<--currently checked out
       sub-branchc
  bugfix1
       sub-branch-foo

$ git checkout sub-brancha
$ git branch
  feature1
       sub-branch1
       sub-branch2
       sub-branch3
  feature2
      *sub-brancha  #<--currently checked out
       sub-branchb  
       sub-branchc
  bugfix1
       sub-branch-foo

Is it possible to do something like this, or do I need to adopt a more primitive naming scheme?

EDIT

To make it slightly more concrete what I'm looking for, if feature1 is a git branch, then in the example above, sub-branch1 would all have been created by git checkout -b sub-branch1 from the feature1 branch (which is branched from master). e.g.:

$ git checkout master
$ git checkout -b feature1
$ git checkout -b testing
$ git branch
master
   feature1
     *testing
$ git checkout master
$ git checkout -b feature2
$ git branch
master
   feature1
      testing
  *feature2

Having git branch simply organize branches by where they came from (with a little extra indentation) is probably good enough for my purposes ... Although super bonus points if I can have:

$ git branch
  feature1
       testing
  feature2
       testing
  bugfix1
       sub-branch-foo

With some way to manage the name-conflict between "feature1/testing" and "feature2/testing"

Upvotes: 24

Views: 24986

Answers (3)

zmk110
zmk110

Reputation: 902

I've personally used this approach in earlier versions of Git, but it seems that nested branches are no longer supported in version 1.8+.

It was also nice to see the UI support in Git Tower.

enter image description here

Upvotes: 0

migu
migu

Reputation: 1401

Branches in Git are in fact symbolic refs to a key in Git's object store. To quote the documentation ("What is a branch"):

When we need to be precise, we will use the word "branch" to mean a line of development, and "branch head" (or just "head") to mean a reference to the most recent commit on a branch. In the example above, the branch head named "A" is a pointer to one particular commit, but we refer to the line of three commits leading up to that point as all being part of "branch A".

Git manages only a list of references which are stored in files under .git/refs. Those references are simply not designed as a tree-like structure.

I recommend to use a branch naming scheme that conveys the hierarchy.

Another possibility is to write an extension that handles the branch tree for you.

Upvotes: 3

jgosmann
jgosmann

Reputation: 770

You can use a naming schemata like feature1/sub-brancha, feature2/sub-branchb and so on, the slash in the name is not a problem for git. However, the branches will still be handled as normal branches (but I wouldn't know how one could handle subbranches differently). The git branch command will list the branches of course with its full name and not the way it's in the example.

Interestingly, the naming schemata including slashes will create a directory hierarchy in .git/refs/heads/. Maybe that's useful for handling the subbranches with some low level commands?

Upvotes: 19

Related Questions