4

Trying to get all the branches under a project using GitLab API, but I can see only 20 branches are returned. How can I get the complete list of all the branches? I am using the following API.

curl --header "PRIVATE-TOKEN: <token>" "https://gitlab.com/api/v4/projects/1521/repository/branches"
Ashish Pratap
  • 439
  • 5
  • 21
  • If does not send you back all, maybe try with `?search=*` at the end. Should force to match all – Marco Massetti Nov 26 '22 at 13:52
  • As mentioned in the answers below, results are returned on a per-page basis. You can extend the page length, but that option may break in the future as the results list grows. A more extensible option is to iterate through the pages until an empty set is returned. – Jim Fell Jul 21 '23 at 17:04

2 Answers2

4

Found the solution under pagination in the official Gitlab API documentation, by default we get 20 results, we can increase the number of results by using per_page in our API link as follows.

https://gitlab.com/api/v4/projects/<Project_id>/repository/branches?per_page=50
Ashish Pratap
  • 439
  • 5
  • 21
0

Getting branches is limited to 20 branches. In order to get all branches use query parameters like below:

https://gitlab.com/api/v4/projects/2009901/repository/branches/?page=2

https://gitlab.com/api/v4/projects/2009901/repository/branches/?per_page=100

Documentation can be found here: https://docs.gitlab.com/ee/api/#pagination

This is missing in the Branches API documentation unfortunately: https://docs.gitlab.com/ee/api/branches.html

Bouke
  • 1,531
  • 1
  • 12
  • 21
  • 1
    Apparently page size has a max limit of 100. I tried to put 500 and it still only pulled back 100 branches. Our repo has 161 branches and I had to get the rest of the branches by going to the 2nd page: ?per_page=100&page=2 – Tracy Xia Jul 24 '23 at 21:25