Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for loop over all git branches with certain name

Tags:

git

I have the following git branches

foo
bar
foobar
feature/foo
feature/bar
feature/buzz

How would you do a for loop over all branches that start with the word 'feature/' ?

When I try the following, it strangely prints out more than just the git branches.

for i in $(git branch --list "feature/*"); do echo $i; done;
stuff.txt
icon.jpg
morestuff.txt
Vagrantfile
feature/foo
feature/bar
feature/buzz
like image 509
spuder Avatar asked Dec 19 '22 09:12

spuder


1 Answers

"We actively discourage against use of any Porcelain command, including git branch, in scripts" - from the Git Maintainer's Blog

You want to use something like

git for-each-ref --format='%(refname:short)' refs/heads/feature/

which will work fine inside a for br in $() construct

like image 198
Andrew C Avatar answered Jan 06 '23 15:01

Andrew C