This tutorial explains how to rename the current local branch and remote branch.

How to rename a local branch in git

  • Step1#1:

Run the git branch -a command to list all local and remote branches

B:\nodetest>git branch -a
* branch
  branch1
  main
  remotes/origin/HEAD -> origin/main
  remotes/origin/branch
  remotes/origin/branch1
  remotes/origin/main

Run, the below command to know only local branches.

B:\nodetest>git branch
* branch
  branch1
  main
  • Step2#: There are two ways to rename a branch

  • Next, run the below commands, rename in the local repository

  • One approach: rename a branch that you need to check out and in

First switch to the branch you want to delete, using the below command

B:\nodetest>git checkout branch
Switched to branch 'branch'
Your branch is up to date with 'origin/branch'.

Currently, the current code is pointed to branch,

Run with git branch -m with the new branch name.

It renames from branch to newbranch.

B:\nodetest>git branch -m newbranch

Now, It is renamed and pointed to a new branch name. git branch gives a new branch name.

B:\nodetest>git branch
  branch1
  main
* newbranch
  • another way: you can rename with any branch pointed.

    • Changed branch to main
    • renamed branch1 to newbranch1
    • Now, branch is renamed in the local repository

First, Check the main branch.

B:\nodetest>git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.

next, rename branch1 to newbranch1

B:\nodetest>git branch -m branch1 newbranch1

List out the local branch using the below command.

B:\nodetest>git branch
* main
  newbranch
  newbranch1

  • Step3#: Changes are local only, which means, renaming the branch locally, and Push it to remote.

First, delete the branch from the remote repository using the below command.

git push origin :oldbranch

Next, Push a new branch name reference.

git push origin newbranch:refs/heads/newbranch

Here is the execution of the command

B:\nodetest>git push origin :branch

- [deleted]         branch

B:\nodetest>git push origin newbranch:refs/heads/newbranch
Everything up-to-date

See a list of all branch names, branch is renamed to newbranch

B:\nodetest>git branch -a
* main
  newbranch
  newbranch1
  remotes/origin/HEAD -> origin/main
  remotes/origin/branch1
  remotes/origin/main
  remotes/origin/newbranch

let’s rename branch1 in remote

B:\\nodetest>git push origin :branch1
 - [deleted]         branch1

B:\nodetest>git push origin newbranch1:refs/heads/newbranch1
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
remote:
remote: To create a merge request for newbranch1, visit:

 * [new branch]      newbranch1 -> newbranch1

Step5#: set upstream to remote branch

if there are references to old branches in the local repository in a machine, change upstream by running the command

git branch --unset-upstream

How to rename a remote branch git

There are multiple ways to rename remote branches with git.

  • One way, First, rename the local branch as per the above steps, next, push the changes to remote.
  • Second, directly rename the remote branch and keep the old name the same

Run the below command directly to rename a remote branch

git push origin origin/oldbranch:refs/heads/newbranch :oldbranch