Git Branch: How to rename a local and remote branch
You can rename a local or remote Git branch by using the -m command. While this is not a problem for the local branch, for the remote branch you must first delete the outdated version and replace it with the new one.
Git: Open source and easy to learn
Git is a version control system that has been available license-free and free of charge since 2005. The special feature of the tool is that numerous developers can work simultaneously and completely autonomously on a project. This is because Git does not work with a central repository, but each user can download a copy to his local computer. This also acts as a back-up. Git is characterized by its ease of use and is quick to learn. In our Digital Guide you will find a Git tutorial, which will help you with the first steps when using the tool and ensure that you can soon start your own projects.
$1 Domain Names
Register great TLDs for less than $1 for the first year.
Why wait? Grab your favorite domain name today!
Get your own website in just a few steps! With the Website Builder from IONOS, you can easily create a professional website for your company or for private purposes. Thanks to three plans on offer, you will also find the right option for your purposes.
What is a Git repository?
In order to understand how renaming the Git branch works, it is advisable to first take a look at how Git is actually structured and used. The Git repository is crucial in this regard. The version control system uses the repository as a kind of collection basin. All files of current and previous versions of a project are stored here and made available to all developers. Any changes can thus be easily tracked, while errors can be corrected more quickly. In contrast to systems such as SVN Git builds thereby not on a central repository, but on numerous local copies. Each developer can work on these independently.
What is a Git branch?
If a large number of developers are working on a project and are pursuing different approaches, it is all the more important to separate work steps and still maintain an overview. For this purpose, Git uses so-called branches. Think of it like a tree. Thus, numerous small branches or lines of development branch off from the large trunk of the entire project, which can be worked on autonomously. New features can be added with branches, or bugs can be fixed quickly. However, the original code is not changed.
A website according to your taste! The Website Design Service from IONOS helps you to create the website of your dreams. Get free advice from our experts and benefit from regular maintenance and updates even after going live!
How can I rename a Git branch?
There may be times when you need to rename a Git branch. This is because if the naming is wrong and other developers continue to work with it, you may run into problems. Fortunately, despite the tight integration and various forks, if you want to rename a Git branch, it’s not a big issue. To do this, use the -m command. The corresponding syntax always follows the same structure:
“git branch -m <old-name> <new-name>”.
However, there are differences between branches that you edit locally and those that are already remote. Below we explain the steps for both cases.
Rename a local Git branch
A local Git branch exists only on your computer. You make changes and tests here without other developers noticing. Renaming it can therefore be done quickly.
- In the command line, select the Git branch you want to rename. The command for this is “git checkout old-name”.
- You will get a confirmation that you have selected the correct branch. This will read “Switched to branch 'old-name'”.
- Now perform the actual rename for the local Git branch. The appropriate command for this is: “git branch -m new-name”.
Alternatively, you have the option to rename the Git branch via the master. To do this, use the following steps:
- Switch to the master via the command “git checkout master”.
- Now enter the following command if you want to rename a Git branch: “git branch -m old-name new-name”.
- To ensure that the rename was successful, retrieve the current status of the branch using the “git branch -a” command.
Renaming a remote Git branch
In a remote repository, you cannot simply rename a Git branch, as this would lead to complications. Instead, you need to delete the old name and then add the branch with the new name. Fortunately, this is not too hard either and can be done with a few simple commands. As with the local branch, you have two options.
- First, make sure the local branch has the correct, new name. The appropriate command is “git branch -a”.
- Now delete the branch with the old, incorrect name from the remote repository. To do this, use the following command: “git push origin --delete old-name”.
- Verify that the old branch has been deleted properly.
- Now add the branch with the correct name. For this, use the command “git push origin -u new-name”.
- Lastly, perform a reset of the upstream branch to ensure that the changes are effective.
However, if you want to rename the remote Git Branch with just one command, you also have the following option.
- Enter the following command: “git push origin :old-name new-name”.
- Then also perform a reset of the upstream branch as described above.
Conclusion: Git branch rename
Mistakes happen, and Git is designed to let you rename Git branches easily. It can be done quickly and with a few commands. While the process is a rename in the true sense for local branches, a deletion is necessary in the remote branch. Once the error is fixed, it makes sense to carefully double-check. Afterwards you and other developers can continue working on your project without any restrictions.
Test the IONOS Cloud Server for free and convince yourself of its countless advantages. With our XS to 5XL plans, you will find exactly the right choice for your need.