Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming a branch in GitHub

I just renamed my local branch using

git branch -m oldname newname 

but this only renames the local version of the branch. How can I rename the one on GitHub?

like image 335
enchance Avatar asked Mar 01 '12 22:03

enchance


People also ask

Can you rename an existing branch in git?

You created a new branch , pushed the changes to the remote repository, and realized that your branch name was incorrect. Luckily, Git allows you to rename the branch very easily using the git branch -m command.

How do I rename a branch in repository?

The steps to change a git branch name are: Rename the Git branch locally with the git branch -m new-branch-name command. Push the new branch to your GitHub or GitLab repo. Delete the branch with the old name from your remote repo.


2 Answers

As mentioned, delete the old one on GitHub and re-push, though the commands used are a bit more verbose than necessary:

git push origin :name_of_the_old_branch_on_github git push origin new_name_of_the_branch_that_is_local 

Dissecting the commands a bit, the git push command is essentially:

git push <remote> <local_branch>:<remote_branch> 

So doing a push with no local_branch specified essentially means "take nothing from my local repository, and make it the remote branch". I've always thought this to be completely kludgy, but it's the way it's done.

As of Git 1.7 there is an alternate syntax for deleting a remote branch:

git push origin --delete name_of_the_remote_branch 

As mentioned by @void.pointer in the comments

Note that you can combine the 2 push operations:

git push origin :old_branch new_branch

This will both delete the old branch and push the new one.

This can be turned into a simple alias that takes the remote, original branch and new branch name as arguments, in ~/.gitconfig:

[alias]     branchm = "!git branch -m $2 $3 && git push $1 :$2 $3 -u #" 

Usage:

git branchm origin old_branch new_branch 

Note that positional arguments in shell commands were problematic in older (pre 2.8?) versions of Git, so the alias might vary according to the Git version. See this discussion for details.

like image 161
Adam Parkin Avatar answered Oct 13 '22 23:10

Adam Parkin


The following commands worked for me:

git push origin :old-name-of-branch-on-github git branch -m old-name-of-branch-on-github new-name-for-branch-you-want git push origin new-name-for-branch-you-want 
like image 28
Taimoor Changaiz Avatar answered Oct 14 '22 00:10

Taimoor Changaiz