Rename Git Branch – Locally & Remotely

Rename Git Branch Locally & Remotely

This is a quick tutorial on how you can rename a git branch locally as well as remotely. First, you have to rename it locally and the have to push the changes to the remote repository.

Renaming Local Git Branch

If you want to rename the current local branch, use the following git branch command.

git branch -m <newName>

In case you needed to rename some other local branch than the current local branch, you just need to explicitly define the current name of the branch to be renamed. The exact command is defined below.

git branch -m <oldName> <newName>

The execution of both of the above commands for renaming the local git branches is shown in the following screenshot.

Git Branch Rename

Here, you can memorize the -m option as the mv command which we use in the Unix operating systems either to move files from one location to another or simply to rename the files.

Note. In case you’re on a case-insensitive file system such as on Windows, then if you are making changes in the name of the local git branch such that the changes are case-sensitive, then you must use the option -M instead of -m. Doing case-sensitive changes using the option -m on case-insensitive file systems will give you the branch already exists error.

You can clearly see in the following screenshot. I run the rename commands with option -m and -M on a Windows File System to make case-sensitive changes and you can see the command with -m option gives the same branch already exists error while the command with -M works pretty well.

Git Rename Branch Case Sensitive Changes

Push Rename Changes To Remote Repository

Make sure you make the rename changes locally first using the command(s) specified in the above section. Now on the remote repository, you’ve to first delete the branch with the old name and then have to push the branch with the new name.

Run the following command to delete the old-name branch also to push the new-name branch.

git push origin :old-name new-name

Now, for the new name local branch, you can reset the upstream branch. First, switch to the branch and then run the following command.

git push origin -u new-name

Related Articles.

Leave a Reply

Your email address will not be published. Required fields are marked *