Git Branching Strategies: A complete Guide

Akash Agarwal
3 min readAug 7, 2023

--

In this document, you will learn to use the git branching strategy that we are going to follow throughout the project development process, and also about how to manage your code on GitHub with the help of branching and merging in a professional way.

Pre-Requisites

  • Basic Knowledge of Git and GitHub.
  • A GitHub Repository in your local.

Note — If you don’t have a repo in your local. A whole detailed guide is here.

This is all you need to manage your code in a Professional way.

In almost every repo you will see, they will have 2 branches in common. The first and the most important one is “master” and the second one will be “develop” or “development”.

Here you can see on which branch currently you are working.

Steps to follow

1. Create your branch -

First of all, you need to create a branch of the name of your choice from the “master” branch. The best practice to name your branch is to go like “feature/<name>”. Here <name> means the reason or task for which you are creating the branch.

Now, you must keep in mind that you are in the “master” branch so the base branch for your branch will be the “master” branch. Why start with a master? The answer is — Starting from the “master” branch as the base allows developers to work on new features independently without affecting the stable version, ensuring a clear versioning and deployment process.

Note — The “master” branch serves as the stable and reliable main branch, enabling collaborative development, code reviews, and a historical record of the project’s evolution

The command to check the current branch name is: git branch --show-current

The command to create a branch is: git checkout

git checkout -b feature/<name>

2. How to merge? -

Now that you are done with your changes and are ready to merge your code into the “master” and “develop” branches.

For that, first of all, you need to create 2 more branches. Now, why do we need to do this? The answer is simple: -

For that first of all, you need to create a new branch from the “develop” branch and for ease of understanding you can name the new branch as :

git checkout -b feature/<name>-D

Now let me explain that why we added a -D at the end of the name so that you can specify that this branch will be merged into the “develop” branch. So now you will raise a PR from your feature branch to the newly made branch. The need of doing this is because in the “develop” branch there is the latest code and when you will merge the code it will get synced with it. Also, there might be some merge conflicts and this will also help in resolving them.

Note — If you are not aware of how to raise a PR. A whole detailed guide is here.

Once, your code gets synced with the “develop” branch code, after resolving all the conflicts, you need to raise a PR for the “develop” branch from the branch which you created from it.

i.e. git checkout -b feature/<name>-D

Now, you need to perform the same process with the “master” branch. But this time when you will create a new branch from the “master” branch, instead of writing -D at the end of the branch you will go with -M as this indicates that the branch is created from the “master” branch and will also be merged into “master” branch.

Now the final thing is to get the PR approved, and once it is done your code will be merged into the main branches.

--

--

Akash Agarwal
Akash Agarwal

Written by Akash Agarwal

0 Followers

Full Stack Developer

No responses yet