乐闻世界logo
搜索文章和话题

How can i rename a local git branch?

1个答案

1

How to rename a local Git branch? To rename a local Git branch, you can use the following command:

bash
git branch -m <old-name> <new-name>

Here, <old-name> represents the current branch name, and <new-name> is the new branch name you intend to use.

For example, if your branch is currently named feature-x and you want to rename it to feature-y, you can execute:

bash
git branch -m feature-x feature-y

Before renaming the branch, ensure you have checked out the branch you wish to rename:

bash
git checkout feature-x git branch -m feature-x feature-y

If you attempt to rename a branch that is not currently checked out, use the following command:

bash
git branch -m <old-name> <new-name>

If you have already pushed the old branch to the remote repository and also want to rename the remote branch, first delete the old remote branch, then push the new branch name, and reset the upstream branch:

  1. Delete the old remote branch:
bash
git push origin --delete <old-name>
  1. Push the new branch to the remote and set the upstream branch:
bash
git push origin -u <new-name>

Please note that before pushing the new branch name to the remote repository, verify that no other team members are actively using the old branch, as this change will affect all users of the branch. It is advisable to communicate with your team in advance to ensure everyone is aware of the branch name update.

2024年6月29日 12:07 回复

你的答案