How To Delete Git Branch (Locally and Remotely)?

Git branches are an important part of the git. By using branches the codebase can be easily developed by different developers or groups and then merged again. After the development is complete and changes are merged into the main branch other development branches can be deleted. There are two types of branches called local branch and remote branch.

List Local Git Branches

Before deleting branches we generally need to list local branches. The following command is used to list local branches.

$ git branch
 *master
 testing 

The output shows two branches where the master branch is the actively used branch.

List Remote Git Branches

Remote branches can be used similar to the listing local branches. We just provide the -r option to the git branch command like below.

$ git branch -r
origin/HEAD -> origin/master
origin/master

Delete Local Git Branches

We may require to delete a branch after its usage is over. Generally, a few branches are used for a long time and temporary branches are created to developed new features or bug fixes. After the development of the new features or bug fix is complete it is merged into the main branch and the temporary branch is deleted. If the development is done via a local system a local branch is created. The local branch can be deleted with the git branch command by providing the -d option with the branch we want to delete. In the following example, we delete the local branch named testing .

$ git branch -d testing

The output is like below which simply says that the branch removal is completed successfully.

Deleted branch testing (was 51180ec95).

If the branch is not pushed or merged yet it can not be deleted with the -d option. If we try it throws an error. The -D option can be used to force delete operation for the specified branch even it is not merged into a remote branch.

$ git branch -D testing

Delete Remote Git Branches

Remote branches can be deleted by using the git push origin --delete command. The “–delete” option is used to delete the specified remote branches. In the following example, we delete the remote branch named origin/testing .

$ git origin --delete origin/testing

Leave a Comment