Menu Close

Renaming Local and Remote Git Branch

Image: www.git-scm.com

For local branch:

First select the branch that you want to rename:

git checkout old_name

Replace the old_name branch with the new_name branch:

git branch -m new_name

Another way to rename the local branch is to define the old_name branch and new_name branch at once. For this first checkout the master branch:

git checkout master

Now, rename the branch with the following command:

git branch -m old_name new_name

For remote branch:

Remote branch cannot be renamed directly. We need to first delete the old remote branch and push a new branch to the remote repository.

Rename the local branch by following the above steps.

Delete the old branch from the repository and push the new branch.

git push origin --delete old_name

Reset the upstream branch for the local branch:

git push origin -u new_name

Another way to do the above steps directly is:

git push origin :old_name new_name
git push origin -u new_name

Leave a Reply

Your email address will not be published. Required fields are marked *