Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 461
ton.yeung Avatar asked Jan 23 '13 17:01

ton.yeung


People also ask

When a branch is merged is it automatically deleted?

Automatically delete merged branchesGithub branches can be automatically deleted after press on the merge button. You need to be a repository admin: Navigate to repository Settings.

Should you always delete branch after merge?

When you're done with a branch and it has been merged into master, delete it. A new branch can be made off of the most recent commit on the master branch. Also, while it is ok to hang onto branches after you've merged them into the master they will begin to pile up.


3 Answers

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.

like image 115
Karan Bansal Avatar answered Sep 29 '22 11:09

Karan Bansal


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.

like image 45
Octavian A. Damiean Avatar answered Sep 29 '22 12:09

Octavian A. Damiean


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.

like image 42
Jsilvermist Avatar answered Sep 29 '22 12:09

Jsilvermist