'How to remove branches except for development and main

Goal Delete all branches except for several exceptions. In my case: development, main

I found this command but it only applies to main.

git branch | grep -v "main" | xargs git branch -D

I thought you could do it like this:

git branch | grep -v "main|development" | xargs git branch -D

But that doesn't work.



Solution 1:[1]

Basic regular expressions require you to escape the |, which is otherwise treated as a literal character, to indicate alternation.

git branch | grep -v "main\|development"

If your version of grep supports it, you can use extended regular expression, in which | is the alternation operator.

git branch | grep -Ev "main|development"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 chepner