Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organizing git branches

Tags:

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"

like image 428
mgilson Avatar asked Jun 15 '12 13:06

mgilson


People also ask

What is the best branching strategy in git?

Build your strategy from these three concepts: Use feature branches for all new features and bug fixes. Merge feature branches into the main branch using pull requests. Keep a high quality, up-to-date main branch.

What are the three types of branching in Git?

The two primary branches in Git flow are main and develop. There are three types of supporting branches with different intended purposes: feature, release, and hotfix.

How do you choose a branching strategy?

Every organisation using Git have some form of Branching Strategy if they work in a team and deliver useful software. There is no right branching strategy, and it has always been a point of contention between developers and industry experts about which one is the best.

Are Git branches like folders?

As Melebius said, they both are ways to keep two different versions of your code. A folder is a feature of the filesystem. A branch is a feature of version control systems, which means that Git tracks at which point in the version history you created it. Git lets you merge a branch back into mainline, merging them.


2 Answers

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?

like image 153
jgosmann Avatar answered Sep 24 '22 19:09

jgosmann


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.

like image 40
migu Avatar answered Sep 23 '22 19:09

migu