Delete Git local and remote branches
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.
a. The -d option is an alias for --delete, which deletes the branch only if it has been fully merged in its upstream branch.
This is demonstrated below, which deletes local branch nim that has been fully merged in its upstream branch.

b. The -D option is an alias for --delete --force, which deletes allow deleting the branch irrespective of its merged status.
This is demonstrated below, forcing deleting a local branch vlang that hasn’t been merged in its upstream branch.

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

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.
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.
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.

That’s all about deleting Git local and remote branches.
Also See:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)