How To Rename Local and Remote Git Branch
In this tutorial, we will show you how to rename a local or remote Git Branch. For those of you who didn’t know, In Git branches are used to create separate versions of a codebase. This allows multiple people to work on different parts of the codebase simultaneously without interfering with each other. When working with branches in Git, it’s often necessary to rename them, both locally and remotely. This can be done using a few simple commands.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you the step-by-step rename of a local or remote Git Branch on Linux.
Prerequisites
- A server running one of the following operating systems: Linux Server or Desktop.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Renaming a Local Branch.
To rename a local branch, you can use the git branch
command with the -m
option, followed by the current name of the branch and the new name you want to use. For example, if you want to rename the branch old-branch
to new-branch
, you can use the following command:
git branch -m old-branch new-branch
This will rename the branch locally, but it will not affect the remote repository. To push the changes to the remote repository, you will need to use the git push
command with the -u
option, followed by the remote name and the new branch name. For example:
git push -u origin new-branch
Renaming a Remote Branch
To rename a remote branch, you will need to first rename the local branch, as explained above, and then use the git push
command with the -u
option, followed by the remote name and the new branch name. For example:
git branch -m old-branch new-branch git push -u origin new-branch
This will push the renamed branch to the remote repository and update the remote branch to match the new local branch.
For additional resources on installing Git, read the post below:
Conclusion
In this tutorial, we explained how to rename both local and remote Git branches using the git branch
and git push
commands. By following these steps, you can easily change the name of a branch to better reflect the changes you have made to your code.