

How to Rename a Branch in Git
source link: https://www.makeuseof.com/git-rename-branch/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Git is a popular version control system that makes it super easy to change branch names. Developers may wish to rename branch names for many reasons. So open-source collaborators should also know how to rename a branch in Git.
To make things simple for you, we will explain how to rename both local and remote git branches.
How to Rename a Local Branch in Git
Git users typically work in local versions of their projects. Once a change is validated, it is integrated to the upstream. It's always a good idea to see which local branches are available before you rename one of them.
$ git branch
$ git branch -a
You can find out the local branches for your Git project using the above commands. The -a option also lists the remote branches. Now, you can proceed with renaming your local Git branch. Follow the below steps for doing this from your terminal.
1. Verify the Local Branch
$ git checkout <old-branch-name>
$ git checkout alpha
The git checkout command allows us to switch between branches and verify working trees. If you're on the alpha branch, the last command will confirm it. If you're on a different branch, it will switch to alpha.
2. Rename the Local Branch
Once you switch to the desired branch, you can rename it using the git rename branch command.
$ git branch -m <new-branch-name>
$ git branch -m beta
This command changes the name of the local branch alpha to beta.
You can also rename a local branch from inside another git branch. Use the following command to do this from your favorite Linux terminal.
$ git branch -m <old-branch-name> <new-branch-name>
$ git branch -m alpha beta
3. Verify the New Branch Name
You can easily verify whether the git rename branch operation was successful by listing the branches once more.
$ git branch -a
How to Rename a Remote Branch in Git
Git refers to remote versions of your repositories as simply 'remotes'. You can not rename a remote branch as easily as local branches. Instead, you will need first to rename the local branch, push the new branch to the server, and delete the old branch from your repository.
1. Rename the Local Branch
Rename the local branch alpha to beta using the git rename branch command below.
$ git branch -m beta
$ git branch -m alpha beta
2. Push the Updated Branch
Push the renamed branch beta to the remote server using the following command.
$ git push origin <new-branch-name>
$ git push origin beta
3. Set the Upstream
You need to set the upstream to make sure Git can track changes between your remote and local branches.
$ git push origin -u <new-branch-name>
$ git push origin -u beta
Git will set up tracking between the local branch beta and the remote branch beta.
4. Remove the Old Branch
You can safely delete the old branch from your remote once you've pushed the renamed branch and set up the upstream. Use the following git command for deleting a remote branch in Git.
$ git push origin --delete <old-branch-name>
$ git push origin --delete alpha
Git will proceed to delete the alpha branch from the remote origin.
5. Verify the Remote Branch
List the remote git branches once more to verify if the branch renaming operation was successful or not. You should see the new remote branch beta after issuing the following command.
$ git branch -a
Rename Git Branches Effectively
Renaming git branches is quite straightforward once you learn some basic git operations. You can easily change branch names for local branches. We've also covered the necessary git rename branch commands for remote branches. Try some of the commands on your test projects to get the relevant experience.
About The Author

Rubaiat Hossain (12 Articles Published)
Rubaiat is a CS grad with a strong passion for open-source. Apart from being a Unix veteran, he's also into network security, cryptography, and functional programming. He's an avid collector of secondhand books and has a never-ending admiration for classic rock.
Subscribe To Our Newsletter
Join our newsletter for tech tips, reviews, free ebooks, and exclusive deals!
Recommend
-
181
How do you rename a Git tag? Asked...
-
64
-
128
-
19
The Internet Engineering Task Force (IEFT) points out that "Master-slave is an oppressive metaphor that will and should never become fully detached from history" as well as "In addition to being inappropriate and arcane,...
-
15
Rename a git branch Posted: 2010-08-09 - Last updated: 2019-06-05 Tagged
-
11
How to Rename the Default Git Branch and Why You’d Want To By Bobby Jack Published 12 hours ago Use this tutorial to change your de...
-
9
Rename Hugo Blog Git Repo Branch From master To main 2021-03-05 :: 荒野無燈 :: Mod 2021-03-07(2270d09) #git March 7, 2021
-
10
Renaming files on git can be really painful to deal with. Git is case insensitive, so we can’t rename the file and hope for them to be updated in the remote repository. It can also create all sorts of
-
14
March 10, 2022 /
-
14
How do you rename a branch in GitTo rename a branch in Git, you can use the following steps: 1. Check the current branch: Before renaming the branch, ensure that you are not cu...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK