0

When I execute git branch on the command line I get a list of all the branches on a repo, however when I execute $(git branch) in a sub-shell, it first prints out a list of files in the top level folder in a repo before printing out the branch names. Why?

I'm basically trying to iterate over the branches using a for loop, but the listing of files breaks my script.

for i in $(git branch); do 
    echo $i
done
user229044
  • 232,980
  • 40
  • 330
  • 338
Andrew De Andrade
  • 3,606
  • 5
  • 32
  • 37
  • I learned that this is the better solution to this problem: http://stackoverflow.com/questions/3846380/how-to-iterate-through-all-git-branches-using-bash-script – Andrew De Andrade Jul 17 '14 at 21:36

2 Answers2

3
$ git branch
* master

Try echo * master in your shell and see what you get?

Hint: You'll get the list of files in the current directory from the shell glob expansion of *.

See DontReadLinesWithFor for more details.

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
2

Because your first branch (or, the checked out branch) has a * next to it, which is a shell wild card. This is being evaluated and expanded to the list of files in the current directory.

Try checking out a branch further down the list, you'll find the list of files moves to be inserted in the list next to the currently checked out branch, not at the top.

user229044
  • 232,980
  • 40
  • 330
  • 338