Deleting branches in Git
Creating a new branch for everything you are working on will lead to a lot of branches in your repository. These stale branches will mess up your repository and make it harder to find a specific branch. Therefore, you should know how to delete branches locally and remotely.
Deleting a local branch
After finishing a task and merging it into your development or master branch, you may want to delete your local branch for this task. You can delete a local branch by using the branch command with the -d flag:
git branch -d featureXYZ
To delete a local branch, you should have switched to another branch beforehand. Also, for using the -d flag, the branch must be fully merged in its upstream branch.
If this is not given and you still want to delete a local branch, you can force the deletion with the -D flag:
git branch -D featureXYZ
Deleting a remote branch
For deleting a remote branch, you won't need the branch command but the push command instead. This might be surprising. In case that we wanted to delete a remote branch called "featureXYZ", the command would look as follows:
git push -d origin featureXYZ
git push origin :featureXYZ
You may specify multiple branches for deletion.