How To Rename A Git Branch: Local & Remote

Git is an extremely popular open-source version control system that might look complicated, but once you get the hang of it, it will make your life very easy. In this guide, we will cover the basics of how to rename your local and remote git branches. Read on!

Related: How To Overwrite Local Git Branches With Remote

How To Overwrite Local branch with Remote In Git
Git is one of the most popular version control systems out there. It is free and open-source and allows you to do a number of complex actions to streamline your workflow in order to create an easy-to-collaborate-on project. One of the most common issues that people face when trying to

How To Rename a Local Git Branch

Renaming a local git branch is easy and you can do it in as little as one command!

#1. Navigate to the branch you want to rename. You can do this by using the following command:

$ git checkout <branch_name>

#2. Now, you can rename your original branch:

$ git branch -m <new_name>

If you are on a different branch, you can instead use a different command.

$ git branch -m <old_name> <new_name>

That's all! At this point, you should have successfully renamed your local git branch.

You can verify the names of your local branches with the command -  git branch, which will show you a list of all your remote branches.

How To Rename a Remote Git Branch

Once you've followed the steps outlined above and deleted your local git branch, you might want to rename its remote copy as well (if you have already pushed the original branch). There is no direct command to this. Instead, you will have to delete the original remote branch and then push the renamed local one. Here's how.

#1. First, you will have to delete the remote git branch. To do this, type one of the following into the terminal:

$ git push <remote> --delete <branch_name>
$ git push <remote> :<branch_name>

Here, <remote> is the name of your remote repository. In most cases, it tends to be origin.

#2. Now, you can push the renamed branch to <origin> and ask your new local branch to track your new remote branch.

git push origin -u <new_name>

And you're done!