Introduction
This tutorial will show you how to rename a local and a remote(hosted on Github , Gitlab )
New to
git
& git branching? Please go through this tutorial from Attlasian
1. Renaming a local branch
To rename a git
branch:
- Switch to the branch to be renamed:
#inside your repository:
$ git checkout main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
- Use
-m
flag to modify the branch name:
#inside your repository:
$ git branch -m old-name your-new-branch-name
2. Rename a remote branch
This might lead to complications instead, we
- delete the old branch name
- add the new branch with a new name
To rename a remote branch:
- Ensure the local branch has the correct new name(from above step above):
$ git branch -a
* development
main
remotes/origin/HEAD -> origin/main
remotes/origin/main
- Delete and set the new branch with the incorrect name in in the remote repository:
git push origin -u <new-name>
- Finally, delete the old remote branch:
git push origin --delete <old-branch-name>
Note: the <new-name>
should be exactly same as the new name you set for the local branch