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
$ 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.
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.