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

相关:


当前回答

  1. 重新命名您的本地分支 。

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

git branch -m new-name

如果在另一分支上:

git branch -m old-name new-name
  1. 删除旧名称远程分支并按下新名称本地分支 。

git push origin :old-name new-name

  1. 重置新命名本地分支的上游分支。 切换到分支, 然后 :

git push origin -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

其他回答

之前所有的答案都在谈论git branch -m。 当然, 操作很容易, 但对我来说, 可能很难记住另一个 Git 命令 。 所以我试图完成我熟悉的指令。 是的, 你可能猜到了 。

我用的是git branch -b <new_branch_name>。如果你不想保存旧的分支,现在你可以执行git branch -D <old_branch_name>要删除它。

我知道这或许有点无聊, 但更容易理解和记住。我希望这对你有益。

试图具体回答问题(至少是标题)。

您也可以重新命名当地当地树枝,但继续追踪遥控器上的旧名称。

git branch -m old_branch new_branch
git push --set-upstream origin new_branch:old_branch

现在,当你跑的时候git push远程old_branchref 与您的本地更新new_branch.

你必须知道并记住配置此配置。 但如果您没有选择远程分支名称的选择, 它可能会有用, 但是您不喜欢它( 我的意思是, 您有一个非常好也希望本地分行名称更明确。

使用抓取配置, 您甚至可以重命名本地远程引用 , 即refs/remote/origin/new_branchref 指向分支,事实上就是old_branch上 年 月origin然而,我非常不赞成这样做,因为你心智安全。

重命名当前分支 :

git branch -m <newname>

将指定给任何分支的分支重新命名 :

git branch -m <oldname> <newname>

-m短为--move.


推动地方分支并重设上游分支:

git push origin -u <newname>

删除远程分支 :

git push origin --delete <oldname>

创建git rename别名 :

git config --global alias.rename 'branch -m'

在 Windows 或其他不区分大小写的文件系统上使用-M如果名称仅出现资本化变化, 则该名称只有资本化变化 。 否则, Git 将投"部门已经存在"错误 。

git branch -M <newname>

有关这一程序的更多详情。

如何在 Git 中重命名本地分支

  • 要重命名当前分支, 请确认您已经检查过, 并且正在使用您想要重命名的分支 。
    git checkout oldbranch
    时当时
    git branch -m newbranch
  • 如果您想要,您可以在另一个分支工作时重新命名分支。
    git branch -m oldbranch newbranch

如何重命名 Git 中的远程分支

如果其他用户使用此分支并承诺执行此分支, 您应该在将其重新命名为本地之前将其拉动 。 这样可以确保您的本地仓库得到更新, 并确保其他用户所做的更改不会丢失 。

  • 首先,我们需要删除oldbranch从远程仓库中键,然后按enwbranch至远程。
    git push origin --delete oldbranch

  • 使用-u(固定上游)选项,
    git push origin -u newbranch

如果您不想将分支推到远程服务器, 这个示例将很有用 :

假设你有一个现有的分支 叫做"我的热能特长" 你想把它改名为"特长15"

首先,你想改变你的地方分支。

git branch -m my-hot-feature feature-15

更多信息,请访问地方和远程重命名Git的一个分支.