In this tutorial, How to rename a branch in Git. How to rename both local and remote git branches.
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 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
How to 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
You have renamed both local and remote git branches. I hope will this your helpful. Thank you for reading the DevopsRoles page!