What are branches
Created: Last updated:
This document has the answer (hopefully) about what branches are in Git. I was dumbfounded for a while and it was even tougher to find an answer but I think I get it now.
The answer to what branches are in Git lies in the history. At least that's where I found the answer.
I could just simply point you to the Git User's Manual where I found the answer but I don't. You should find that in your Git installation at ./doc/git/html/user-manual.html . Oh, actually I have just pointed to the manual but I will also give an excerpt here and some additional thoughts of mine.
Oh I love this title because I love history. We can learn so much from history and especially in IT; it's so much easier when you know how things evolved.
In this case it is a little bit different with history, though. In Git every change is (should be) logged with commits. All these commits comprise the "history in Git".
Accordingly the User's Manual has a chapter aptly named Understanding History: Commits.
What is a branch?
Within this chapter we also have a sub-chapter What is a branch? with a very short answer to what branches are. Here's the excerpt from it
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. ... However, when no confusion will result, we often just use the term "branch" both for branches and for branch heads.
Got it? No? I didn't really at first but there is a little example just above this sub-chapter and reference in this quote.
It will become clearer when we look at that example which is about History diagrams.
The example looks like this
- o--o--o <-- Branch A
- o--o--o <-- master
- o--o--o <-- Branch B
In the manual it says about diagrams "We will sometimes represent git history using diagrams ..." and this makes it somewhat clear what branches as a concept indeed are.
In this diagram each letter "o" represent a commit and we can see that branches divert—or branch out—from the master. Branch A and Branch B are different development paths or as we now have learned branches.
The idea behind this is to develop without touching the master. At least, that's how I see it.
The other idea and thing to understand about this concept is that we have to later "merge" the branch back into the master. I guess you have also seen and wondered about the merge command.
Since I have this final understanding about branches, merge becomes clear, too. I will end here and not dive into merge and maybe have another article about that later. When I know more about it!