Git Branch Concept and work on the Git branch

What is a Git Branch?

A Git branch is a separate line of development that allows you to work on a specific feature or version of your project without affecting the main codebase. It enables multiple developers to work on different parts of the same codebase simultaneously and merge their changes together later.

In general, a branch means a separate workstation. Like this concept of a separate workstation, we can also create separate branches in Git according to the steps of our workflows, like Development (dev), Testing (test), or Production (prod), or based on the functionality we will add to our project.

We also create branches in Git to not get messed up with our code. It is a common practice to work in a group on any project. Many people work their own way. If they start to write code for the same project in the same branch and if one of them writes the wrong code, it may destroy the code structure or the entire project. So, to protect the original (master) branch, the Git branching concept has come into existence.

Some people write code as developers, while others write code for testing, etc. So, it’s best practice to make separate branches according to work styles. And finally, we can “MERGE” these branches with the master (original) branch.

Creating a new branch

When we create a repository, the default branch is called master or main. We can create additional branches :

git branch <branch_name>

Example:

$ git branch test

This just creates the new test branch, which at this point is exactly the same as our master (Original Branch) which means this branch gets property(I.g. : codes) exactly like the master branch.

Checking available branches

$ git branch 
test
* master

Two branches are available, test and master. Master is our default branch and is marked with an asterisk symbol. But if want to work on our newly created branch, we have to switch to this branch.

Switching to another branch

$ git checkout test

Marge Branches

Now, we are in the test branch, we can add new features here and update our code but it won’t affect the master branch.

So, now one question should arise how can the master branch get updated??

Yes, we need to merge our branch with the master branch. After adding new features we can commit and push it to our branch(test) and finally merge it with the master branch.

If new features are successfully added to our branch, we can go back to the master branch.

$ git checkout master

Now marge dev branch with the master branch

$git merge dev

Remove a branch:

The master branch is now up to date. The dev branch is no longer needed and can be removed.

$ git branch -d dev
 

Delete Local Branch:

To delete the local branch we can use one of the following:

$ git branch -d <branch-name>
$ git branch -D <branch-name>

📝Note: The -d option is an alias for –delete, which only deletes the branch if it has already been
fully merged with its upstream branch.
We can also use -D, which is an alias for –delete –force, to delete the branch irrespective of its merged status.

Delete remote Branch:

$ git push --delete    <remote-name> <branch-name>

📝 Note: In most cases, the remote name is “origin”.

Git Branch Summary Table

ConceptCommandExampleExplanation
Create a branchgit branch <branch_name>git branch featureCreates a new branch named “feature”
Switch to a branchgit checkout <branch_name>git checkout featureSwitches to the “feature” branch
Create and switch to a branchgit checkout -b <branch_name>git checkout -b featureCreates a new branch named “feature” and switches to it
List branchesgit branchgit branchLists all branches in the repository
Delete a branchgit branch -d <branch_name>git branch -d featureDeletes the “feature” branch
Merge branchesgit merge <branch_name>git merge featureMerges the “feature” branch into the current branch
Resolve merge conflictsWhen there are conflicting changes in the branches being merged, you must manually resolve the conflicts
Push a branch to remotegit push -u <remote_name> <branch_name>git push -u origin featurePushes the “feature” branch to the “origin” remote repository
Pull changes from remote branchgit pull <remote_name> <branch_name>git pull origin featurePulls the latest changes from the “feature” branch in the “origin” remote repository

This chart summarizes the basic concepts of Git Branch and provides examples for each concept. It can be used as a quick reference guide for beginners who are learning Git.

See code examples in our GitHub repository.

Follow us on social media

Author