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
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.