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

How can i rename a local git branch?

7 个月前提问
3 个月前修改
浏览次数55

6个答案

1
2
3
4
5
6

如何重命名本地 Git 分支?,要在 Git 中重命名本地分支,您可以使用以下命令:

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

这里的 <old-name> 是当前分支的名称,<new-name> 是您想要的新分支名称。

例如,如果您的分支当前名称为 feature-x 并且您想将其重命名为 feature-y,则可以这样做:

bash
git branch -m feature-x feature-y

请确保在重命名分支之前,您已经检出到了想要重命名的分支上:

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

如果您尝试重命名当前没有检出的分支,您需要使用以下命令:

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

如果您已经将旧分支推送到远程仓库,并且也想要重命名远程分支,那么您需要先删除旧的远程分支,然后推送新的分支名称,并重新设置上游分支:

  1. 删除远程的旧分支:
bash
git push origin --delete <old-name>
  1. 推送新的分支到远程,并设置上游分支:
bash
git push origin -u <new-name>

请记得,在推送新的分支名称到远程仓库之前,确保没有其他团队成员正在使用旧的分支,因为这将影响所有使用该分支的人。最好是先与团队沟通,确保大家都知道分支名称的变更。

2024年6月29日 12:07 回复

To rename the current branch:

shell
git branch -m <newname>

To rename a branch while pointed to any branch:

shell
git branch -m <oldname> <newname>

-m is short for --move.


To push the local branch and reset the upstream branch:

shell
git push origin -u <newname>

To delete the remote branch:

shell
git push origin --delete <oldname>

To create a git rename alias:

shell
git config --global alias.rename 'branch -m'

On Windows or another case-insensitive filesystem, use -M if there are only capitalization changes in the name. Otherwise, Git will throw a "branch already exists" error.

shell
git branch -M <newname>
2024年6月29日 12:07 回复

You can rename a local Git branch using the following command:

shell
git branch -m old_branch_name new_branch_name

Keep in mind that when you rename a branch, it still maintains its association with the old upstream branch if there was one.

To push changes to the master branch after renaming your local branch to new_branch_name, use the following command:

shell
git push origin new_branch_name:master

With this command, your changes will be pushed to the master branch on the remote repository. However, your local branch will still be named new_branch_name.


For more details, see: How to rename your local branch name in Git.

2024年6月29日 12:07 回复

To rename your current branch:

shell
git branch -m <newname>
2024年6月29日 12:07 回复

Here are the steps to rename the branch:

  1. Switch to the branch which needs to be renamed
  2. git branch -m <new_name>
  3. git push origin :<old_name>
  4. git push origin <new_name>:refs/heads/<new_name>

EDIT (12/01/2017): Make sure you run command git status and check that the newly created branch is pointing to its own ref and not the older one. If you find the reference to the older branch, you need to unset the upstream using:

shell
git branch --unset-upstream
2024年6月29日 12:07 回复

Rename the branch will be useful once your branch is finished. Then new stuff is coming, and you want to develop in the same branch instead of deleting it and create the new one.

From my experience, to rename a local and remote branch in Git you should do the following steps.

Quoting from Multiple States - Rename a local and remote branch in git

1. Rename your local branch

If you are on the branch you want to rename:

shell
git branch -m new-name

If you are on a different branch:

shell
git branch -m old-name new-name

2. Delete the old-name remote branch and push the new-name local branch

shell
git push origin :old-name new-name

3. Reset the upstream branch for the new-name local branch

shell
git push origin -u new-name
2024年6月29日 12:07 回复

你的答案