How to Rename a Local or Remote Branch in Git
source link: https://www.freecodecamp.org/news/how-to-rename-a-local-or-remote-branch-in-git/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
March 10, 2022 / #Git
How to Rename a Local or Remote Branch in Git
Git is a version control system that software developers use to keep track of changes in their applications and collaborate with others.
One helpful feature of Git is branches. Different people working on a software project can work in different branches before merging their changes with the original code.
You can also add new features and fix bugs on a different branch without affecting the original code.
Sometimes, you might want to change the name of a branch because of typos or other errors, and that’s what I’m going to show you how to do in this guide.
How to Rename a Local Git Branch
Step 1: To see the branches you have, run git branch --list
or git branch -a
Step 2: Switch to the branch you want to rename by running git checkout branch-name
.
In this case, I’m going to switch to the mistake-fixes
branch so I can rename it bug-fixes
.
To switch to a branch, run git switch branch-name
or git checkout branch-name
.
Step 3: To rename the branch, run git branch -m new-name
You can see that the branch has been renamed from mistake-fixes
to bug-fixes
If you are on another branch, for example, main and you want to rename the branch from there, run git branch -m old-name new-name
N.B.: Make sure you verify that the branch has been renamed by running git branch -a
to see all branches.
How to Rename a Remote Git Branch
Renaming a remote branch is not as straightforward as renaming a local branch.
To be precise, renaming a remote branch is not direct – you have to delete the old remote branch name and then push a new branch name to the repo.
Follow the steps below to rename a remote git branch:
Step 1: Delete the old name by running git push origin --delete old-branch-name
In the example I’ve been using, this would be git push origin --delete mistake-fixes
Step 2: Reset the upstream branch to the name of your new local branch by running git push origin -u new-branch-name
.
So, for the example, this would be git push origin -u bug-fixes
To confirm that you successfully renamed the remote repo, log into your client's website and check the repo.
In the case of this tutorial, I’m using Github as the client and the renaming was successful:
Conclusion
Branches are an awesome feature of Git that make your hosted software project safer.
Oftentimes, renaming branches locally and remotely might be inevitable, so that’s why I wrote this article to help you rename your branches without costly errors.
If you find this article helpful, don’t hesitate to share it with your friends and family.
Thank you for reading.
Web developer and technical writer focusing on frontend technologies.
If you read this far, tweet to the author to show them you care.
Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK