Understanding branching in Git

Understanding branching in Git


Introduction to branching in Git

The concept of branching in git is broad and it would be better understood with a more practical approach.

But then it is good to understand what a branch is why we should ever create a branch and when to create a branch.

What is branching in Git

Branching in Git is a function that is used to create an independent, similar copy of the current repository for different usage requirements.

In other words, branching means diverging from whatever you have been doing to a new lane where you can continue working on something else without disturbing your main line of work.

It is widely used in bug fixing and in the addition of new features to an existing project.

Why is branching necessary in Git

To understand the why part, let’s consider a situation where you are working in a team where different people contribute to different pieces existing in your project.

Your entire team recently launched phase one of your project and is working towards phase two.

Unfortunately, an important feature was not implemented in phase one All of a sudden your priority shifts to implementing the feature first, thereby dropping whatever you’ve been doing for phase two and quickly implementing that feature in phase one.

To handle this kind of situation, you have the branching concept, which allows you to work on multiple things without obstructing the active branch(master branch).

When do you need a branch With Git, There are no strict rules on when you need to create a branch.

You can have your own technical or even organizational reasons to do so. From my experience using git, I will suggest the following to give you an idea:

Most times, when building software, developers do try implementing a piece of logic in an existing codebase severally before it works.

During this experimental phase, the actual released version of the application should not be disturbed. In such a case, a branch is needed.

And the classic one is that few people in the team might be working on the bug fixes of the released version, whereas the others might be working on the next phase/release, branching is also very applicable in this case.

Branching is also very applicable in bug fixing.

Enough of the explanation lets create our first branch with git

  1. Create a directory called Branching on your desktop.
  2. Create two files named index.html and style.css inside the Branching directory.
  3. Add the following code into index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <link rel="stylesheet" href="./style.css" />
    <title>Experimenting git branch</title>
  </head>
  <body>
    <div class="container">
      <h1>Welcome to code school</h1>
    </div>
    <div class="container">
      <button class="btn-register">Register</button>
    </div>
  </body>
</html>

4. Add the following code into style.css

.container {
    display: flex;
    justify-content: center;
    align-content: center;
 }
.btn-register {
    color: white;
    border-radius: 5px;
    width: 80px;
    height: 30px;
    background-color: green;
 }

5. Save the files.

6. open the Branching folder via the terminal and initiate it as a Git repository.

7. add all the files, and make a commit as follows:

git init
git add .
git commit –m 'added initial project scaffold with the initial piece of code'

Boss: Hey work on the color of the heading it is so horrible to the sight.

You: Okay I will. What just happened? You were asked to fix a bug in the project.

  1. Create a new branch called bug_fixes using the following syntax:
git branch bug_fixes

2. Enter the bug_fixes branch using the following syntax:

git checkout bug_fixes

3. Now open the style.css file and change the header color to green by adding the following syntax:

h1 {
color: green;
}

4. Let’s add the changes made in the style.css file and commit it.

git add .
git commit –m 'change the colour of the header in style.css'

5. Let’s check if this change has affected the master branch.

git checkout master

What do you notice? For me, the master branch is not affected.

6. Let’s add a login button to the container class our index.html file as follow:

<div class="container">
  <button class="btn-register">Register</button>
  <button class="btn-register">Login</button>
</div>

7. Now we need to merge bug_fixes branch to master branch with the following command.

git checkout bug_fixes
git merge master
git checkout master

Notice that the header color in the master branch is now green.

8. To view a list of branches available in the system, use the command

git branch

Bravooooooo!!!!! you can now grab a bottle of beer. We can now create a branch and merge one branch to another. In order to solidify your understanding of the concept of git, I urge you to actively engage yourself with the practice project below.

Practice Project

Try to build a repository alongside the idea explained in this module. Have one mainline branch called development and five-component development branches, which should be merged in after the customizations are made to the source.


Share on social media

//