Git Branch
Branching is an essential feature that is available in most version control systems. In Git, branches are used in the everyday development workflow. Git branches are just a pointer to a snapshot of the changes you have made. Every time you want to add a new feature or fix a bug, you create a new branch to encapsulate the changes. Using branch allows to clean the feature's history before merging it, so it will be difficult for unstable code to get merged into the main code base.
How git branch works
Git stores branches as a reference to a commit compared to other version control systems that for creating a branch, they copy files from directory to directory.
A branch on Git represents an isolated line of development. Branches work as an abstraction for the edit, stage, commit workflow. Creating a new branch is like a request to have a new working directory, staging area, and commit history. New commits are saved in the history of the current branch.
The git branch
command is used to list, create, rename, and delete branches. However, it can not switch between branches or clean the commit history. This is why git branch
is used with git checkout and git mege commands.
Developing using branches allow working in a parallel isolated line of development. And also, it will protect the main code base from questionable code. The following illustration shows a repository with two isolated lines of development (Featute_A, Feature_B).

Common options and usages
The git branch
has many options. The following list shows different use cases:
git branch
: It will list all the branches of the repository. It is equivalent togit branch --list
.$ git branch
git branch <branch_name>
: It will create a branch called <branch_name>, but it will not check out to the new branch.$ git branch <branch_name>
git branch -d <branch_name>
: It will delete the specified branch . If there are unmerged changes, Git will prevent you from deleting the branch.$ git branch -d <branch_name>
git branch -D <branch_name>
: It will force delete the specified branch even if it contains unmerged changes. Be sure before executing this command because it will permanently remove all the commits associated with the specified branch.$ git branch -D <branch_name>
git branch -m <branch_name>
: It will rename the current branch to <branch_name>$ git branch -m <branch_name>
git branch -a
: It will list all the remote branches.$ git branch -a
Creating branches
It's crucial to know that branches are just pointers to commits. When you create a new branch, Git will just create a new pointer. Git will not change anything in the repository.
Let us start with a repository that looks like the following:

You can create a new branch using the git branch
command as following:
$ git branch demo_branch
After running the above command, nothing will change in the repository. Git will just create a new pointer to the current commit and nothing else.

As shown in the illustration above, the git branch
will just create a new pointer to the current commit. To start working in the new branch, first, you need to switch to the new branch using the git checkout command, and then use git add and git commit.
Creating remote branches
The git branch
command operates on local branches and also on remote branches. To operate on remote branches, a remote repo must be configured as follows :
# Create a new demo branch
$ git branch demo_branch
# Add a remote repo to the local repo config
$ git remote add new_demo_remote_repo
# pushs the demo_branch to new_demo_remote_repo
git push new_demo_remote_repo demo_branch
The command above will push a copy of the local branch demo_branch to the remote repository.
Deleting branches
After finishing working on a branch and merging it into the main code base, you can delete the branch without losing history.
$ git branch -d demo_branch
If the branch hasn't been merged, the delete will not continue. Git tries to protect you from losing access to this branch, and it will output an error:
error: The branch 'demo_branch ' is not fully merged.
If you are sure you want to delete it, run 'git branch -D demo_branch'.
If you are sure that you want to permanently delete the specified branch, you can use the git branch
command with the -D
option.
$ git branch -D demo_branch
The above command will delete just the local copy of the branch. If you want to delete the remote branch, run the following command.
$ git push origin --delete demo_branch
Or
$ git push origin :demo_branch