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

How To Delete/Remove Git Branch Local and Remote?


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.

$ git branch

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.

$ git checkout master
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.

$ git branch -d 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.

 $ git branch -D 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 exist on the remote repository branches. We will use the following command which will delete/remove all local branches that do not exist in the remote repository branches.

$ git branch -vv | grep ': gone]'| grep -v "\*" | \ 
      awk '{ print $1; }' | xargs -r git branch -d

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.

$ git branch | grep -v "master" | xargs git branch -D
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 the 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.

$ git push origin --delete test

OR

$ git push origin -D test

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.

$ git fetch -p
Synchronize Branch List with The Remote Repository and Branches At The End
Synchronize Branch List with The Remote Repository and Branches At The End

LEARN MORE  How To Create and Manage New Branch with Git?

Leave a Comment