Best practice of deleting branches on Github

Sorry but I have to nitpick your post, there are dangerous statements there.

  • It’s true that a branch is only a pointer to a commit.
  • It’s true that deleted branches can be recreated from the last commit in that branch - provided that the commit still exists.
  • When you delete a branch, any commits that were unique to that branch become “unreachable”, and are only accessible by commit hash/id (which can be found in the reflog for 30 days, by default).
  • Unreachable commits (i.e. not tagged or in a branch) more than 30 days old are permanently deleted when git performs garbage collection.
  • Garbage collection can be triggered by common git operations such as fetch and merge when there are too many “loose objects”.
  • So branches cannot “always” be restored at will - only for 30 days, or if the commits exist in ancestor trees of other branches and tags.
  • Deleting commits isn’t complicated. Delete all branches they occur in and they’ll be gone in the next garbage collection after 30 days.
  • A git rebase is often called “rewriting history”, but it doesn’t actually rewrite anything. It creates new commits and makes the old ones unreachable, but you can still find them in the reflog (for 30 days).
5 Likes