anon
anon

Reputation:

Automatically delete git branch after merge to master

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.

Upvotes: 65

Views: 73083

Answers (7)

DevThiman
DevThiman

Reputation: 1138

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

Upvotes: 2

slisnychyi
slisnychyi

Reputation: 1980

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

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

Upvotes: 9

Sameera De Silva
Sameera De Silva

Reputation: 1980

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

Upvotes: 3

Karan Bansal
Karan Bansal

Reputation: 1484

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.

Upvotes: 126

Max Desiatov
Max Desiatov

Reputation: 5565

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.

Upvotes: 3

Jsilvermist
Jsilvermist

Reputation: 501

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.

Upvotes: 3

Octavian Helm
Octavian Helm

Reputation: 39604

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.

Upvotes: 3

Related Questions