Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Multiple remotes setup, how to delete merged branches only from single remote?

I have a repo with many remotes setup because we are using the Forking Workflow

I typically clean all the merged branches from the origin repo every quarter. I’m having difficulty figuring out how to clean the merged branches from the individual remotes.

git branch -r --merged <how can i target a specific remote?> |
  grep -v "develop" |
  grep -v "master" |
  grep -v "pr" |
  sed -r 's/(remote1|remote2|remote3|remote4)\///' |
  xargs git push -d origin

The idea here is to list all the merged branches, filter out develop, master, pr and remove origin/, then git push -d

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

the problem I’m having is I don’t know how to target only a specific remote for deletion. Each time I run this command, it lists all the branches from each remote. Then running the command outputs an error

error: unable to delete '<branch-name>': remote ref does not exist

>Solution :

Since the output of git branch -r --merged includes the remote name, you can use grep (etc) to filter by remote. E.g. to see only those branches in the remote1 remote:

git branch -r --merged | grep remote1/

Putting that together with what you have — and simplifying things a bit — we get:

git branch -r --merged |
  grep remote1/ |
  grep -vE 'develop|master|pr' |
  sed 's/[^/]*\///' |
  xargs git push -d remote1

If you’re getting errors, replace xargs with echo xargs so you can see the generated command line without running it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading