Issue
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
Escape the Pipe in your pattern \|
. Like that:
git branch | grep -v "branchName1\|branchName2\|branchName3" | xargs git branch -D
git branch | grep -v -E "branchName1|branchName2|branchName3" | xargs git branch -D
Short explanation of the parameters
-v or --invert-match
The -v parameter returns all non-matching lines, which is important because you want to remove them.
-E or --extended-regexp
Means that the pattern which you pass is extended regex which is used for the search. Then you can use the pipe to separate without escaping.
Answered By - KrassVerpeilt Answer Checked By - Willingham (WPSolving Volunteer)