How To Delete/Remove Git Branch Local and Remote? Tutorial with Examples – POFTUT

How To Delete/Remove Git Branch Local and Remote? Tutorial with Examples

We work hard to create and add new features to our software by working with new Git branches. But after some time it can be quite a local or remote branch hell. We can remove or delete local and remote branches in different ways.

Switch To Another Branch

Before deleting a local branch we should change to the other branch. Generally, with a working branch we may be using the local branch we want to delete. So as a first step we will switch to the new branch so we can delete the local branch.

We will print existing branches and currently active branch with like below.

Then we are currently on the branch named newversion and in order to delete this local branch, we will change to branch master. We will use checkout command and provide the branch name.

Switch To Another Branch

Switch To Another Branch

Delete Local Git Branch

Now we can delete or remove our branch with the option -d or --delete. -d is the short version of the --delete. We will also specify the local branch we want to delete. In this example, we will delete the branch named newversion.

Delete Local Git Branch

Delete Local Git Branch

Force To Delete Local Git Branch with Unmerged Changes

If there are some minor problems like unmerged changes and the delete operation should be forced we can use -D or  --delete --force. Actually -D is an alias for the --delete --force. In this example, we will forcibly delete the branch named com_support.

Force To Delete Local Git Branch with Unmerged Changes

Force To Delete Local Git Branch with Unmerged Changes

Delete All Local Branchs Not Presents In The Remote Repository

While working in a big project we can stick to the remote branches. In some cases, we may need to remove/delete local branches that do not exists on the remote repository branches. We will use following command which will delete/remove all local branches that do not exist in the remote repository branches.

LEARN MORE  How To Clone Git Branch Command Tutorial with Examples?

Delete All Local Branches Except Master

We can also use the following command in order to delete all local branches except the master branch. This will clean our home easily without much effort.

Delete All Local Branches Except Master

Delete All Local Branches Except Master

Delete Remote Git Branch

We can also delete remove branches with the push command. Because deleting a local branch won’t delete/remove the remote branch. In order to delete/remove branch we will also provide the remote repo name with the branch name we want to delete. We provide the repository name as git may work in a distributed manner where it can have multiple repositories. But generally, the remote repository will be origin. In this example, we will delete the branch named test from the remote repository named origin.

OR

Synchronize Branch List with The Remote Repository and Branches At The End

At the end of the local or remote branches, removal/deletion synchronizing is a best practice. We will use the fetch git command with the -p option which will remove any remove-tracking references that no longer exist.

Synchronize Branch List with The Remote Repository and Branches At The End

Synchronize Branch List with The Remote Repository and Branches At The End

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)