How To Create and Manage New Branch with Git? – POFTUT

How To Create and Manage New Branch with Git?


Git branch mechanism is an important part of the code versioning. We can create new branches in order to make the same source available multiple development paths. In this tutorial, we will learn how to create and manage a new branch with Git.

List Branches

We will start by listing the current existing branches. We will use git branch command without any option or parameter. The branch which prefixed with * is the current working or active branch.

$ git branch
List Branches
List Branches

Create Local Branch  and Switch This Branch

We can create a local branch with different commands branch and checkout. We will use branch command and provide the branch name test in this example.

$ git branch test

We will use checkout command with -b option and branch name. This will also automatically change the current branch to the newly created branch. In this example, we will create a branch named silver.

$ git checkout -b silver

We can see the message Switched to a new branch 'silver'.

Change Working Branch

If we just want to change the current working or active branch we can use the checkout command. We will also provide the branch name we want to change. In this example, we will change the branch newversion.

$ git checkout newversion

Add New Remote Repository For Current Branch

Git is mainly designed to be used in a distributed manner. So there will be a lot of remote Git repositories and branches. We can add a new remote branch for the current branch with the remote add command. In this example, we will add remote system github.com new branch named test

$ git remote add github.com/ibaydan test

Update Branch From Remote Repository

Git remote branches can be updates frequently where our local branch may be outdated. We can update our local branch from the remote repository branch with the fetch command like below. We will update the local branch from remote named github.com/ibaydan

$ git fetch github.com/ibaydan

Delete Local Branch

After completing changes and merged to the main branch we may need to delete the local branch. we will use branch command with -d option. -d means delete. We will delete the local branch named silver in this example.

$ git branch -d silver

Force Delete Local Branch

If there are some problems for normal removal or deletion of the local branch we can force delete operation. We will use -D option for forcing deletion. In this example we will delete branch named silver

$ git branch -D silver

LEARN MORE  What Is SVG (Scalable Vector Graphics) File?

Leave a Comment