gitgitlabbranchgit-branch

Git: how can I retrieve all branches that are protected?


I want to be able to get all branches from a project that are protected. The protection is done in gitlab, so I wasn't sure if there's API exposed so I can get it from the command line.

I looked at git branch command, but I didn't see anything there that could tell me whether a branch is protected or not.


Solution

  • You can use the GitLab API for listing protected branches.

    GET /projects/:id/protected_branches
    

    From there, you can use in your local repo git config or git notes to leave a "marker" to your branches.


    Note that GitLab 15.10 (March 2023) includes a nice GUI for this:

    See all branch-related settings together

    All branch-related protections now display on a single page.

    To see a unified list of your branches and all their protection methods, go to Settings > Repository > Branch rules.
    Each branch shows the merge request approvals, security approvals, protected branches, and status checks configured for it.

    branches

    Previously, these settings were grouped by type, making it tough to see a holistic view of a specific branch’s protections.

    We hope this change helps you discover, use, and monitor these settings more easily. We’d love your feedback in issue #388149.

    See Documentation and Epic.
    And this video


    See also GitLab 17.5 (October 2024)

    Enhanced branch rules editing capabilities

    In GitLab 15.10, we introduced a consolidated view for branch-related settings and rules. This view provided you with an easy way to understand the configuration of your project across multiple settings.

    Building on this feature, you can now directly modify specific branch rules in this view, including branch protections, approval rules, and external status check configurations. These new capabilities lay the foundation for continued improvements in branch configuration that will allow for greater flexibility in the future.

    We encourage you to explore these new capabilities and to provide feedback. You can do this by contributing to our dedicated feedback issue.

    See Documentation and Epic.