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.?

3 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
3 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.

“git branch -m oldbranchname newbranchname” only changes your local repo.
You need to push:
git push origin HEAD:newbranchname

Then you need to delete the old remote branch:
git push origin --delete oldbranchname

2 Likes

from @Michal.Rysanek should be the accepted answer.

1 Like

This does not help if you have merge requests pending from that branch, you’ll also need to delete the merge requests and create new ones which is not ideal. Simply renaming a branch in the remote would be great.

1 Like

It can be done by :point_down:
rename the branch locally:

git branch -m old-branch-name new-branch-name

then, delete the old branch from the remote:

git push origin --delete old-branch-name

Finally, push the new branch to the remote and set the upstream:

git push origin new-branch-name
git push --set-upstream origin new-branch-name

Inform your team members about the branch name change, so everyone is aware and can update their local repositories accordingly.

1 Like