How can I rename git local and remote branch name?

Ask a question+

I have a branch named stage and want to change both its local and remote names. I’m new in git and don’t know which command to use. Please, help me.

Oliver 700
add comment

4 Answers


If you are trying to rename branches remotely, you can use this command
git push <remote> <remote>/<old_name>:refs/heads/<new_name> :<old_name>

Alvard 940
add comment

If you are a new to git, you might meet these words very often : local and remote.
local — is the code on your machine/device and remote —is the code repository hosting services like GitHub, GitLab,
1: Rename your local branch
type git branch to get the list of all branches.
if you are in the same branch

git branch -m new-name

if you are not in the same branch

git branch -m old-name new-name

2: Pushing changes to the remote
if the renamed branch is not pushed to the remote, then you can push it normally by

git push origin new-name

if the renamed branch is already there on the remote (if you push it using the above method, it will create a new branch instead of replacing it) use

git push origin :old-name new-name

1: Deleting only in the local
if you want to delete a branch only in the local and not in the remote, you can use this

git branch -d branch_name


git branch -D branch_name

use -D only If there are un-merged changes that you want to delete.

2: Deleting only in the remote
You want a branch to present in the local but not in the remote, just do this

git push origin --delete branch_name

3: Deleting in both local and remote
if this is the case, then complete 1 and do2 or vice-versa.

Sargsyan 435
add comment

You shouls create a new local branch with the “new” name, push it to your remote, and then delete the old remote branch:

$ git branch new-branch-name origin/old-branch-name

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

$ git push origin :old-branch-name

Then, to see the old branch name, each client of the repository would have to do:

$ git fetch origin
If you want to try some examples, you can do it in W3Docs

$ git remote prune origin

Muhamed Aziz 320
add comment

Rename a Local Git Branch

$ git branch -m <old-branch-name> <new-branch-name>

The -m option is an alias for —move, which is analog to the Unix mv command.
Continuing with the convention we saw with the delete branch option, capitalizing the flag as -M, which is an alias for —move —force, allows you to “force” the change. Using the option in this way will let you rename the branch even if the new branch name already exists in your repository.

If you’re wanting to rename the branch that is currently checked out, then you can omit the <old-branch-name> option, which looks like this:

$ git branch -m <new-branch-name>

Rename a Remote Git Branch

Renaming a remote branch is a bit more involved, and isn’t actually possible in the same way it is for renaming local branches. To do it, you’ll need to rename the local branch, delete the remote branch, and then push the renamed local branch to the remote repo again.

In terms of Git commands, here is how the process looks:

$ git branch -m <old-branch-name> <new-branch-name>

$ git push <remote-repo> -d <old-branch-name>

$ git push <remote-repo> <new-branch-name>

$ git checkout <new-branch-name>

$ git push <remote-repo> -u <new-branch-name>

In plain English, here is what is happening line-by-line:

  • Rename the local branch using the same method shown the first section
  • Delete the remote branch that is to be renamed
  • Push the new branch to the remote repo
  • Switch to the new branch
  • Reset the upstream reference for the renamed branch

Once you understand what is going on, it isn’t too bad, but it certainly requires more steps than simply renaming a local branch.
Source: https://www.w3docs.com/learn-git/git-push.html

Jack 590
add comment

Your Answer