Table of Contents
Introduction
In this tutorial, How to git rename branch in Git. How to rename both local and remote git branches. Branches are a powerful feature in Git that allows developers to work on multiple features or experiments concurrently.
However, there may be situations where you need to rename a branch for clarity or consistency. In this guide, we’ll walk you through the steps to rename a branch in Git.
How to use git rename branch
Rename a Local Branch in Git
we can find out the local branches.
$ git branch
$ git branch -a # The -a option lists the remote branches.
Check the local Branch
$ git checkout <old-branch-name>
$ git checkout oldbranch
Rename the Local Branch
we have switched to the desired branch. you can rename the local branch as the command follows
$ git branch -m <new-branch-name>
$ git branch -m newbranch
This command changes the name of the local branch oldbranch to newbranch
You can also rename a local branch from inside another git branch
$ git branch -m <old-branch-name> <new-branch-name>
$ git branch -m oldbranch newbranch
Check the New Branch Name
$ git branch -a
Rename a Remote Branch in Git
- You need first to rename the local branch
- push the new branch to the server
- delete the old branch from your repository.
Step 1. Rename the Local Branch
$ git branch -m newbranch
# or
$ git branch -m oldbranch newbranch
Step 2: Push the Updated Branch
Push the renamed branch newbranch to the remote server
$ git push origin <new-branch-name>
$ git push origin newbranch
Set the Upstream
Set up tracking between the local branch newbranch and the remote branch newbranch.
$ git push origin -u <new-branch-name>
$ git push origin -u newbranch
Step 3: Remove the Old Branch
$ git push origin --delete <old-branch-name>
$ git push origin --delete oldbranch
Conclusion
You have successfully renamed both your local and remote Git branches. Git rename branch is a straightforward process that enhances the clarity and consistency of your project’s branch structure.
It’s important to note that renaming a branch changes only its name without affecting the commit history or the contents of the branch. If other developers are working on this branch, make sure to inform them about the name change to facilitate smooth collaboration.
Thanks to its flexibility and robust branch management features, Git remains a vital tool for version control and collaborative development. For more tips like how to rename a branch, stay tuned.
Thank you for visiting DevOpsRoles!