How to Change Name of Git Branch
Managing a project with Git often requires changing the names of branches to reflect the project’s evolving stages or to avoid conflicts. Whether you want to rename a branch to better describe its purpose or to consolidate multiple branches, here’s a step-by-step guide on how to change the name of a Git branch.
Step 1: Check Current Branch Name
Before renaming the branch, it’s essential to confirm the current branch name. You can do this by running the following command in your terminal or command prompt:
“`
git branch
“`
This command will list all branches in your local repository, along with the currently active branch, which is marked with an asterisk ().
Step 2: Rename the Branch
To rename a branch, use the `git branch -m` command, followed by the new branch name. For example, if you want to rename a branch called `old-branch-name` to `new-branch-name`, run:
“`
git branch -m new-branch-name
“`
This command will rename the branch in your local repository but won’t affect the remote repository until you push the changes.
Step 3: Push the New Branch Name to Remote Repository
After renaming the branch locally, you need to push the new branch name to the remote repository to ensure that the remote branch is also updated. Use the following command:
“`
git push origin :old-branch-name
“`
This command deletes the old branch on the remote repository and pushes the new branch name. If you want to create a new remote branch instead of deleting the old one, use the following command:
“`
git push origin new-branch-name
“`
Step 4: Update Local Branch Tracking
If you want to keep the local branch tracking the new remote branch name, use the `git branch -u` command:
“`
git branch -u origin/new-branch-name
“`
This command sets the local branch to track the specified remote branch name.
Step 5: Verify the Branch Rename
To confirm that the branch has been renamed successfully, you can check the branch list again using the `git branch` command. You should see the new branch name listed.
By following these steps, you can easily change the name of a Git branch, ensuring that your project’s branching strategy remains clear and organized.
