how do i read all the branches in a git repo using the git api?

You have to perform a full repository checkout in your workflow job in order to get the full repository data (including all branches). When you do git clone... (without any switches like --depth or --single-branch) on your local machine you are getting all the repository data (tags, branches, commit history, etc.). This is why git branch -r command output contains all the remote branches.

In GitHub Actions (or mentioned Travis CI) the clone operation behaves slightly different. By default, to conserve bandwidth and disk storage space the CI system retrieves only the minimal required part of the repository in order to execute the workflow. This usually is only the current branch associated with the workflow and limited commit history. This is why your commands do not return all the expected branches. However, you can override the default checkout behaviour. If you look at the actions/checkout docs:

Only a single commit is fetched by default, for the ref/SHA that triggered the workflow. Set fetch-depth: 0 to fetch all history for all branches and tags.

Taking the above into consideration, to fetch all the branches, you have to adjust the actions/[email protected] step in your workflow according to the docs:

Fetch all history for all tags and branches

- uses: actions/[email protected]
  with:
    fetch-depth: 0

Now, when you call in the next step sth. like:

- name: List all the remote branches
  run: git branch -r

you should get the expected output.

Speaking about Travis CI. The documentation is also clear about this (see §Git Clone Depth). You have to adjust your travis.yml to disable --depth flag:

git:
  depth: false

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top