Sangram Barge

How to Rename Branch in Git

April 30, 2024

Even in the world of coding, small changes can make a big difference. Renaming branches in Git might seem like a tiny tweak, but it’s a handy skill that can keep your projects neat and tidy. Here’s a straightforward guide to help you rename branches without any hassle.

Find Your Branch: Start by making sure you’re not currently working on the branch you want to rename. Use git checkout to switch to a different branch if needed. Rename Locally: Use the command

git branch -m <oldname> <newname>

to rename the branch on your local machine. For example, if you want to change “feature-123” to “feature-xyz,” type

git branch -m feature-123 feature-xyz.

Sync with the Remote: Once you’ve renamed the branch locally, sync your changes with the remote repository by typing

git push origin <new_name>. This updates the branch name in the remote repository.

Tidy Up (Optional): If you want to delete the old branch from the remote repository, use

git push origin —delete <old_name>. Stay on Track (Optional): If the renamed branch was being tracked locally, update the tracking reference with

git branch -u origin/<new_name>.

By mastering this simple process, you can keep your Git repository organized and make collaboration with your team smoother.


© 2024 Made with ❤️ in India. Twitter