Git Branch Basics

28/12/2020
Git

Basics of Git Branching

The ability to easily branch is one of the best features of Git. Creating branches in other version control systems can be expensive in terms of space and processing requirements. Git branching is efficient. So users are more prone to use branches in Git.

A Branching Workflow

Let’s suppose you started a new project called myvideogame. It has a single branch. The default name of the initial branch in Git is called master. It’s automatically created.  Let’s create the myvideogame Git repository.

$ mkdir myvideogame
$ cd myvideogame
$ git init

You have created an empty Git repository. Let’s add our design.txt file with some text in it.

$ echo "Design Decision 1: Add Images" >> design.txt
$ echo "Design Decision 2: Write Code" >> design.txt
$ git add -A
$ git commit -m "C0: Added Design File"

Lets add some more changes:

$ echo "Design Decision 3: Test Game" >> design.txt
$ git add -A
$ git commit -m "C1: Modified Design File"

If you check the history, you’ll find:

$ git log –oneline
6a09bd6 C1: Modified Design File
5f18d89 C0: Added Design File

If you check Git status and all branches that were created (using the command: git branch -a), you see:

$ git status
On branch master
nothing to commit, working directory clean
 
$ git branch -a
* master

Currently, you have the following situation:

You have made two commits in the master branch.

Let’s suppose, you have found bugs in your game testing, but you don’t want to address the issue in the master branch because you don’t want to mess with the original design yet. So you can create a new branch called bugfix:

$ git branch bugfix

Now if you check all branches:

$ git branch -a
bugfix
* master

Now you have created a new branch called bugfix. The situation can be visualized as this:

However, the star(*) beside the master branch means that you are still in the master. If you make changes it will still go into the master branch. You can use the checkout command to change branches:

$ git checkout bugfix
Switched to branch ‘bugfix’

You can check which branch you are using with  status or “branch -a” command:

$ git status
On branch bugfix
nothing to commit, working directory clean
 
$ git branch -a
* bugfix
master

Now, let’s fix the bug:

$ echo "Bug Fix 1" >> design.txt
$ git add -A
$ git commit -m "C2: Bug Fixed 1"

You have created a situation like this:

The master branch doesn’t have the C2 change. You can easily verify this by checking the history of the two branches.

First, the history of the bugfix branch:

$ git status
On branch bugfix
nothing to commit, working directory clean
 
$ git log –oneline
e8f615b C2: Bug Fixed 1
6a09bd6 C1: Modified Design File
5f18d89 C0: Added Design File

Then you can switch to master branch and check its history:

$ git checkout master
Switched to branch ‘master’
 
$ git status
On branch master
nothing to commit, working directory clean
 
$ git log –oneline
6a09bd6 C1: Modified Design File
5f18d89 C0: Added Design File

You can see the master branch doesn’t have the changes from the bugfix branch.

You can always create a new branch from the current branch you are located in. Suppose, you want to create another branch that will contain experimental features. You can create the branch from master and add experimental features to it:

$ git status
On branch master
nothing to commit, working directory clean
 
$ git branch experimental
$ git checkout experimental
Switched to branch ‘experimental’
$ git status
On branch experimental
nothing to commit, working directory clean
 
$ echo "Adding Experiment features" >> design.txt
 
$ git add -A
 
$ git commit -m "C3: Added Experimental Features"
[experimental 637bc20] C3: Added Experimental Features
1 file changed, 1 insertion(+)

If you check the history of your experimental branch, you’ll see:

$ git status
On branch experimental
nothing to commit, working directory clean
 
$ git log –oneline
637bc20 C3: Added Experimental Features
6a09bd6 C1: Modified Design File
5f18d89 C0: Added Design File

You will notice that you don’t have the C2 commit which was created in bugfix branch. Because experimental branch is created from master branch, it doesn’t see the bugfix changes. You have the following situation:

Conclusion

Congratulations! You have learned how to branch.

Git branches are easy and fast to make. It’s one of the reasons behind Git’s popularity. If you want to become a proficient Git user, you need to become proficient in Git branching.

Further Study:

https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Git Shallow Clone and Clone Depth

Understanding Git Shallow Clone and Clone Depth Git is a distributed version control system. That is one of the advantages...
28/12/2020

Configure Git Server with HTTP on Ubuntu

If you want to setup a Git HTTP Server for working with Git repositories privately, then this article is for you.  In...
29/12/2020

Configure Git Server with SSH on CentOS 8

If you have a small tech company, you can set up an SSH based Git server so that the people working on a project can collaborate...
29/12/2020