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.
$ 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 2: Write Code" >> design.txt
$ git add -A
$ git commit -m "C0: Added Design File"
Lets add some more changes:
$ git add -A
$ git commit -m "C1: Modified Design File"
If you check the history, you’ll find:
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:
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:
Now if you check all branches:
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:
Switched to branch 'bugfix'
You can check which branch you are using with status or “branch -a” command:
On branch bugfix
nothing to commit, working directory clean
$ git branch -a
* bugfix
master
Now, let’s fix the bug:
$ 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:
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:
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:
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:
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