mshafrir
mshafrir

Reputation: 5220

When to delete a branch from Git?

I'm relatively new to Git, and want to get advice on best practices for deleting branches.

After I've created and merged a branch back into master, should I leave it hanging around for historical purposes, or should I delete it as soon as it's no longer needed for housekeeping purposes?

Upvotes: 28

Views: 6668

Answers (4)

Olivier Verdier
Olivier Verdier

Reputation: 49146

Typically, you delete a branch after a merge.

For example, after the following merge, you would delete the branch iss53, as you don't need to develop from that branch anymore. You can later recreate it at any moment using the sha1 value of the commit by git checkout -b <name> <sha1>.

(Branches are only necessary when they point to commits that are "tips" of the tree. In fact, in that case, git won't let you remove it, unless you force it to.)

alt text

(the image above comes from the excellent progit book)

Upvotes: 31

devth
devth

Reputation: 2750

Delete topic branches (like "fix-iss05") as soon as you merge them back into your master or development branch. Depending on your workflow, you may want to do all work and merges on a "development" branch, and only merge changes to master after they've been tested and are ready to release.

For a great read on git workflow, check out: http://geewax.org/2009/11/21/agile-git-workflow.html

Upvotes: 3

Jeff Ferland
Jeff Ferland

Reputation: 18292

Nuke it from orbit. You only really have to care when your delete will remove stuff that isn't in the history of your head branch... and even then I do that quite often if I started testing something and decided it was worthless.

Upvotes: 2

ABach
ABach

Reputation: 3738

As I see it, there's really no need to keep it around. Unless you --squash the merge, you will have that branch's history in master. I'd go ahead and delete ones you no longer need.

Upvotes: 2

Related Questions