How to Rename a Git branch

Renaming a Git branch is straightforward. Follow these steps depending on whether you're working with a local or remote branch.

Rename a Local Git Branch

Step 1: Switch to the Branch You Want to Rename

You can't rename the branch you're currently on. Switch to a different branch first:

git checkout <another-branch>

Step 2: Rename the Branch

Use the following command to rename the branch:

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

For example:

Rename a Remote Git Branch

Renaming a remote branch requires additional steps:

Step 1: Rename the Local Branch

Follow the steps above to rename the local branch.

Step 2: Delete the Old Remote Branch

Push the deletion of the old branch to the remote:

git push origin --delete <old-name>

Example:

Step 3: Push the Renamed Branch to Remote

Push the new branch to the remote repository:

git push origin <new-name>

Step 4: Update Remote Tracking (Optional)

If your new branch should track the remote branch, set the upstream:

git branch --set-upstream-to=origin/<new-name> <new-name>

A single code search interface for all your repos

Search across all your repos (GitHub, GitLab, BitBucket, etc) using a single open-source, blazingly fast, and feature-rich interface.

A single code search interface for all your repos

Search across all your repos (GitHub, GitLab, BitBucket, etc) using a single open-source, blazingly fast, and feature-rich interface.