How to delete local and remote branches in Git?

Branching is a feature available in most version control systems. Branches represent an
independent line of development. It provides a way to isolate changes feature or bug
fix from your main branch. Committing changes to the particular branch won't affect the
other branch.

It is common practise to delete the git branches once they no longer used but this is
not the necessary one. In this blog, I will show you, how to delete the git branches.

Local and Remote branches in git are completely separate objects in Git. So deleting 
the local branch wouldn't delete the remote branch and vice-versa.

1. Deleting the local branch

Let's take a scenario you worked on the branch named "feature/add-to-cart" and merged
into the main branch. Once it is merged, you need to delete the local branch
"feature/add-to-cart". Here is the command to delete the local branch in git,
    git branch -d feature/add-to-cart

syntax: git branch -d <branch-name>
-d tells git to delete the branch

2. Deleting the remote branch
You can delete the remote branch by executing,
    git push origin --delete feature/add-to-cart

syntax: git push origin --delete <branch-name>

Comments