我如何重命名尚未被推到远程仓库的本地分支 ?
相关:
重命名本地和远程 Git 仓库的主分支 ?
我如何重命名尚未被推到远程仓库的本地分支 ?
相关:
重命名本地和远程 Git 仓库的主分支 ?
当前回答
如果您想要更改分支的名称
git branch -m newname
其他回答
高级 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
重新命名您的本地分支 。
如果您在分支上, 您想要重命名 :
git branch -m new-name
如果在另一分支上:
git branch -m old-name new-name
删除旧名称远程分支并按下新名称本地分支 。
git 推推源: old- name new- name
重置新命名本地分支的上游分支。 切换到分支, 然后 :
git 推进源 - u new- name
或者为了快速地做到这一点 你可以使用这三步:
# 本地重命名分支
git branch -m old_branch new_branch
# 删除旧的远程分支
git push origin :old_branch
# 推动新分支, 设置本地分支以跟踪新远程
git push --set-upstream origin new_branch
参考文献:https://www.w3docs.com/snippets/git/how-to-rename-git-local-and-remote-branches.html
到目前为止,答案是正确的,但以下是一些补充信息:
人们可以安全地将分支名称重新命名为 '-m' (move) , 但必须小心使用 '- M' , 因为它迫使重命名, 即使已经有一个已有的分支名称相同 。 以下是“ gitt- branch” man page的节录 :
在 a - m 或 - M 选项下, < oldbranch > 将重新命名为 < newbranch > 。 如果 < oldbranch > 拥有相应的 reflog, 它将被重新命名为匹配 < newbranch > , 并创建一个 reflog 条目以记住分支重命名。 如果存在 < newbranch > , 则 - M 必须用来强制重命名 。
实际上,你有三个步骤 因为本地分行服务器上有一个复制件 所以我们在服务器上的两个步骤上有一个步骤是本地分行:
重命名本地 : 只要使用以下命令重命名您的当前分支, 甚至您检查了它 : git 分支 - m < old- branch- name > < new- branch- name > < new- branch- name > 删除服务器 1 : 使用以下命令删除服务器上的旧名称分支 : git push < remote- name[默认发自] > : <old- branch- name> 推动新分支 : 现在是时候推动服务器上的新分支了 : git push - u < new- branch- name >
之前所有的答案都在谈论 Git 分支- m。 当然,这很容易操作, 但对我来说, 可能很难记住另一个 Git 命令。 所以我试图让我熟悉的指令完成它的工作。 是的,你可能猜到了。
我使用 git 分支 -b <new_branch_name>。 如果您现在不想保存旧分支, 您可以执行 git 分支 -D <old_branch_name> 来删除它 。
我知道这或许有点无聊, 但更容易理解和记住。我希望这对你有益。