0

I would like to perform a branch comparison in a Github Action. But git branch returns an ls-like result instead with a list of files!

run: echo $(git branch)

shell: /bin/bash -e {0}

LICENSE README.md index.html node_modules package-lock.json package.json src yarn.lock (HEAD detached at pull/29/merge)

Why is that? Is it broken?

How to perform git commands on branches?

Community
  • 1
  • 1
Mouloud85
  • 3,826
  • 5
  • 22
  • 42

1 Answers1

1

git branch returns a list in which the current branch is marked with an asterisk *. Then echo (actually, the shell) expands the asterisk.

Either process the output of git branch and remove * or use git for-each-ref refs/heads/.

Upd. git clone --depth implies --single-branch. To fetch other branches:

git remote set-branches origin '*'
git fetch

See git shallow clone (clone --depth) misses remote branches

phd
  • 82,685
  • 13
  • 120
  • 165
  • Thanks! This answers why the list of files is added. But I still don't know why I can't see other branches. By the way, I can also add `fetch-depth: 0` to the checkout, as recommended by the docs, but it doesn't help. – Mouloud85 Jun 12 '20 at 08:38
  • "*I still don't know why I can't see other branches.*" Most probably because Github Action didn't fetch them. Please be advised that `git clone --depth` implies `--single-branch`. – phd Jun 12 '20 at 10:41
  • Correct. I'll validate the answer, if you could add the `git fetch` to the run – Mouloud85 Jun 12 '20 at 11:11