Allow force push on all GitLab repositories
2022.07.16
devopsgitlabgit
After moving repos to GitLab I needed to force push changes across many repos. Grab the repo IDs first, then unprotect branches and re-protect with force push allowed.
# Get repo IDs:
# curl "https://gitlab.com/api/v4/projects?private_token=<token>&simple=true&owned=true&per_page=100" | jq .[].id
#!/bin/bash
gitlab_ids=( 11111111 22222222 33333333 )
for i in "${gitlab_ids[@]}"; do
echo "Processing: $i"
curl --request DELETE \
--header "PRIVATE-TOKEN: <token>" \
"https://gitlab.com/api/v4/projects/$i/protected_branches/master"
curl --request DELETE \
--header "PRIVATE-TOKEN: <token>" \
"https://gitlab.com/api/v4/projects/$i/protected_branches/main"
curl --request POST \
--header "PRIVATE-TOKEN: <token>" \
"https://gitlab.com/api/v4/projects/$i/protected_branches?name=*&allow_force_push=true"
sleep 0.2
done