Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all local branches that have been squashed and merged

Tags:

git

github

I am working on a fork of a repository. I send pull requests from branches of that fork and they get "Squash and Merged" into the master branch of the upstream repository once they are accepted. How can I automatically find and delete local branches that have already been squashed and merged? Most strategies shown in other solutions rely on determining whether all commits in a branch can be found in master's commit history, but since all my commits are squashed this condition is never met.

More Info

My git remote -v looks like:

origin  [email protected]:sshleifer/transformers_fork.git (fetch)
origin  [email protected]:sshleifer/transformers_fork.git (push)
upstream    [email protected]:huggingface/transformers.git (fetch)
upstream    [email protected]:huggingface/transformers.git (push)
like image 614
Sam Shleifer Avatar asked Nov 15 '25 23:11

Sam Shleifer


1 Answers

Through git alone : the short answer is you can't (not with 100% reliability).

Here are some unreliable ways to explore :

  • if the commit messages on master contain the branch name : grep it out ;
  • if the commit messages on master contain a PR name : grep it out, then use Github's api to find what branch is linked to this PR ;
  • if the commit messages on master contain an issue number : grep it out, and match it with your own rule (issue number in branch name ? fix #xyz in some commit message ?) ;
  • if the branches are always merged soon enough, or rebased on master before being squashed + merged, you can try to look if the content (the ^{tree}) in your branch matches the content of a commit on master :
    • git log --first-parent --format="%T" will give you the list of trees on master,
    • git rev-parse branch/name^{tree} will give you the tree for branch branch/name
like image 160
LeGEC Avatar answered Nov 17 '25 22:11

LeGEC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!