Should I delete a branch after merging it?

GitBranching and-Merging

Git Problem Overview

After merging a branch, do you delete it from the repository?
Is it a good practice or not?

I usually create a lot of branches, since I don't want to break my current release, and I'd wish to delete them to keep things in order.
However, if you work with Assembla or GitHub, your merge requests from old branches will be saved on the site, so if you delete them you'll get an error since it won't be able to fetch them...

Usually how do manage that?

Git Solutions

Solution 1 - Git

There's no problem in deleting branches that have been merged in. All the commits are still available in the history, and even in the GitHub interface, they will still show up (see, e.g., this PR which refers to a fork that I've deleted after the PR got accepted).

Solution 2 - Git

I definitely clean up my branches after they've been merged in.

We use GitLab and merge requests at work, so the historical information about branches is stored there; I don't need them cluttering my branch list, and when I look at a coworker's fork, ideally I'd like only to see the branches of their current active development. If I'm trying to look at some code on their branch, I want to be able to look through just a few currently active branches, and not every feature or fix they've ever started work on.

The above applies to BitBucket and GitHub, too.

The only reason you might have for not deleting a branch post-merge is so you know where a given feature ended, but merge commits (and git merge --no-ff if you really want) make that irrelevant.

Solution 3 - Git

Just take care of
All hyperlinks URLs references of your DELETED branches, will be BROKEN.

For example
If you delete branch_feature_x branch from your repo
The corresponding hyperlink URL of this branch will be broken

Solution 4 - Git

Just to clarify, branch, from git point of view, is just link onto some commit. By deleting branch, you will not delete commits from git repo. Of course, detached commits will be cleaned after some time via git garbage collector.

FYI: We're usually merging branches into master via bitbucket interface. There you can set delete feature branch after merge flag.

If you need to deal with too old branches, you could have a look for some utilities, for example this one .


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiontampe125View Question on Stackoverflow
Solution 1 - GitFred FooView Answer on Stackoverflow
Solution 2 - GitAsherahView Answer on Stackoverflow
Solution 3 - Gitahmednabil88View Answer on Stackoverflow
Solution 4 - GitAGrigoriiView Answer on Stackoverflow