更改 Git 存储库中的分支名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3866951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:13:26  来源:igfitidea点击:

Change a branch name in a Git repo

gitgit-branch

提问by Alex

How do I rename an existing branch in a Git repo?

如何重命名 Git 存储库中的现有分支?

I want the current branch to have a new name.

我希望当前分支有一个新名称。

回答by Richard Fearn

Assuming you're currently on the branch you want to rename:

假设您当前在要重命名的分支上:

git branch -m newname

This is documented in the manual for git-branch, which you can view using

这记录在手册中git-branch,您可以使用

man git-branch

or

或者

git help branch

Specifically, the command is

具体来说,命令是

git branch (-m | -M) [<oldbranch>] <newbranch>

where the parameters are:

其中参数是:

   <oldbranch>
       The name of an existing branch to rename.

   <newbranch>
       The new name for an existing branch. The same restrictions as for <branchname> apply.

<oldbranch>is optional, if you want to rename the current branch.

<oldbranch>是可选的,如果要重命名当前分支。

回答by javierdvalle

If you're currently on the branch you want to rename:

如果您当前在要重命名的分支上:

git branch -m new_name 

Or else:

要不然:

git branch -m old_name new_name 

You can check with:

您可以通过以下方式检查:

git branch -a

As you can see, only the local name changed Now, to change the name also in the remote you must do:

如您所见,现在只更改了本地名称,要更改远程名称,您必须执行以下操作:

git push origin :old_name

This removes the branch, then upload it with the new name:

这将删除分支,然后使用新名称上传它:

git push origin new_name

Source: https://web.archive.org/web/20150929104013/http://blog.changecong.com:80/2012/10/rename-a-remote-branch-on-github

来源:https: //web.archive.org/web/20150929104013/http: //blog.changecong.com: 80/2012/10/ rename-a-remote-branch-on-github