我如何重命名尚未被推到远程仓库的本地分支 ?

相关:

重命名本地和远程 Git 仓库的主分支 ?


当前回答

要在 GitHub 桌面上重命名本地分支, 请点击当前分支标签( 到当前仓库右侧) , 右单击您想要重命名的分支, 并点击重命名 。 然后您会被弹出提示到您可以重命名的地方 。

其他回答

git branch -m [old-branch] [new-branch]

-m 意指全部从[旧部门]移动到[新部门],并记住您可以使用 -M 用于其他文件系统。

高级 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 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 分支名称是一个很好的文章 。

重新命名您的本地分支 。

如果您在分支上, 您想要重命名 :

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

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