How to rename a local branch in Git
Git is a widely known, in-demand, and popular version control system (VCS) that is commonly used by software developers to collaborate on code development in small or large teams. It helps us coordinate our work and helps us track the changes in the source code during the software development process.
Git branches play a very important and unique role in developing our projects in a team environment. Branches provide us a track to work with our main (master) branch while keeping it free of clutter and incomplete code in a separate branch.
Sometimes we are in a hurry when developing branches and come across a situation where we have named a branch incorrectly, which does not match our code or does not describe it accurately and looks inaccurate. Then, we want to rename the branch.
Most of the time, renaming branches is due to the above mentioned situations. So, in this article, we will learn to change the name of a local Git branch using the techniques mentioned below.
Rename a local branch in Git
In Git, if we want to rename a branch, we will follow the steps mentioned below to achieve our purpose.
-
Before we begin, we should make sure we have selected the branch we want to rename by entering the branch name by using the following command.
$git checkout old-name
-
After selecting the correct branch, we will use the command aliased as -m
git branch
.
Alternatively, we can also use a single command. If we are not already present in master, we will switch to it.$ git branch -m new-name
$ git checkout master
- After running the checkout command
above , we will run the following command to change the branch name.
Now, our branch name has been changed.$ git branch -m old-name new-name
-
To verify that the name has changed, we will recheck it by running the following command.
If we wanted to see the correct names of all our local branches, we would run the following command.$ git branch -a
$ git branch --list
From the above Git commands, we have seen that we can easily change our local branch name from old to new without any problem.
If you ever find yourself in a situation where you need to change the name of your local branch for any reason, you can follow the above steps to successfully change the name.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Setting Upstream in Git
Publish Date:2025/03/26 Views:151 Category:Git
-
In this article, we will learn how to use in Git upstream . When using Git by cloning and creating new repositories in branches, we must set up upstream branches for future commits and fetches. But first, we should understand what is upstre
Add Git to PATH on Windows
Publish Date:2025/03/26 Views:91 Category:Git
-
Git is a free and open source version control system designed to work with projects quickly and efficiently. You can use it on Windows, Mac, and Linux operating systems. This article explains how to add git to your Windows PATH environment
Open a file in Git Bash
Publish Date:2025/03/26 Views:67 Category:Git
-
We cannot use open in Git Bash. If you try to open a file using open on Git Bash, you will get the error bash: open: command not found . This short article explains how to open a file on Git Bash for Windows. Open a file in Git Bash We can
Creating Git patches from uncommitted changes
Publish Date:2025/03/26 Views:90 Category:Git
-
This article explains how we can create a Git patch from uncommitted changes in our working directory. This is handy when we want to create a patch without creating a commit. Creating Git patches from uncommitted changes We will use an exam
How to uninstall git in Windows
Publish Date:2025/03/26 Views:187 Category:Git
-
Git for Windows is a well-known and user-friendly software that provides a smooth platform to easily use Git in a team-friendly environment. Sometimes we want to get rid of some software because of space reasons, or the software is not comp
How to check the Git version
Publish Date:2025/03/26 Views:129 Category:Git
-
Git is primarily known as a unique free source code distributed version control system that can easily store all kinds of projects, big or small. Any new developer or someone who is new to it can understand it quickly and easily. We can ins
Staging deleted files in Git
Publish Date:2025/03/26 Views:191 Category:Git
-
This article discusses the necessary steps to stage deleted files in Git. We know that the rm command can delete a file without removing it from the working directory. So, how do we stage the deleted files for commit? Staging deleted files
Git 拉取和 Git 克隆的区别
Publish Date:2024/02/04 Views:223 Category:Git
-
这篇简短的文章将解决 git clone 和 git pull 的区别。本教程将讨论 git clone 和 git pull 命令之间的区别。