Introduction
Git is a software package used for tracking software as it moves through stages of development. Git uses branching to maintain a central repository of code while creating a copy to make changes on.
In this guide, learn how to change the name of a Git branch on a local system or remote repository.
Prerequisites
- An existing installation on Git on CentOS or Git for Ubuntu
- A Linux-based operating system
- Access to a terminal window/command line (Ctrl+Alt+T or Ctrl+Alt+F2)
Rename Local Branch
To rename a branch in Git:
1. Enter the name of the branch you want to change in your command-line interface:
git checkout old-branch
You should receive confirmation that the branch is checked out.
2. Rename the branch by entering the command:
git branch –m new-name
Alternatively, you can use a single command. If you’re not already in the master, switch to it:
git checkout master
Enter the following to change a branch name:
git branch –m old-name new-name
3. Verify the renaming was successful by checking the status :
git branch –a
The output confirms that the branch was successfully renamed, as shown below.
This is useful if you created a new branch and pushed your remote repository’s changes to discover the branch name was incorrect.
Note: Replace old-name with the actual name of the branch you want to change. Replace new-name with the name of the branch you want to use going forward.
Rename a Remote Git Branch
There isn’t a way to directly rename a Git branch in a remote repository. You will need to delete the old branch name, then push a branch with the correct name to the remote repository.
1. Verify the local branch has the correct name:
git branch -a
2. Next, delete the branch with the old name on the remote repository:
git push origin ––delete old-name
The output confirms that the branch was deleted.
3. Finally, push the branch with the correct name, and reset the upstream branch:
git push origin –u new-name
Alternatively, you can overwrite the remote branch with a single command:
git push origin :old-name new-name
Resetting the upstream branch is still required:
git push origin –u new-name
Note: Replace old-name with the actual name you want to change. Replace new-name with the name you want to use going forward.
Conclusion
Now you know how to rename a local or remote Git branch, even if it has been loaded on a remote repository.
Next you should also read
SysAdmin,DevOps and Development
February 3, 2020
Developers need to switch between branches frequently. Git branches allow you to work on your code, fix bugs,…
SysAdmin,DevOps and Development
How to Install and Use Git on Windows
January 8, 2020
Git tracks source code changes during the software development process. It can help coordinate work among…
SysAdmin,DevOps and Development
How to Create a New Branch in Git
December 30, 2019
This article outlines the basic commands needed to create a Git branch. A Git branch allows you to work on…
SysAdmin,DevOps and Development
How To Install Git on Ubuntu 18.04 / 20.04
April 10, 2019
Git is a widely-used software package in Linux. Its main purpose is to track changes in software code during…
SysAdmin,DevOps and Development
How to Install Git on CentOS 7 With Yum or Latest Repository
March 15, 2019
In this tutorial, learn how to install Git on CentOS 7. Git is a distributed version control system to track…
Security,DevOps and Development
How to Implement Validation for RESTful Services with Spring
November 13, 2018
Data validation is not a new topic in web application development and here we take a brief look at data…
Author
Vladimir Kaplarevic
Vladimir is a resident Tech Writer at phoenixNAP. He has more than 7 years of experience in implementing e-commerce and online payment solutions with various global IT services providers. His articles aim to instill a passion for innovative technologies in others by providing practical advice and using an engaging writing style.