我如何重命名尚未被推到远程仓库的本地分支 ?
相关:
重命名本地和远程 Git 仓库的主分支 ?
我如何重命名尚未被推到远程仓库的本地分支 ?
相关:
重命名本地和远程 Git 仓库的主分支 ?
当前回答
之前所有的答案都在谈论 Git 分支- m。 当然,这很容易操作, 但对我来说, 可能很难记住另一个 Git 命令。 所以我试图让我熟悉的指令完成它的工作。 是的,你可能猜到了。
我使用 git 分支 -b <new_branch_name>。 如果您现在不想保存旧分支, 您可以执行 git 分支 -D <old_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] [new_branch]
如果您想要删除旧的远程分支,请运行 :
git push origin :[old_branch]
如果您想要删除旧的远程分支并创建一个新的远程分支,请运行 :
git push origin :old_branch new_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
如果您愿意使用 SourceTree (我强烈建议), 您可以右键单击分支并选择“ 改名 ” 。
调
所有你必须做的是三步:
在.git/refs/head 下给旧分支以.git/refs/head 下的新名称 给旧分支以.git/log/refs/head 下的新名称 更新 .git/HEAD 以导致您的新分支名称 。