How can I rename a local Git branch?
Learn how to rename a local Git branch using simple Git commands to keep your repository organized and well-maintained.
Renaming a local Git branch is a common task when working with version control. Whether you've made a typo or want a name that better reflects your branch’s purpose, Git provides an easy way to rename branches. This guide will walk you through the steps needed to rename a local Git branch without disrupting your workflow.
When managing a Git repository, it's not uncommon to find yourself needing to rename a branch. Fortunately, Git makes this task simple with just a few commands.
If you are on the branch you want to rename, use the following command:
The -m
flag stands for “move,” which renames the branch. You replace new-branch-name
with whatever name you want for your branch.
If you're not currently on the branch you want to rename, first make sure you're on a different branch (e.g., main
), then use:
This command allows you to rename a branch while not being on it.
After renaming your local branch, you'll want to push it to the remote repository. Use:
This command uploads your renamed branch to the remote. Git will treat it as a new branch, so you may need to set the upstream branch:
If the old branch was already pushed to a remote repository, you should delete the old branch from the remote to avoid confusion:
This ensures that the old branch is removed from the remote repository, keeping your remote branches clean and up-to-date.
Renaming a local Git branch is a straightforward process that helps maintain a clean and organized repository. By following the steps above, you can easily rename branches, push them to the remote repository, and delete the old branches as needed.
Remember, consistent and meaningful branch names improve collaboration and make it easier to navigate your codebase.