When $(git branch) is expanded for the for loop to loop over it, it expands to the multi-line string
branch1
* branch2
master
Since the command substitution is unquoted, this is then split on spaces, tabs and newlines (by default) into the four words
branch1 * branch2 master
Each word then undergoes filename generation (globbing). The second word, *, will be replaced by all (non-hidden) filenames in your current directory. This appears to be the name of one file only in your case, README.txt.
The final list that the loop will loop over is therefore
branch1 README.txt branch2 master
Instead, if you just want to output this in a script, use
git branch
without doing anything more.
If you want to save the output in a variable, use
branches=$( git branch )
Would you want to get the name of the current branch, then extract the branch whose name is preceded by a *:
curr_branch=$( git branch | awk '/^\*/ { print $2 }' )
Would you want to iterate over the output of git branch, use a while loop:
git branch |
while read -r star name; do
if [ -z "$name" ]; then
name=$star
printf 'One branch is "%s"\n' "$name"
else
printf 'The current branch is "%s"\n' "$name"
fi
done
You may also use git branch --show-current to get the name of the current branch directly.