Rename local and remote Git branches
While working on a project, if you feel that the branch name is not suitable for the branch you are working on and want to rename the branch, there are several ways to rename the branch depending on the scenario you are in. In this article, we will look at how to rename a branch in your local machine or if the branch is located in a remote repository.
Rename the currently active Git local branch
The syntax to rename the currently active branch is as follows.
git branch -m <New_Branch_Name>
For example, if we are currently working on a branch name bug-fix
and need to change the name to bug-fix-1
, we can rename the branch using the following syntax.
git branch -m bug-fix-1
However, with this approach, if we push changes to the remote repository, a new branch will be created containing the changes, and the old branch will remain as it is.
Rename an inactive Git branch
If we master
are working in branch and need to bug-fix
rename another branch named to bug-fix-1
, we can follow the following syntax.
git branch -m <Old_Branch_Name> <New_Branch_Name>
For example,
git branch -m bug-fix bug-fix-1
git config --global alias.rename 'branch -m'
Now, after setting up the alias, we can rename the git branch using the following syntax.
git rename <New_Name> # If Renaming Currently Active Branch
or,
git rename <Old_Name> <New_Name> # If Renaming a Not Active Branch
Rename Git remote branch
If we want to rename a remote branch, then we need to follow three steps instead.
-
Rename the branch name locally.
git branch -m <New_Branch_Name> # OR git branch -m <Old_Branch_Name> <New_Branch_Name>
-
Push changes to branch name to the remote repository.
git push origin :<Old_Branch_Name> <New_Branch_Name>
Note:
<Old_Branch_Name>
The leading colon (:
) should not be missed. -
Set up a local branch to track a remote repository with the same name. To do this, we need to use the following syntax.
git push --set-upstream origin <New_Branch_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
Changing drives in Git Bash
Publish Date:2025/03/30 Views:56 Category:Git
-
This short article will discuss how we can use Git Bash to have a Unix-style command line environment in Windows operating system and run multiple commands in this terminal. Git Bash Git is a collection of command-line utilities created to
Adding a remote branch in Git
Publish Date:2025/03/30 Views:142 Category:Git
-
Git does not allow its developers to create new branches on remote repositories. But instead, we can push an already existing local branch, and after doing so, we can bring it to the remote repository using some Git commands. In every versi
Synchronize your local repository with a remote repository in Git
Publish Date:2025/03/30 Views:92 Category:Git
-
This article outlines the process of syncing your local repository with a remote repository. We will also see how to sync a GitHub branch with a remote repository on the command line. Synchronize your local repository with a remote reposito
Creating a remote repository from a local repository in Git
Publish Date:2025/03/30 Views:105 Category:Git
-
This article discusses the necessary steps to create a remote repository based on a local repository. This is ideal when you have a local repository that needs to be available on a remote or SSH-enabled server. Creating a remote repository
Removing the upstream repository in Git
Publish Date:2025/03/30 Views:177 Category:Git
-
This article will teach you how to delete an upstream repository in Git. We may sometimes need to delete or change the remote repository that we use with our local repository. To do this, we can use the Git command git remote . Removing the
Git remote add SSH
Publish Date:2025/03/30 Views:53 Category:Git
-
In this day and age, the most widely used version control system is Git, which is operated by most developers within a team structure. This is mainly used to increase code efficiency, no matter how big or critical the project is. In this se
Differences between Git Reset, Revert and Checkout commands
Publish Date:2025/03/30 Views:185 Category:Git
-
This article discusses the differences between the git reset , , git revert and git checkout commands. These are some of the most useful Git utilities that allow us to undo certain changes in our repository. It’s easy to get confused with
Git revert local commits
Publish Date:2025/03/30 Views:84 Category:Git
-
When a software engineer or a web developer uses Git, it is obvious that he pushes a lot of code and commits to the Git repository every day, and while doing so, the need to undo or revert a particular commit or a set of commits arises from
Deleting local and remote Git branches
Publish Date:2025/03/30 Views:146 Category:Git
-
Git comes into play in version control systems when you are working with a team and the entire team is making changes to the same code repository. Creating a new branch in Git is relatively easy than other version control systems and deleti