如何重命名本地Git分支?
我不想重命名远程分支,如本地和远程Git存储库的重命名主分支中所述.
如何重命名尚未推送到远程分支的本地分支?
如果您还需要重命名远程分支:
如何重命名远程git分支名称
回答
如果要在指向任何分支时重命名分支,请执行以下操作:
git branch -m <oldname> <newname>
如果要重命名当前分支,可以执行以下操作:
git branch -m <newname>
记住这个的方法-m
是"移动"(或mv
),这是你重命名文件的方式.
- @PandaWood:它会在你推送时添加新分支,但不会删除旧分支.如果使用`git push -f --mirror`,它将重命名远程分支,但是如果远程只是当前存储库的副本,则只应使用此方法.另见这个问题:http://stackoverflow.com/questions/1526794/git-rename-remote-branch
- 我真正想知道的是,当你推/推时,这是否一定会影响远程分支
- `-m`选项的长名称是`--move`,例如``git branch --move master`将当前分支重命名为"master".
- @PandaWood,这取决于`push.default`的配置方式.默认情况下(`matching`)它将推送到名称匹配的遥控器.您必须执行`git push origin <newname>:<oldname>`或者您将创建一个新的远程分支.但是,如果`push.default`设置为`upstream`,那么你可以`推送原始头`,而东西将转到遥控器上的oldname.
- @ NightOwl888:-m可能是"move"的缩写,遵循使用`mv`重命名文件的Unix惯例.原因是在基于目录的inode文件系统中移动和重命名完全等效.
- 对于那些偶然发现这个帖子和评论的人,在考虑使用`git push -f --mirror`时要小心.这也将*所有*本地分支也推送到远程,包括任何未经修改的分支.我做了这个,并将30多个旧分支推向远程......哎呀!
- 也是**非常谨慎的`git push -f --mirror`,因为它会完全"镜像"你的本地回购.如果您单独工作,这可能没问题,但是如果您使用团队贡献代码库,那么`git push -f --mirror`将删除不在您本地的任何分支.
- 执行此操作后,还应运行`git push origin:<old_name> <new_name>`来更新远程分支名称.
- @ Sam.Rueby如果你只是改变大小写,你将需要使用-M重命名,因为git会告诉你分支已经存在.
- @ Sam.Rueby:我无法重现那个错误.这对我来说可以.我在Windows 7,64位上使用msysgit(git version 1.9.5.msysgit.0).
git branch -m old_branch_name new_branch_name
上面的命令将更改您的分支名称,但您必须非常小心地使用重命名的分支,因为它仍将引用与之关联的旧上游分支(如果有).
如果要在将本地分支重命名为new_branch_name(示例名称)后将某些更改推送到master中:
git push origin new_branch_name:master
(现在更改将转到主分支,但您的本地分支名称是new_branch_name)
有关更多详细信息,请参阅" 如何在Git中重命名本地分支名称 ".
要重命名当前分支:
git branch -m <newname>
- 如果您只是更改大小写,则需要使用-M重命名,因为git会告诉您分支已经存在.
以下是重命名分支的步骤:
git branch --unset-upstream
编辑(12/01/2017):确保运行命令git branch -m <new_name>
并检查新创建的分支是否指向自己的引用而不是旧引用.如果找到对旧分支的引用,则需要使用以下命令取消设置上游:
git branch --unset-upstream
- This is the best answer here as it describes the full process to correctly complete a rename
- 在哪一步会取消上游?在第 4 步之前?
- @Milind Anantwar, what does it mean to "check that the new branch is pointing to it's own ref"? And could you please explain how `git branch --unset-upstream` resolves the unsynchronised condition(s) to which you're referring?
分支完成后,重命名分支将非常有用.然后新的东西即将到来,你想在同一个分支中开发而不是删除它并创建新的分支.
根据我的经验,要在Git中重命名本地和远程分支,您应该执行以下步骤.
1.重命名您当地的分支机构
如果您在分支机构上,则要重命名:
git branch -m new-name
如果你在不同的分支:
git branch -m old-name new-name
2.删除旧名称远程分支并推送新名称本地分支
git push origin :old-name new-name
3.重置新名称本地分支的上游分支
git push origin -u new-name
到目前为止的答案是正确的,但这里有一些额外的信息:可以用'-m'(移动)重命名分支,但必须小心,因为'-M'强制重命名,即使存在已经有相同名称的分支.以下是'git-branch'手册页的摘录:
- 被覆盖的分支会发生什么?
1.重命名
如果它是您当前的分支,那就行了
git branch -m new_name
如果它是您要重命名的另一个分支
git branch -m old_name new_name
2.跟踪新的远程分支
- 如果您的分支被推送,则在重命名后,您需要从远程Git存储库中删除它,并要求您的新本地跟踪新的远程分支:
git push origin :old_name
git push --set-upstream origin new_name
我愚蠢地命名一个以连字符开头的分支,然后检查出主人.我不想删除我的分支,我已经在其中工作了.
这些都没有奏效:
git checkout -dumb-name
git checkout -- -dumb-name
"
s,'
s和\
s也没有帮助. git branch -m
不起作用.
这是我最终修复它的方式.进入工作副本的.git/refs/heads,找到文件名"-dumb-name",获取分支的哈希值.然后这将检查出来,创建一个具有合理名称的新分支,并删除旧分支.
git checkout {hash}
git checkout -b brilliant-name
git branch -d -- -dumb-name
- 难道你不能在refs/heads中重命名文件吗?
- 你可能已经使用过`reflog`了
要在本地重命名分支:
git branch -m [old-branch] [new-branch]
现在,您还必须在远程服务器上传播这些更改.
要推送已删除的旧分支的更改:
git push origin :[old-branch]
推动创建新分支的更改:
git push origin [new-branch]
使用以下命令重命名分支:
git branch -m [old_branch_name] [new_branch_name]
-m
:它重命名/移动分支.如果已经存在分支,则会出现错误.
如果已有分支并且您想要使用该分支重命名,请使用:
git rename -M [old_branch_name] [new_branch_name]
有关帮助的更多信息,请在终端中使用此命令:
git branch --help
要么
man git branch
高级Git用户可以手动重命名:
Rename the old branch under .git/refs/heads to the new name
Rename the old branch under .git/logs/refs/heads to the new name
Update the .git/HEAD to point to yout new branch name
重命名分支:
-m
这里-m选项的长名称是--move.所以我们也可以使用
--move
如果要重命名当前分支,请使用此分支.
git branch -m old_branchname new_branchname
要么
git branch --move old_branchname new_branchname
如果要将这些更改移动到远程,请使用以下命令.
git branch -m new_branchname
这将删除old_branchname远程分支并推送new_branchname本地分支.
git branch -move new_branchname
这将重置new_branchname本地分支的上游分支.
- 重命名您当地的分支机构.
如果您在分支机构上,则要重命名:
git branch -m new-name
如果你在不同的分支:
git branch -m old-name new-name
- 删除旧名称远程分支并推送新名称本地分支.
git push origin :old-name new-name
- 重置新名称本地分支的上游分支.切换到分支,然后:
git push origin -u new-name
或者为了快速做到这一点,您可以使用以下3个步骤:
#在本地重命名分支
git branch -m old_branch new_branch
#删除旧的远程分支
git push origin :old_branch
#推送新分支,设置本地分支以跟踪新远程
git push --set-upstream origin new_branch
Referance:https://www.w3docs.com/snippets/git/how-to-rename-git-local-and-remote-branches.html
以下是三个步骤:您可以在终端内调用并更改分支名称的命令.
git branch -m old_branch new_branch # Rename branch locally
git push origin :old_branch # Delete the old branch
git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote
如果您需要更多:一步一步,如何更改Git分支名称是一篇很好的文章.
可能正如其他人所提到的,这将是分支命名中的情况不匹配.
如果你有这样的情况,我猜你在Windows上也会引导你:
$ git branch -m CaseSensitive casesensitive
fatal: A branch named 'casesensitive' already exists.
然后你必须做一个中间步骤:
$ git branch -m temporary
$ git branch -m casesensitive
而已.
试图专门回答这个问题(至少标题).
您还可以重命名本地分支,但会一直跟踪远程上的旧名称.
git branch -m old_branch new_branch
git push --set-upstream origin new_branch:old_branch
现在,当您运行时git push
,远程old_branch
引用将使用您的本地更新new_branch
.
您必须知道并记住此配置.但是如果您没有远程分支名称的选择,但是您不喜欢它(哦,我的意思是,您有一个非常好的理由不喜欢它!)并且更喜欢更清晰您当地分行的名称.
使用fetch配置,您甚至可以重命名本地远程引用.即,具有refs/remote/origin/new_branch
指向分支的ref指针,实际上是old_branch
on origin
.但是,为了您的安全,我强烈反对这一点.
仅需两个步骤即可remote
在GitHub和GitHub 上复制名称更改:
步骤1 git branch -m old_branchname new_branchname
第2步 git push origin :old_branchname new_branchname
- I had also to do one addtional thing: `git push --set-upstream origin new_branchname` which is mentioned in @Nomade answer
- Step 3 not needed. Everything was up-to-date after Step 2.
如果您愿意使用SourceTree(我强烈推荐),您可以右键单击您的分支并选择"重命名".
要重命名当前分支(分离的HEAD状态除外),您还可以使用此别名:
[alias]
mvh = !sh -c 'git branch -m `git rev-parse --abbrev-ref HEAD` $1'
在本地更改分支很容易......
如果您在分支机构上想要更改名称,只需执行以下操作:
git branch -m my_new_branch
否则,如果您在master
或您想要更改名称的分支以外的任何其他分支,只需执行以下操作:
git branch -m my_old_branch my_new_branch
此外,我创建下面的图像,以在命令行中显示此操作,在这种情况下,您在master
分支上,例如:
另一种选择是根本不使用命令行.诸如SourceTree之类的Git GUI客户端会消除大部分语法学习曲线/痛苦,导致诸如此类问题之类的问题成为Stack Overflow中观看次数最多的问题.
在SourceTree中,右键单击左侧"分支"窗格中的任何本地分支,然后选择"重命名...".
- 我不会称之为痛苦.git命令非常易于使用,一旦你看到这个答案,你可能永远不会再回来了.问题更多,因此看起来,git命令行的*documentation*不够直观.
由于您不希望将分支推送到远程服务器,因此该示例将非常有用:
假设您有一个名为"my-hot-feature"的现有分支,并且您想将其重命名为"feature-15".
首先,您要更改本地分支.这可能不容易:
git branch -m my-hot-feature feature-15
有关更多信息,您可以在Git中访问本地和远程重命名分支.
git版本2.9.2
如果要更改您所在的本地分支的名称:
git branch -m new_name
如果要更改其他分支的名称:
git branch -m old_name new_name
如果要将其他分支的名称更改为已存在的名称:
git branch -M old_name new_name_that_already_exists
注意:最后一个命令是破坏性的并且将重命名您的分支,但是您将丢失具有该名称和那些提交的旧分支,因为分支名称必须是唯一的.
一种简单的方法:
git branch -m old_branch new_branch # Rename branch locally
git push origin :old_branch # Delete the old branch
git push --set-upstream origin new_branch # Push the new branch, set local branch to track the new remote
如需更多信息,请参见本。
如果要更改当前分支的名称,请运行:
git branch -m [old_branch] [new_branch]
如果要删除旧的远程分支,请运行:
git push origin :[old_branch]
如果要删除旧的远程分支并创建新的远程分支,请运行:
git push origin :old_branch new_branch
实际上你有三个步骤,因为本地分支在服务器上有一个副本,所以我们在服务器上的两个步骤中有一个本地步骤:
-
重命名本地:只需使用以下命令重命名当前分支,即使您已将其检出:
git branch -m <old-branch-name> <new-branch-name>
-
删除服务器一:使用以下命令删除服务器上的旧名称分支:
git push <remote-name[origin by default]> :<old-branch-name>
-
推送新分支:现在是推送服务器上命名的新分支的时候了:
git push -u <new-branch-name>
Git 分支重命名可以通过使用:
-m和-M的区别:
-m:如果您尝试使用-m使用现有分支名称重命名分支。它会引发一个错误,指出分支已经存在。您需要提供唯一的名称。
但,
-M:这将帮助您使用给定的名称强制重命名,即使它存在。所以现有的分支将完全覆盖它......
这是一个Git 终端示例,
mohideen@dev:~/project/myapp/sunithamakeup$ git branch
master
master0
new_master
test
* test1
mohideen@dev:~/project/myapp/sunithamakeup$ git branch -m test1 test
fatal: A branch named 'test' already exists.
mohideen@dev:~/project/myapp/sunithamakeup$ git branch -M test1 test
mohideen@dev:~/project/myapp/sunithamakeup$ git branch
master
master0
new_master
* test
mohideen@dev:~/project/myapp/sunithamakeup$
Before we begin, make sure you’ve selected the branch you want to rename:
git checkout old-name
If you want to see all of your local branches, use the following command:
git branch --list
When you’re all clear, follow these steps:
-
git branch -m new-name
-
git checkout master git branch -m old-name new-name
-
git branch -a
Although it isn’t possible to rename a remote branch directly, the process of renaming one involves these three easy steps:
-
git push origin --delete old-name git push origin :old-name new-name
-
git push origin -u new-name
git branch -m old_branch_name new_branch_name
要么
git branch --move old_branch_name new_branch_name
- 这似乎是现有答案的重复。
对于Git GUI用户,这再简单不过了。在Git GUI中,从菜单项Branch:Rename创建的“重命名分支”对话框的下拉列表中选择分支名称,键入新名称,然后单击“重命名”。我突出显示了在哪里可以找到下拉列表。