6

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
1

1 Answer 1

12

"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

Sign up to request clarification or add additional context in comments.

1 Comment

I'll add that the reason git branch doesn't work is because the output includes a * next to the current branch. If you echo * or for f in *; do echo $f; done you get the contents of the current directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.