How to delete a git branch on both remotely and locally

In this tutorial, we will learn how to delete a git branch locally and remotely. 

When and why do we need to delete a branch?

Whenever we work on git. It is good practice to have a master branch or stage branch for the production and dev branch for the development. while working on some modules, new features or fixes we create a new isolate branch to work on that feature and when we are done with the feature branch we finally merge and delete that branch. 

Delete git branch Locally

Below are the commands that we can use to delete the local GIT branch.

git branch -d <branch-name>
git branch -D <branch-name>

The difference in both commands is that one with a small letter d and one with a capital D letter.

The -d option is for --delete that will delete your local branch only if you have already merged and pushed it with your remote branches.

The -D option is for --delete --force that will delete the branch even if you have not pushed or merge your local branch with remote branches. Be careful with this command.

Delete git branch Remotely

To delete a branch remotely use the below command

git push <remote> --delete <your-branch-name>

For example:

git push origin --delete loginModule

You can also use git shorter command to delete a branch remotely: git push <remote> :<branch>

If you want to learn more about git. You can download Pro Git book.