Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to delete a branch from Git?

Tags:

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?

like image 543
mshafrir Avatar asked May 11 '10 18:05

mshafrir


People also ask

When should git branches be deleted?

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.

Is deleting branch is good practice?

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.

What does deleting a git branch do?

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.

Should you delete a branch after merging?

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.


1 Answers

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)

like image 189
Olivier Verdier Avatar answered Nov 24 '22 01:11

Olivier Verdier