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
meager
  • 209,754
  • 38
  • 307
  • 315
Andrew De Andrade
  • 3,506
  • 5
  • 30
  • 35
  • 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
  • 68,917
  • 7
  • 78
  • 118
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.

meager
  • 209,754
  • 38
  • 307
  • 315