3

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
spuder
  • 14,200
  • 14
  • 77
  • 129
  • Possible duplicate of [How to iterate through all git branches using bash script](http://stackoverflow.com/questions/3846380/how-to-iterate-through-all-git-branches-using-bash-script) – pihentagy Aug 24 '16 at 12:19

1 Answers1

10

"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

Andrew C
  • 11,594
  • 4
  • 44
  • 52