If I run git branch
in a terminal, I get this output:
abstract-controller
core-button-bar
* dev
final-core-datatable
generic-loan-search-exp
working-tags
I have a Bash script that looks like this:
branches=$(git branch)
echo ${branches)
It does output the branches, but also includes all of the files in the current directory.
abstract-controller core-button-bar README.md config <<all the other files>> dev final-core-datatable generic-loan-search-exp working-tags
First, why is it doing this, and second, how can I get just the branches?
CodePudding user response:
git branch
is not doing it. Your shell is doing it:
- What does
echo *
print, in your shell? - Who expanded
*
?
Once you know and understand the answers to these, you know what happened with git branch
.
Git, being sensibly written,1 has its commands neatly—or sometimes messily—divided into programs whose output is designed for other programs to consume, and programs whose output is designed for humans to read. The former programs are called plumbing and the latter are called porcelain.
The plumbing version of git branch
is git for-each-ref
, run with refs/heads/
as a limiting refspec. The documentation tells you what --format
directives to use here as well. You probably want just %(refname:short)
.
1For some values of sensibly anyway.