This post will discuss how to delete local and remote branches in git using the git-branch command.

1. Delete the local branches

To delete the local branch, we can use the git-branch command with the -d or -D option.

git branch (-d | -D) <branchname>

a. The -d option is an alias for --delete, which deletes the branch only if it has been fully merged in its upstream branch.

git branch -d <branchname>

This is demonstrated below, which deletes local branch nim that has been fully merged in its upstream branch.

Delete a local branch

 
b. The -D option is an alias for --delete --force, which deletes allow deleting the branch irrespective of its merged status.

git branch -D <branchname> # -D is same as -d -f

This is demonstrated below, forcing deleting a local branch vlang that hasn’t been merged in its upstream branch.

Force delete local branch

2. Delete the remote branches

To delete remote branches using the git-branch command, specify the -r option together with the -d option.

git branch -d -r <branchname>

remove remote branch using git-branch

 
Note that the git-branch command can accept multiple branches for deletion.

 
It only makes sense to delete remote branches if they no longer exist in the remote repository or if git fetch is configured not to fetch them. You can use the prune subcommand of git-remote for cleaning obsolete remote branches.

git remote prune origin

Alternatively, use the get-fetch command with the --all option to fetch changes from all remotes and supply -p option to remove any remote references that no longer exist on the remote.

git fetch --all -p

How to list all local/remote branches?

To get a list of both remote and local branches, we can use the -a option. To get only the remote branches, use the -r option.

list all git branches

That’s all about deleting Git local and remote branches.

 
Also See:

Delete remote-tracking branches in Git