To rename a Git branch, you first rename it locally and then update the remote repository if necessary.

Renaming a Local Branch
You can rename a local branch whether you are currently on that branch or not. The -m flag stands for "move" (or rename).

If you are on the branch you want to rename:

1. Switch to the branch you want to rename using git checkout <old-name> (or git switch <old-name>).

2. Run the following command:

bash

git branch -m <new-name>

The current branch will be instantly renamed.

If you are on a different branch:

Run the following command, specifying both the old and new names:

bash

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

This renames the specified branch without switching to it.

Renaming a Remote Branch

It is not possible to rename a remote branch directly. Instead, you delete the old branch from the remote and push the newly renamed local branch.

1. Ensure your local branch is renamed as described above.

2. Push the new branch to the remote repository and set its upstream tracking reference:​

bash

git push origin -u <new-name>

This command pushes the new branch and links your local branch to the remote one.

3. Delete the old branch from the remote repository:​

bash

​​git push origin --delete <old-name>

This removes the old name from the remote server.

Renaming on a Hosting Service (GitHub, GitLab, etc.

Alternatively, you can often rename a branch directly through the web interface of your hosting service (like GitHub). The platform will usually manage redirects for a period, making the process smoother.

← Back to Learning Journey