How to rename a Local Git branch?

It is easily possible to rename the existing local branch of GIT using the command- git branch and option -m on our system. We can use this command to rename the branch in case we have named it wrongly or just want to change it for some other project.

For example, your old git branch name is old-Linux-h2s and now you want to change it to some new name let’s say new-Linux-h2s. Now, the command syntax for this will be.

git branch -m old-Linux-h2s new-Linux-h2s

Running the above command syntax with your actual GIT branch name will only rename the branch available on your local system and does not affect the branch on the remote repository.

Whereas, for those who are interested in renaming the remote GIT branch as well on the remote repository then instead of the git branch command and -m option we will use the git push command with the -u option to update the remote branch. However, here we are actually pushing the locally renamed branch to the remote repository and then deleting the old branch from there. For example:

Let’s say the remote repository name is XYZ and you want to push the locally renamed branch to it

git push -u xyz new-Linux-h2s

After that, you will see both new and old branches in your remote repository. Hence, keep the new one and delete the old branch from the remote repository.

git push xyz --delete old-Linux-h2s

This will rename the remote branch to new-branch-name and set it to track the local new-branch-name branch.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.