Branches can (if the project is somewhat complex) sometimes just pile one onto another, and can make the process of navigating the project and checkouting to different branches harder. Especially if a version control management software is used. Such management software can auto-assign task-numbers to tasks, and then branches could end up having similar names (6446-task1, 6464-task2, 6466-task3, etc…).
Additionally, once you’ve finished working on a branch most of the time you don’t need it anymore!
So, you finished your job, and you just want to get rid of excess branches, both locally and remotely. (we’ll see that one is a bit more dangerous than the other :P). So the question is – how to delete GIT branches?
Deleting GIT branches locally
git branch -d branch_name git branch -D branch_name
Using any of the above mention commands we can delete branches. The only difference is in the argument (lowercase -d vs. uppercase -D). Before deleting any branches make sure you moved to another branch that you’re not deleting.
The lowercase -d argument will delete the local branch ONLY IF the content on that branch is pushed and merged with remote branches.
The uppercase -D argument will forcefully delete local branches, not taking into account whether the contents are pushed or merged. This is DANGEROUS.
git branch -d 6446-task1
We just deleted 6446-task1 branch locally.
Deleting GIT branches remotely
git push <remote> --delete <branch> git push <remote> :<branch>
These two commands do the same thing, the only difference is that the second has a shorter syntax.
With this command we need to set up the remote repository that the project was cloned from (most often origin), the argument –delete and the name of the branch itself.
git push origin --delete 6446-task1
We just deleted 6446-task1 branch remotely.