Git Does Not Have Branches
Okay, this is not another git rant. Git has taken the world by storm, and anyone complaining should go sit in a corner, and stop wasting our time.
This post is an explanation of the most important quirk in git terminology and how it affects everything you do in version control. You see, while “branch” is a central keyword in git — git in fact does not have branches.
Let’s start at the beginning. What is a branch?
A version control system tracks our development as a set of change sets, gradual steps that lead from “Version A” to “Version B”.
But in the real world, things are not always linear. Sometimes we start at one point, but “branch” in two directions. It may be because we are developing two features in parallel (i.e. “feature branches”) or because we want to introduce a fix to an old version (“fix branch”). The effect is the same -we now have parallel development tracks. In the pre-git world we called these “branches”
Turns out that git does NOT call the path “a branch”. In fact, git does not even save the fact that this path was created for a purpose (“To fix bug 123”) anywhere.
In git-speak, a branch is just a pointer to the head of a code change path. A place where you can commit more changes if you would like.
Why does this matter?
It means that git does not store the rationale of branching, the reason for why we branched in the first place. This can be very confusing:
And this is not a rare scenario. It actually happens every time you merge code from on branch to another. Looking at the history of an actual git repo exemplifies this pretty well:
And what is the lesson of this? When branching in git, you should maintain a consistent practice and not be “overly creative”.
Because git does not have branches, it has pointers that somehow got the wrong name. Branching in git is very simple, but always keep in mind that the cost of a new branch is not the complexity of opening the branch — it is risking your ability to understand the motives behind changes in hindsight.
There are several branching strategies, and each has it’s pros and cons. Stick to one and you should be fine.