Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename remote branch without deleting it

Tags:

git

I need your help to rename a remote branch.
I created a PR but now I need to rename the remote branch name.
I don't want to follow the process where I have to rename the local branch then push and delete the old branch because it will clear the PR.
Is there a way to just rename the remote branch and PR should stay as it is. thanks.

like image 436
Vikas Rathore Avatar asked Jan 25 '23 00:01

Vikas Rathore


1 Answers

Renaming a remote branch in Git is really just creating a new branch with same commits and new name and deleting old branch. Depending on where you have pull request open (you don't mention whether is it Github, Gitlab, Phabricator or something else) you might have some options given to you by tool operating on repository locally where pull request exists. I can say only for Github that there's no option to rename a branch in their interface nor option to change "compare" branch in a pull request.

Best solution (for Github) is probably to create a new branch and new pull request, post a comment to old pull-request that it was "closed in favor of #NEW" and "continuation of #OLD" to new one. Then delete old branch which will also close the old pull-request.

Quick oneliner for deleting old branch and creating a new one with same content is:

git push <remote> <remote>/<old>:refs/heads/<new> :<old>

where remote is obvious - e.g. origin, old and new are names of branches old and new respectively.

like image 188
blami Avatar answered Jan 28 '23 11:01

blami