If you're using git having multiple branches can be useful, even if they're dead. You can track in which product version the bug was introduced (bisect by versions), you can organize your work for many small teams. You can see why some ideas didn't work out just by looking at dead branches.
The key is consistency. Try to group your branches to fit your workflow.
You can for example have
stable
- CI builds production and/or staging from this one
staging
- CI build staging from this one
feature/*
- branches for features
hotfix/*
- started from staging/stable branch, used for hotfixes
experimental/*
- used for R&D functionality that might not result in clean and maintainable code or may be abandoned halfway through
Some basic tips here:
http://nvie.com/posts/a-successful-git-branching-model/
Also, if you want your team to quickly start using a good branch structure try git flow: http://jeffkreeftmeijer.com/2010/why-arent-you-using-git-flow/
About refactoring other co-workers bad code. You can use some refactor/*
branches so you can easily see what broken by having merges/rebases atomic on a separate branch. Of course having tests is EXTREMELY useful, but if you don't a simple git bisect
will show you who and when introduced a bug (and if you write a test to check for this bug which bisect would use you now have a meaningful test to add to your test suite).
Bottom line is: don't be afraid of having many branches, just keep them organised. Merges most of the time aren't that complex as people say they are and you can always undo them (or postpone till the next release if you aren't using continous delivery model).
EDIT: By something/*
I mean is having multiple branches with a common something/
prefix, thus mimicking a directory structure.
EDIT2: Things are different on SVN-likes, where branching and merging isn't that cheap. Proceed with caution ;)
EDIT3: Consider using different (sub)repositories for different modules. It might simplify the development. Module developers are only concerned by the latest stable version of the main application and branch model is only oriented towards one module.
EDIT4: Q: "can you perhaps consider to put some numbers or formulae at the threshold below which messy branching is acceptable and above which branches better be organised?"
Sure!
Formula (in my humble opinion) is simple.
- have as many 'dirty' branches locally as you want (just don't push them to other people or shared repositories)
- try not to push dirty branches unless they represent some value for other developers. If you have them already in your shared repository keep them
and rename them (
legacy/*
or plain dirty/*
comes to mind).
Think of them as a file structure. You can have many legacy files no longer needed but if you keep them in an organised manner you can easily separate the archive from your working set.
Seeing as you like numbers you probably would like a real world use case for those branches
Let me give you an example of a small-middle sized Symfony2 PHP project I've been working on.
If you have a project going on for 6-9 months developed actively by 5 developers in agile (scrum) manner with a client demo every two weeks you might want to have branches:
- per user story (on tightly integrated user stories this might be a bad idea), around 50 branches total, think of them as feature branches
- per developer (on demand, if developer needs to work on something for a while), they come and go, but usually developers have less than 3 in this kind of projects. Some of them don't use public developer branches at all and keep their dirty branches to themselves
- experimental (unlimited number of branches for research purposes, for example different algorithm or library used in module), about 7 branches as far as I remember
- per sprint (merged from user stories, helpful for demoing), around 10, these we're our staging/stable during initial development. Why not tags? Tags too, but branches because it's easier to apply a hotfix.
- hotfixes (usually short-lived, isolated for easy cherry-picking), 3 tops ;)
- misc (usually on-going system-wide features and/or 2-3 people team branches), around 10
As you can see there isn't an exact number here either. I've done several projects of this size and most of them had about 70-80 branches (20-30 without user story branches). If they are organised in a logical, clean way the code repository is easy to browse.
With git consider also rebaseing instead of merging, so you won't get merge bubbles (check out this article http://stevenharman.net/git-pull-with-automatic-rebase ).