How to rename a branch in Gitlab?

HI,
I need to rename branch “branch” to “branch_old” in gitlab,
I searched for it and found that I need to do the following

  1. Checkout the branch locally.
  2. Rename it locally
  3. delete remote branch
  4. push the locally renamed branch to remote.

When I do this I get errors of pre-recive hooks rejecting the deletion of protected branches.

Then I go to gitlab and delete the protected branch via GUI and when I try to push the new branch, I get the following

$ git push --set-upstream origin branch_old
Counting objects: 8450, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (4249/4249), done.
Writing objects: 100% (8450/8450), 5.03 MiB | 3.19 MiB/s, done.
Total 8450 (delta 6592), reused 5965 (delta 4199)
remote: Resolving deltas: 100% (6592/6592), completed with 695 local objects.
remote: fatal: bad object 0000000000000000000000000000000000000000
To :testlab/.git

  • [new branch] branch_old -> branch
    Branch ‘branch_old’ set up to track remote branch ‘branch’ from ‘origin’.

In gitlab gui, I see the “branch” being restored.

What am I doing wrong? what is the correct way to rename a branch in Gitlab.?

2 Likes

You can create a branch named “branch_old” from “branch”, and then delete the branch named “branch” .

Try to disable the branch protection in the settings/repository page. Check the sections Default Branch and Protected Branches

After that:

  1. Checkout the branch locally.
    $ git checkout branch
  2. Rename it locally
    $ git checkout -b branch_old
  3. delete remote branch
    $ git push --delete origin branch
  4. push the locally renamed branch to remote.
    git push --set-upstream origin branch_old
4 Likes

Hello all,
Thanks a lot for your help. I was deleting the remote branch in Gitlab via GUI and that was not helping.
I tried deleting the remote branch by git on command line and it worked.

Regards

1 Like

That’s what I did in the same situation.
Easy going, worked like a charm.

(My branch to rename was not protected, so I did not have to unprotect it before deleting.)

I had to Used this command git branch -m old branch name new branch name. In terminal its showing correct but its not updated in browser. Please let me know any having knowledge about this.

I had to Used this command git branch -m old branch name new branch name . In terminal its showing correct but its not updated in browser. Please let me know any having knowledge about this.