When working with Git, it’s common for projects to contain multiple branches. Sometimes these branches change purpose over time or simply have a naming error, and in such cases it’s necessary to rename the branch.
In this guide, we’ll show you the step by step instructions for renaming Git branches via the command line on a Linux system. We’ll show the process for renaming local branches as well as remote branches and go over what you need to know to ensure a smooth transition.
In this tutorial you will learn:
- How to rename local and remote Git branches
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
Rename Git Branch
When renaming a Git branch, you have a few options to consider. Most likely, you want to rename the local branch and the remote branch along with it. But it’s also possible to rename just the local branch. We’ll show you the steps for both options below.
If the HEAD is currently pointed to the branch you wish to rename – in other words, if you’ve “checked out” the current branch or are actively working with it – you can rename it with the following command.
$ git branch -m <new_name>
Or to rename some other branch that the HEAD isn’t pointed to:
$ git branch -m <old_name> <new_name>
This has renamed the local Git branch. If you push this branch, it won’t conflict with the old name anymore, but rather be its own branch under the new name. From here, it’s up to you if you want to delete the old branch.
$ git push <remote> --delete <old_name>
Finally, push the new branch to remote and reset the upstream branch.
$ git push <remote> <new_name> $ git push <remote> -u <new_name>
Note that some servers may require the
--unset-upstream option in order to begin pushing the new name instead of the old one.
$ git checkout <new_name> $ git branch --unset-upstream
In this article, we learned how to rename a local and remote Git branch on Linux. We also saw some recommended commands to help finalize the changes and ensure a smooth transition.