When to delete branches in Git?

Anthony Compton picture Anthony Compton · Mar 16, 2011 · Viewed 73.7k times · Source

Suppose we have an application that's stable.

Tomorrow, someone reports a big ol' bug that we decide to hotfix right away. So we create a branch for that hotfix off of "master", we name it "2011_Hotfix", and we push it up so that all of the developers can collaborate on fixing it.

We fix the bug, and merge "2011_Hotfix" into "master" as well as into the current development branch. And push "master."

What do we do with "2011_Hotfix" now? Should it just sit out there as a branch forever until the end of time or should we now delete it, since it has served its purpose? It seems unclean to just leave branches lying around everywhere, as the list of branches will likely become very long, most of which aren't even necessary anymore.

In the event that it should be deleted, what will happen to its history? Will that be maintained, even though the actual branch is no longer available? Also, how would I remove a remote branch?

Answer

Artefact2 picture Artefact2 · Mar 16, 2011

You can safely remove a branch with git branch -d yourbranch. If it contains unmerged changes (ie, you would lose commits by deleting the branch), git will tell you and won't delete it.

So, deleting a merged branch is cheap and won't make you lose any history.

To delete a remote branch, use git push origin :mybranch, assuming your remote name is origin and the remote branch you want do delete is named mybranch.