61

We will be attempting a work flow in github where every ticket is a branch off of master.
After the ticket is complete, the work is merged into staging where regression and integration tests are performed before it is merged into master.

A team lead brought up the issue of the old ticket branches after a merge will start to build up.

I found this script and want to know if this would work in our environment. We only want to delete branches that have been merged into master.

4
  • GitHub has a button for deleting branches that were merged into the branch they were submitted to, but I guess that doesn't help you much.
    – Fred Foo
    Commented Jan 23, 2013 at 17:54
  • Yea, we want to have that process automated. The link shows how to do it with shell, but we don't get access to github shell, so no way to chronjob it, afaik
    – anon
    Commented Jan 23, 2013 at 21:40
  • So you are actually looking for git hooks, more specifically for the post-merge hook. If that's the case then it's not going to work I'm afraid. Commented Jan 23, 2013 at 21:56
  • 1
    You can install this GitHub app that removes a branch after the PR gets merged. github.com/apps/delete-merged-branch Commented Sep 19, 2018 at 7:22

7 Answers 7

124

Github has released a feature where anyone with admin permission to the repository can configure branches to get deleted automatically after pull requests are merged. Here are the steps -

  1. Navigate to main page of the repository and click on Settings.
  2. Under "Merge button", you can select or unselect "Automatically delete head branches" option.

This feature has been released by Github on July 31, 2019.

5
  • 10
    What is unclear in the documentation is whether branches in forks (owned by the contributor user) are also auto-deleted after the PR is merged.
    – Tanz87
    Commented Oct 18, 2019 at 0:07
  • 1
    @ton-yeung, please mark this answer as accepted as the currently accepted answer no longer holds true and can be misleading. Commented Jan 1, 2020 at 16:09
  • 2
    IMO this should have been a user/org-level setting instead of a repository-level setting.
    – Tanz87
    Commented Jan 1, 2020 at 16:13
  • 3
    @Tanz87, I agree but I think having it based on repository is better because it gives more granular control. user/org-level setting in addition would have been better definitely. Commented Jan 1, 2020 at 16:15
  • 3
    @Tanz87 @Karan_Bansal IMO This should really be a branch setting. The use case is deleting after PR's are merged to develop, but keeping a hotfix/* branch after you merge to master so you can easily merge the same one into develop.
    – genkilabs
    Commented Sep 15, 2021 at 17:48
5

After pull requests are merged, you can have head branches deleted automatically in GitHub:

repository -> settings -> (general) pull requests -> check Automatically delete head branches

3

There's no ready-to-use script for your use case as far as I know. You'll have to create your own tools for that.

There is a tool called git-flow by Vincent Driessen which was built to assist developers following his git workflow described in "A successful Git branching model".

It's is not as easy as just deleting the branch after merge because you never know if you'll run into a merge conflict or not.

4
  • The link I added is a ready to use script, but I don't think I can use it with github.
    – anon
    Commented Jan 23, 2013 at 21:40
  • GitHub has nothing to do with it. It is just a service hosting your repositories (and a bit more). Commented Jan 23, 2013 at 21:51
  • 1
    hub flow(datasift.github.com/gitflow) is adapted from gitflow and works great.
    – anon
    Commented Feb 22, 2013 at 4:47
  • is there a way to check the box "Automatically delete head branches" on github ?
    – soung
    Commented May 13, 2022 at 15:31
3

Add either of the following to your .gitconfig file for an easy alias to merge and delete a branch with 1 command.

Alias as a function:

[alias]
  ff = "!f() { git merge $1; git branch -d $1; }; f"

Alias as a new shell command:

[alias]
  ff = !sh -c 'git merge $1 && git branch -d $1' --

They both do the exact same thing, just 2 different methods of doing it.

3

As far as I know, the best option currently is a GitHub app called delete-merged-branch. It can be easily integrated into a selected repository as an existing app installation, but its source code is also available. This app will automatically delete branches after they have been merged through a PR.

3

There is an option in Github UI where repository administrator can configure.

Under "Merge button", select or unselect Automatically delete head branches.

https://docs.github.com/en/free-pro-team@latest/github/administering-a-repository/managing-the-automatic-deletion-of-branches

0

Extending @silisnychyi answer with a picture guide (In latest UI 2024).

At the moment, this can be setup at the repository settings as below.

Repository >> Settings >> General >> Pull requests -> Tick Automatically delete head branches

According to the docs these deleted branches will still be able to be restored.

enter image description here enter image description here