git branch vs $(git branch)

不问归期 提交于 2019-12-28 07:09:10

问题


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

回答1:


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




回答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.



来源:https://stackoverflow.com/questions/24812464/git-branch-vs-git-branch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!