Automatically delete git branch after merge to master
Asked Answered
S

7

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.

Sunfast answered 23/1, 2013 at 17:40 Comment(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.Eliseo
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, afaikSunfast
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.Nucleate
You can install this GitHub app that removes a branch after the PR gets merged. github.com/apps/delete-merged-branchFlynn
N
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.

Nucleate answered 23/1, 2013 at 17:52 Comment(4)
The link I added is a ready to use script, but I don't think I can use it with github.Sunfast
GitHub has nothing to do with it. It is just a service hosting your repositories (and a bit more).Nucleate
hub flow(datasift.github.com/gitflow) is adapted from gitflow and works great.Sunfast
is there a way to check the box "Automatically delete head branches" on github ?Stromberg
P
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.

Pastis answered 2/8, 2019 at 14:12 Comment(5)
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.Catlaina
@ton-yeung, please mark this answer as accepted as the currently accepted answer no longer holds true and can be misleading.Pastis
IMO this should have been a user/org-level setting instead of a repository-level setting.Catlaina
@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.Pastis
@Catlaina @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.Karie
P
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

Parkin answered 2/12, 2022 at 9:18 Comment(0)
N
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.

Nucleate answered 23/1, 2013 at 17:52 Comment(4)
The link I added is a ready to use script, but I don't think I can use it with github.Sunfast
GitHub has nothing to do with it. It is just a service hosting your repositories (and a bit more).Nucleate
hub flow(datasift.github.com/gitflow) is adapted from gitflow and works great.Sunfast
is there a way to check the box "Automatically delete head branches" on github ?Stromberg
D
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.

Delate answered 3/5, 2016 at 12:33 Comment(0)
S
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.

Stodgy answered 11/11, 2018 at 12:15 Comment(0)
E
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

Elijah answered 16/12, 2020 at 8:9 Comment(0)
C
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

Corrigible answered 10/5 at 22:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.