0

Say I have a git repo containing these items at the project root, which is also my working directory.

dir1 dir2 script.sh

And I have git branches titled

master 077-fix_issue_a 078-fix_issue_b

I find that if I run the git branch command, I get the following output

077-fix_issue_a
078-fix_issue_b
master

But if I run

for i in `git branch`
do
    echo $i
done

I get the output of

077-fix_issue_a
078-fix_issue_b
dir1
dir2
script.sh
master

Why does git branch also contain a list of directory items when run with command substitution?

DeepDeadpool
  • 1,441
  • 12
  • 36

2 Answers2

2

Because git marks the currently checked out branch with a star, and that star is expanded to all files in current directory. Try

for i in $(git branch | cut -b 2-)
mogu
  • 1,091
  • 6
  • 8
0

Nevermind I found out.

git branch puts a wildcard by the active branch.

DeepDeadpool
  • 1,441
  • 12
  • 36
  • More generally, when writing scripts, avoid `git branch` for this kind of usage. Instead, use `git for-each-ref refs/heads`. See [the `git for-each-ref` documentation](https://www.kernel.org/pub/software/scm/git/docs/git-for-each-ref.html) for details. – torek Dec 28 '17 at 21:19