How to rename remote branch git
How to rename remote branch git
How To Rename a Local and Remote Git Branch
Home » SysAdmin » How To Rename a Local and Remote Git Branch
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.
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:
You should receive confirmation that the branch is checked out.
2. Rename the branch by entering the command:
Alternatively, you can use a single command. If you’re not already in the master, switch to it:
Enter the following to change a branch name:
3. Verify the renaming was successful by checking the status :
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:
2. Next, delete the branch with the old name on the remote repository:
The output confirms that the branch was deleted.
3. Finally, push the branch with the correct name, and reset the upstream branch:
Alternatively, you can overwrite the remote branch with a single command:
Resetting the upstream branch is still required:
Note: Replace old-name with the actual name you want to change. Replace new-name with the name you want to use going forward.
Now you know how to rename a local or remote Git branch, even if it has been loaded on a remote repository.
Table of Contents
In this tutorial I will share the steps and instructions to git rename branch (both local and remote).
Let me clone a branch from my gitlab server to demonstrate this article:
Git rename branch workflow
Following is the basic workflow to rename a branch locally and remotely:
Now we will explain these steps in the next sections with different examples and scenarios.
git rename local branch only
This section assumes that you have a local branch (which is not available on the remote git server) and you intend to rename this local branch only. I will create some local branch:
Following are the list of branch available on my local workstation:
Following are the list of directories on my repository:
I will go ahead an upload some patch under bugfixes directory:
Now we have added patch-7843-1 to our local repo:
As you can see, currently I am in issue-7843 branch:
The syntax to rename local branch is:
Check your current branch, as you can see the issue-7843 branch is not there any more and is renamed to issue-new-7843 as expected:
Verify your uncommitted changes to make sure they are still there:
Since this branch is only locally available, if you push these changes then a new branch issue-new-7843 would be created on the remote server so you don’t need to perform any further renaming for remote branch here.
git rename remote branch only
In this section we assume that you intend to rename remote repository only.
Scenario-1: When remote branch is also cloned locally
In this scenario we assume that the respective remote branch is also cloned locally wherein you wish to rename the remote branch only, leaving the local branch with the existing name.
In our previous example we had renamed our local git branch to issue-new-7843 but it was not pushed to git server. So let’s push this to the remote server:
Verify the list of remote branches:
So now issue-new-7843 exists in my local workstation as well as remote server.
Let us go ahead and rename this branch name on the remote server only. The syntax to be used:
So, we will use this syntax to rename remote branch from issue-new-7843 to issue-7843 on our gitlab server:
The remote branch has been renamed successfully while the local branch name is still the same:
Scenario-2: When remote branch is not cloned locally
In this scenario we assume that the remote branch you plan to rename is not cloned locally. For example I have a remote branch feature is is not cloned on my local workstation:
I also have some un-merged changes in my main branch. This is just to make sure my changes are not lost while renaming the remote branch:
Now let us rename the remote branch using the same syntax as we used earlier, we will rename feature branch to new-feature :
So our remote branch has been successfully renamed:
Now we can also push our previously committed changes to the main branch without loosing any changes:
As you can see our main branch is ahead of new-feature branch by one commit:
git rename branch both local and remote
Now let us rename a git branch in both local and remote repository. Let me pull another branch from my remote repository:
So currently I have the following list of branch on my local workstation:
In this example we will rename issue-7843 to new-issue-7843 in both local and remote repository.
Step-1: Checkout as different branch
This is an optional step and you may choose to ignore it. You can also rename a branch while working in the same branch.
Check your current branch
Step-2: Rename local branch
If you are still in the same branch (i.e. you skipped the step-1 above):
OR you must use the following command by specifying both old and new branch name:
Since we are in a different branch we will execute the following command to perfom the rename:
Verify the new branch name in your local repo:
Step-3: Delete the old-name remote branch and push the new-name local branch
Next we will delete the old branch from the remote repository and push the new branch to the remote repo using following syntax:
Let us convert this syntax into command:
Verify the list of branch on local and remote repo:
Step-4: Reset the upstream branch for the new-name local branch
For every branch that is up to date or successfully pushed, add upstream (tracking) reference:
Let us execute this command on our workstation:
And you are all set to use the new branch name.
Summary
Further Readings
You can read more about git branch at official git branch documentation.
Related Posts
Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.
For any other feedbacks or questions you can either use the comments section or contact me form.
Thank You for your support!!
How To Rename Git Remote Branchs and Local Branches-DecodingDevOps
whatever the procedure we are using to rename the git branches, we use the same procedure for github or bitbucket or gitlab. in the following steps i will show you how to rename git remote branch and how to rename git local branch. so now you can use below commands in all cases like git bitbucket, gitlab.
Git Rename Local Branchs
If It Is A Current Branch:
If you want to change the current branch which you are in right now you, can use below command. This command will rename your current local branch but not remote branch.
If It Is Another Branch:
if you are in one branch and if you want to change another branch you can use above command. This command will change the branch name in locally not in remote.
Git Rename Remote Branch
Using the above commands, we have changed the branch name in local. So now we have to change this name in the remote.
for that, you need to follow three steps. with these three steps you can rename git remote branch.
Now pull your changes
Now you have all the files
Now you can delete the old branch from the remote.
Delete The Old Branch In Remote:
here you are deleting the old branch from the remote and no need to fear that you are deleting a branch since already you have that branch in locally with a different name. just you need to push our locally renamed new branch.
Push New Branch and Track With Remote
it will push your newly renamed branch and all your files to remote. and both remote and local will be in track. now we have completed the git remote branch
now we have successfully renamed the git branch locally and remotely.
lttlrck / gist:9628955
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
alienr commented Nov 4, 2019
hanscode44 commented Nov 7, 2019
lttlrck commented Nov 7, 2019
I can’t believe how many people this has helped, it’s epic 🙏
hanscode44 commented Nov 8, 2019
I can’t believe how many people this has helped, it’s epic 🙏
Yeah really! It’s amazing how a couple of simple lines of code can bring so much fun 😉
bhatrajiv7619 commented Dec 27, 2019 •
This works like charm provided there are atleast 2 branchs. In my sample scenario, i had only one branch named «Dev». I was trying to change it to «master».
kamalsomu commented Jan 13, 2020
StaroKep commented Jan 16, 2020
Oh la la! Thanks 🙏
matchbox10 commented Jan 27, 2020
YimmyRengifoLaHaus commented Mar 10, 2020
andresdhn commented Apr 30, 2020
razziemix commented Jun 10, 2020
vjpoint commented Jul 27, 2020
BwayCarl commented Aug 1, 2020
This worked perfectly. Thank you.
DEARaison commented Sep 24, 2020
It worked perfectly. Thank you.
shravan-prajapati commented Oct 7, 2020
Thanks It is working.
jogaec22 commented Oct 14, 2020
Thanks, it helped me!
DmMhs commented Oct 15, 2020
@chrisivens you’ve saved my day. Thanks
murtajaziad commented Oct 16, 2020
devinrhode2 commented Nov 5, 2020 •
I have a better alias, that only requires you to specify the new branch name you want.
It tries to delete your current branch remotely first, but continues on if this fails. Then renames it locally, and then pushes the newly named branch.
It may be problematic if you have stuff on your remote branch that you don’t have locally, but, that’s extremely unlikely, although you may need to dig into git reflog to find them in that edge case. If you want to preserve your old/current remote branch name, don’t use this.
How to Rename a Local or Remote Branch in Git
Git is a version control system that software developers use to keep track of changes in their applications and collaborate with others.
One helpful feature of Git is branches. Different people working on a software project can work in different branches before merging their changes with the original code.
You can also add new features and fix bugs on a different branch without affecting the original code.
Sometimes, you might want to change the name of a branch because of typos or other errors, and that’s what I’m going to show you how to do in this guide.
How to Rename a Local Git Branch
You can see that the branch has been renamed from mistake-fixes to bug-fixes
How to Rename a Remote Git Branch
Renaming a remote branch is not as straightforward as renaming a local branch.
To be precise, renaming a remote branch is not direct – you have to delete the old remote branch name and then push a new branch name to the repo.
Follow the steps below to rename a remote git branch:
To confirm that you successfully renamed the remote repo, log into your client’s website and check the repo.
In the case of this tutorial, I’m using Github as the client and the renaming was successful:
Conclusion
Branches are an awesome feature of Git that make your hosted software project safer.
Oftentimes, renaming branches locally and remotely might be inevitable, so that’s why I wrote this article to help you rename your branches without costly errors.
If you find this article helpful, don’t hesitate to share it with your friends and family.
Thank you for reading.
Web developer and technical writer focusing on frontend technologies.
If you read this far, tweet to the author to show them you care. Tweet a thanks
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started
freeCodeCamp is a donor-supported tax-exempt 501(c)(3) nonprofit organization (United States Federal Tax Identification Number: 82-0779546)
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.