Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How delete a local branch after merging it through a pull request? git-flow, bitbucket, sourcetree

Tags:

My environment in local:

  • Git connecting with Bitbucket
  • Sourcetree with git-flow

Steps

  1. I create a feature x in Sourcetree then I add the code.
  2. I do a commit including the option to create a pull request and push the changes to feature x branch in remote.
  3. In pull request I select the option for Close {branch} after the pull request is merged.
  4. Within Bitbucket a member team approves the pull request then merges it into the develop branch.
  5. I pull the new changes to my local develop branch.

NOTE It's important to review the code through pull request before merging it into develop, so the question is:

Is there any way to delete automatically the feature x branch in my local after made a pull in develop?

*I tried with a fetch but it does not work.

like image 481
e-israel Avatar asked Jan 23 '20 19:01

e-israel


People also ask

Should I delete a branch after merging pull request?

The rule of thumb that we use (which is here some where on Stack Overflow) is "branches are for work, tags are for history". Whenever a branch is merged (most likely into master) we tag the merge point using the name of the branch with the prefix "branch" (e.g. branch-topic). Then delete the branch.


1 Answers

One simple, non-automated way to handle this is to periodically run a branch cleaning command in your local repo, like

# to be executed with your "main" stable branch checked out
git branch -d $(git branch --merged)

It'll delete every already merged local branch (i.e. those which do NOT have "not yet merged" commits). So all these branches which have been merged through pull requests will be deleted, but not the few ones that have recent (not reviewed/not merged) commits.

Note : if your policy is to squash commits upon pull request, this won't be a suitable solution, since your local branches still have the original (pre-squash) commits, so they won't be viewed as merged and won't be deleted.

like image 69
Romain Valeri Avatar answered Sep 28 '22 10:09

Romain Valeri