Branching in Git

Johnson Nyaanga
2 min readFeb 11, 2022

--

Oooops!

In a collaborative environment, its typical for several developers to work on a single source code. These developers might be concurrently building different feature of the project, others might be fixing bugs.

Branching enables each developer to branch out from the main source code and develop their feature in isolation. Later on the different isolated versions of the code base are merged together to form a complete project.

A branch is a copy of the original codebase ,you add features on it, test if everything is working correctly and merge back to main branch. If by trying to add a feature on your new branch, you mess up the whole code base , you can always switch back to the main branch that was working.

Create a branch

Lets say for instance you are working a school management app and you currently at the main branch. You’ve been asked to add a grading system feature. So you decide to create branch “grading”. you will write the following command:

git branch grading

To move into the new branch and start working on your feature, you write the command:

git checkout grading

There is a short-hand for the two commands, One that combines the above two commands:

git checkout -b grading

So now you are in your “grading” feature branch, you write some code and everything is working as expected. You want to match to your main branch. This is what you will do, push your code to github from “grading” feature branch , create a pull request from “grading” to “ main” branch . If there is no conflict merge the code.

To access your new version on the main branch, first switch to back to main branch on your git bash terminal by writing this two commands:

git checkout main

git pull

Git pull will fetch all the changes added from GitHub and automatically merge them.

--

--