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

相关:

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


当前回答

一个简单的方法来做到这一点:

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 branch -m <newname>

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

git branch -m <oldname> <newname>

- M是短的 -移动。


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

git push origin -u <newname>

删除远程分支 :

git push origin --delete <oldname>

要创建 git 重命名别名 :

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

在 Windows 或其他不区分大小写的文件系统上, 如果名称中仅出现大小写变化, 请使用 - M 。 否则, Git 会丢弃一个“ 分支已经存在” 错误 。

git branch -M <newname>

到目前为止,答案是正确的,但以下是一些补充信息:

人们可以安全地将分支名称重新命名为 '-m' (move) , 但必须小心使用 '- M' , 因为它迫使重命名, 即使已经有一个已有的分支名称相同 。 以下是“ gitt- branch” man page的节录 :

在 a - m 或 - M 选项下, < oldbranch > 将重新命名为 < newbranch > 。 如果 < oldbranch > 拥有相应的 reflog, 它将被重新命名为匹配 < newbranch > , 并创建一个 reflog 条目以记住分支重命名。 如果存在 < newbranch > , 则 - M 必须用来强制重命名 。

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

我使用 git 分支 -b <new_branch_name>。 如果您现在不想保存旧分支, 您可以执行 git 分支 -D <old_branch_name> 来删除它 。

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

可能正如其他人所提到的那样,这将是分支命名中出现的情况不匹配。

如果你有这样的情况,我可以猜测 你在Windows上,这也将导致你:

$ git branch -m CaseSensitive casesensitive
fatal: A branch named 'casesensitive' already exists.

然后,你必须做一个中间步骤:

$ git branch -m temporary
$ git branch -m casesensitive

仅此而已。

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

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