156

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?

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
tampe125
  • 8,231
  • 7
  • 30
  • 45

4 Answers4

114

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).

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • You said that all commits are still available in the history. If I view a project on github.com I find that this is true. However, in the Github desktop app for the Mac, it seems that you can no longer see the commit history for the merged branch. Am I mistaken? – peacetype Dec 09 '15 at 23:57
  • I'd add that if you are not using a git client, especially not using one with a gui, then having the branches can be helpful for understanding your log well. That is, since you don't have a github/gitlab/other gui to look at, keeping the branch names allows you to have a simple place to reference history in addition to commit history--which is otherwise lost by deleting the branch. Someone please let me know if this last statement is wrong. – Raj Oct 26 '16 at 01:24
  • 2
    @Raj you have that already, in the form of `Merge branch fix-foo-bar` commit messages. Try `git log --grep="Merge branch"`, then drop your own anchors of interest via `git checkout -b curious-change`. Also, nothing is lost when deleting a branch -- except the mere "branchname → commithash" pointer (which is what a branch really is, doesn't matter local or remote). – ulidtko May 13 '19 at 13:54
  • 9
    @fred-foo The question if it's a good practice is not answered though. (I have the same question) – Esger Sep 02 '20 at 13:41
41

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.

Asherah
  • 18,948
  • 5
  • 53
  • 72
  • 4
    Apparently [GitHub always does --no-ff](http://ariya.ofilabs.com/2013/09/fast-forward-git-merge.html) so we won't lose the fact that this was a branch even in those situations. – joeytwiddle Aug 08 '14 at 17:33
  • 7
    @joeytwiddle: assuming you're using GitHub's own interface to merge the branch, yes! – Asherah Aug 09 '14 at 01:56
9

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
https://github.com/username/project/tree/branch_feature_x

Ahmed Nabil
  • 17,392
  • 11
  • 61
  • 88
3

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 might be helpful.

cybersam
  • 63,203
  • 6
  • 53
  • 76
AGrigorii
  • 142
  • 6
  • 3
    Please be sure to review [Stack Overflow’s self-promotion rules](https://stackoverflow.com/help/promotion). At minimum, you need to disclose your affiliation with the utility you’re suggesting. – Jeremy Caney Dec 09 '20 at 18:24