Deleting multiple remote branches with a pattern

If you want to delete multiple remote branches following a name pattern, this article will help you!

First, list the branches that you want to delete:

git branch -r | grep "feature\/.*"

The "feature\/.*" is a regex. You can replace it using a regex to match your branches.

Now, let’s write these branches into a file:

git branch -r | grep "feature\/.*" | awk -Forigin/ '{print $2}' > branches.txt

The command awk -Forigin/ '{print $2}' removes the origin/ from the front of each branch name. It will be used the delete command;

You can review the branches created in the branches.txt file, check if you want to keep some branches, and remove them from the list.

cat branches.txt | xargs -I {} git push origin :{}

This command will delete each branch from your branches.txt file.

That’s it. I hope it helped you.