What Is Branch?
Recap of the branch concept
As explained in Chapter 1, a branch in Git is an independent line of development with a commit history. Each branch provides a dedicated recording space and has its own coding history (a line of commits).
Branches allow us to manage different versions of the same set of project files simultaneously. For example, one developer can work on adding a new promotion campaign feature while another developer is working on adding a new payment feature to the same web application.
Git provides a master branch as a default. (If the repository was initiated on the GitHub platform, the default branch may be called the main branch. Check master branch vs. main branch.) Unless you create a new branch, all your work is done on the master branch. The branch to create a new feature is typically called a topic branch.
Once development is completed in a topic branch, you can merge it with the master branch or its parent branch.
With the branching functionality, you can efficiently collaborate with others. Or you can even utilize branches by yourself to manage versions to develop different features simultaneously.
Branch illustrations
In this chapter, we'll show how branches are recorded. The main figure shows two illustrations. Both illustrations have codes like M1, M2, etc. Each of them is a representation of one commit.
- Tree diagram: The tree diagram is used to describe how branches diverge and integrate along coding work. This tree diagram is commonly used to get an overview of branches with a commit history.
- Box illustration: The box illustration describes how branches store commits. A branch is like a file box for storing snapshots of coding history files. When a new commit is made, a snapshot of the project files for the commit is stored in the file box. Also, when one branch merges with another branch, the history of commits can be carried forward.
Branch tree diagram in the command line
You can confirm the branch tree status in the command line by running the git log
command with the --oneline
and --graph
option.
git log --oneline --graph
The command line responses below are examples of the branch logs aligned with the illustrations. The seven-digit code in yellow is a commit hash (short version) and you can also see the status of each branch. You can compare the commit messages (M1, M2,..) with the illustration. As all branches are merged with the master branch, you can see all the branch statuses from the master branch. However, for Branch A, B, and C, you can see the commit histories which are related to their own histories.
Example 1: Master Branch's Commit History
You can see the histories of all the branches from the master branch as all the branches are already merged with the master branch.
* 98a7a09 (HEAD -> master, origin/master, origin/HEAD) M7
* f44a7b1 M6
|\
| * 55e0a1a (origin/Branch_B, Branch_B) B3
| |\
| | * 089bade (origin/Branch_C, Branch_C) C1
| |/
| * 3d7f400 B2
| * 42ba160 B1
* | c6835c2 M5
* | fd694e3 M4
|\ \
| |/
|/|
| * 59ca1dc (origin/Branch_A, Branch_A) A2
| * cb28ee8 A1
* | 43ca3d2 M3
|/
* 5eb04c5 M2
* eadeda0 M1
Example 2: Branch A's Commit History
On the other hand, you have a limited view from Branch A. This is because all the changes after commit A2 were made on the other branches.
* 59ca1dc (HEAD, origin/Branch_A, Branch_A) A2
* cb28ee8 A1
* 5eb04c5 M2
* eadeda0 M1
Example 3: Branch B's Commit History
You can see that Branch C diverged from Branch B at commit B2 and merged with Branch B at commit B3.
* 55e0a1a (HEAD, origin/Branch_B, Branch_B) B3
|\
| * 089bade (origin/Branch_C, Branch_C) C1
|/
* 3d7f400 B2
* 42ba160 B1
* 43ca3d2 M3
* 5eb04c5 M2
* eadeda0 M1
Example 4: Branch C's Commit History
You can see that Branch C diverged from Branch B. However, the commit merging Branch C into Branch B is not visible from Branch C's status as the merge operation was done on Branch B. There is no impact on Branch C from the merge operation on Branch B.
* 089bade (HEAD, origin/Branch_C, Branch_C) C1
* 3d7f400 B2
* 42ba160 B1
* 43ca3d2 M3
* 5eb04c5 M2
* eadeda0 M1
The video below shows how different versions of code are displayed when you switch branches.
On the following topic pages, we'll explain how to manage branches in detail.
The demo code is available in this repository (Demo Code).
Note: Bring the demo code to your local computer
We'll explain how to run branch-related commands. Here are the heads-up in case you want to try them first.
You can clone the master branch and check the branch status by running the following commands.
git clone git@github.com:git-github-introduction/what-is-branch-demo.git
cd what-is-branch-demo
git branch -a
Then, check out all other branches.
git checkout Branch_A
git checkout Branch_B
git checkout Branch_C
git checkout master
git branch -a
You'll see that the four remote branches are now available on your local computer. We'll explain the branch concepts and commands in detail in this chapter.