1. Introduction

Maintaining a clean and organized codebase is essential when using Git. Therefore, an appropriate nomenclature for the branches is crucial.

In this sense, a recurring task is renaming branches, to provide a more representative name, correct typos, or for other reasons. In this tutorial, we’ll explore how to rename a local Git branch.

First, we’ll see how branch renaming works in Git. Next, we’ll examine the steps to renaming the current local branch and how to rename a branch other than the current one. Finally, we’ll learn how to create an alias for the original command.

2. How Branch Renaming Works in Git

In Git, the git branch command offers a range of features for managing branches. We can also use the git branch command in combination with the -m flag to rename branches.

Here, the -m flag refers to “move” (we can even use the –move flag for the same purpose). Thus, it’s as if we’re moving the branch to the same location and with a different name.

In practice, this command renames a branch and then updates the corresponding reflog to keep track of the changes.

3. Rename the Current Local Branch

In short, we can rename the current local branch via the git branch -m command using the syntax below:

$ git branch -m <new_name>

Of course, we should replace the expression <new_name> with the actual new name of the branch.

Now, let’s look at an example to understand better. Suppose our current branch is called “my-branch” and we want to rename it to “baeldung-branch”.

The first step is to check which branch we’re currently on:

$ git branch
  main
* my-branch

The output of the above command lists all the local branches, with the branch marked * being our current branch. Once we’re on the right branch (“my-branch“, in this case), we can rename it:

$ git branch -m baeldung-branch

At this point, the branch will have already been renamed locally. We can check this by running the git branch command again:

$ git branch
* baeldung-branch
  main

4. Rename Another Local Branch (Without Checking Out)

Sometimes, we’re working on a particular branch and, for some reason, we need to rename another branch. In these cases, we don’t want to have to check out this other branch to rename it (which is the method we saw in the previous section). Instead, we’d prefer a method that allows us to rename the other branch without having to save or undo the work on the current branch.

The solution to this situation is to use the command git branch -m, but use the current name of the branch we want to rename followed by the new name:

$ git branch -m <current_name> <new_name>

To exemplify, let’s consider the scenario shown in the output of the git branch command below:

$ git branch
* main
  my-branch

In this case, we’re currently on the branch “main” and we want to rename the branch “my-branch” without checking it out:

$ git branch -m my-branch baeldung-branch

As a result, the command above locally renames the branch “my-branch” to “baeldung-branch“. We can confirm this using the git branch command:

$ git branch
  baeldung-branch
* main

Now, although we’re still on the branch “main” (see the * marker), the other branch has been renamed as desired.

5. Setting an Alias in Git to Rename Branches

To simplify the task of renaming branches, we can create an alias to the command line via git config:

$ git config --global alias.rename 'branch -m' 

Using the above command, we create a global alias called rename. To restrict the alias to a single repository, we just run the command within the desired repository without the –global option inserted.

The last part, ‘branch -m’, is the original command that will be replaced by the alias. When we use the rename alias, Git interprets it as if we’re typing git branch -m, simplifying the renaming process.

As a result, whenever we need to rename a local branch, all we’ve to do is type:

$ git rename <new_name>

6. Conclusion

In this article, we’ve studied how to rename local Git branches. First, we’ve learned that renaming branches in Git works as if we’re moving the branch to the same location with a different name.

As we’ve seen, the basis for renaming branches is the git branch -m command, in which the -m flag refers to “move”. Therefore, to rename the current branch, we only need to add the new branch name at the end of the git branch -m command. However, to rename a branch other than the current one, we need to add the current name of the branch to be renamed and then the new branch name.