git rename local & remote branch

Git Rename Local & Remote Branch

Sometimes you may want to change the name of your git branches. You can easily do this using git branch command. In this article, we will learn how to rename local & remote branches in git.


Git Rename Local & Remote Branch

Here are the steps to rename local & remote branch.


1. Rename local git branch

Let us say you want to rename branch data to new_data. First, navigate to your git repository. Then checkout data branch with the following command.

$ git checkout data

Next rename data branch using git rename command, which has the following syntax.

$ git branch -m new_data

The above command will rename your old branch to new branch.

Alternatively, you can switch to master branch.

$ git checkout master

Then you can rename branch data to new_data with the following command.

$ git branch -m data new_data

Rename data and new_data in the above commands with your old and new branch names.

You can verify the branch names with the following command.

$ git branch -a


2. Rename Remote Git Branch

Please note, if you have a remote branch corresponding to your local branch, then when you rename the local branch, you need to also rename its remote counterpart. Otherwise, it can lead to errors and confusion later on when you push/pull commits. However, there is no direct way to rename a remote branch. We need to delete the remote branch with old name and then push the local branch with new name.

Run the following command to delete the remote branch data.

$ git push origin --delete data

Next, push your local branch new_data to remote repository, and reset the upstream branch.

$ git push origin -u new_data

Alternatively, you can overwrite remote branch with the following command.

$ git push origin :data new_data

In this case also, you will need to reset upstream branch.

$ git push origin -u new-name

That’s it. In this article, we have learnt how to change name of local branch as well as remote branch in git.

Also read:

How to Create Remote Git Branch
How to Unstage Files in Git
How to Enable Bluetooth from Command Line in Ubuntu
How to Add Repository in Ubuntu
How to Delete Folders Older Than 7 Days in Linux

Leave a Reply

Your email address will not be published.