Home > front end >  Is this a correct approach to rename a protected branch in GitLab?
Is this a correct approach to rename a protected branch in GitLab?

Time:09-20

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:

First way:

# 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

Second way:

# Unprotect branch

# Rename local branch
git branch -m master prod

# Delete old remote branch and push the new local branch
git push origin :master prod

# Switch to new local branch
git checkout prod

# Reset the upstream branch for the new local branch
git push origin -u prod

# Reprotect branch

CodePudding user response:

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

CodePudding user response:

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