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?
They're unnecessary. In most cases, branches, especially branches that were related to a pull request that has since been accepted, serve no purpose. They're clutter. They don't add any significant technical overhead, but they make it more difficult for humans to work with lists of branches in the repository.
That means you no longer need to keep and use that branch, so it is a common best practice to delete it so it doesn't clutter up your code.
In Git, branches are just pointers (references) to commits in a directed acyclic graph (DAG) of commits. This means that deleting a branch removes only references to commits, which might make some commits in the DAG unreachable, thus invisible.
When you're done with a branch and it has been merged into master, delete it. A new branch can be made off of the most recent commit on the master branch. Also, while it is ok to hang onto branches after you've merged them into the master they will begin to pile up.
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.)
(the image above comes from the excellent progit book)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With