Geek Logbook

Tech sea log book

How to Rename a Git Branch Locally and Remotely

Renaming Git branches can be necessary when adhering to naming conventions or correcting errors. This guide will walk you through the process of renaming a branch locally and remotely.

Scenario:

You accidentally pushed a branch named old-branch-name to the remote repository but realize it should follow the convention feature/new-branch-name.


Steps to Rename the Branch

1. Rename the Branch Locally

To rename a branch locally, use the git branch -m command:

git branch -m old-branch-name feature/new-branch-name

This renames the branch from old-branch-name to feature/new-branch-name in your local repository.


2. Push the Renamed Branch to the Remote

Next, push the renamed branch to the remote repository and set it to track the upstream branch:

git push origin -u feature/new-branch-name

3. Delete the Old Branch on the Remote

To remove the old branch from the remote repository, use the following command:

git push origin --delete old-branch-name

4. Verify the Changes

To confirm everything is correctly set up, list the branches:

git branch -a

This will show both local and remote branches. Verify that feature/new-branch-name exists and that old-branch-name has been removed.

Tags: