27

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?

mshafrir
  • 5,190
  • 12
  • 43
  • 56
  • Similar question: [What to do with experimental non-merged git branches?](http://stackoverflow.com/questions/8988240/what-to-do-with-experimental-non-merged-git-branches/8989747#8989747) – sleske Jun 21 '12 at 09:04

4 Answers4

30

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)

michiel s
  • 3
  • 2
Olivier Verdier
  • 46,998
  • 29
  • 98
  • 90
3

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

devth
  • 2,738
  • 4
  • 31
  • 51
2

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.

ABach
  • 3,743
  • 5
  • 25
  • 33
  • are you saying that, if we squash commits, and then delete the old branch, we will not be able to recreate it with the SHA1 ? git checkout -b – zonabi Mar 25 '15 at 16:13
2

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.

Jeff Ferland
  • 17,832
  • 7
  • 46
  • 76