我如何重命名尚未被推到远程仓库的本地分支 ?
相关:
重命名本地和远程 Git 仓库的主分支 ?
我如何重命名尚未被推到远程仓库的本地分支 ?
相关:
重命名本地和远程 Git 仓库的主分支 ?
当前回答
高级 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
其他回答
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 branch -m old_branch_name new_branch_name
上述命令将改变您的分支名称, 但你必须非常小心地使用重新命名的分支, 因为它仍然会指与其相关的旧的上游分支, 如果有的话 。
如果您想要在本地分支重新命名为新分支后将一些更改推入母版( 例如名称) :
git push 源新\ branch_ name : master (现在更改将转到母版分支, 但您本地的分支名称是新_ branch_ name )
详情请见“如何在 Git 中重新命名您的本地分支名称 ” 。
Git 分支重命名可以使用 :
git 分支 - m 旧的Branch 新建的Branch Git 分支 - M 旧的Branch 现有Branch
-m和-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$
改变本地分支很容易...
如果您在树枝上, 您想要更改名称, 只需这样做 :
git branch -m my_new_branch
否则,如果你是主人 或任何其他分支 除了你想改名的分支, 简单做:
git branch -m my_old_branch my_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