Is this a correct approach to rename a protected branch in GitLab?

Viewed 34

I currently have two protected branches in my GitLab repo, but want to rename master to prod.

gitlab protected branches

From my research I found some steps and wanted to make sure they were correct:

# Unprotect branch

# Switch to old local branch
git checkout master

# Create and switch to new local branch
git checkout -b main

# Delete old remote branch
git push --delete origin master

# Delete old local branch
git branch -D master

# Push new local branch to remote and set upstream branch
git push --set-upstream origin main

# Reprotect branch
2 Answers

Technically, you can create prod before unprotecting and deleting master, but yes, the steps you describe will work.

Yes, your steps look correct. Note that you can just create a new branch rather than "rename" master. You can do this entirely in the GitLab UI. The steps are roughly something like this:

  1. Create prod branch based on master.
  2. Set prod as a protected branch and configure rules similar to your current master.
  3. Unprotect master
  4. Delete master.
Related