1

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.

jsirr13
  • 944
  • 2
  • 12
  • 38
  • 3
    "Protected" is a property of one of those web interfaces (gitlab in this case), not of Git itself, so you're right: `git branch` *can't* tell you. (Also, in your own Git repo, everything is, well, your own.) – torek Feb 28 '18 at 00:55
  • Is there anyway I can tag certain branches, or some alternative approach so that I can distinguish from certain remote branches vs others? – jsirr13 Feb 28 '18 at 00:57
  • There's no way to automate this within Git (since Git has no idea that the remote has arbitrary constraints), but if you find some API you can run, you can automate whatever you like and make it do whatever you want. But there's no reason to change the way you work with your own Git locally anyway, as there's no constraint that your own Git branch's names match your Git's `origin/*` remote-tracking names. – torek Feb 28 '18 at 01:01

1 Answers1

2

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

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250