首页 文章

如何重命名本地Git分支?

提问于
浏览
7025

我不想重命名远程分支,如Rename master branch for both local and remote Git repositories中所述 .

如何重命名尚未推送到远程分支的本地分支?

如果您还需要重命名 remote 分支:
How to rename a remote git branch name

30 回答

  • 0

    git version 2.9.2

    如果要更改您所在的本地分支的名称:

    git branch -m new_name
    

    如果要更改其他分支的名称:

    git branch -m old_name new_name
    

    如果要将其他分支的名称更改为已存在的名称:

    git branch -M old_name new_name_that_already_exists
    

    Note: The last command is destructive and will rename your branch, but you will lose the old branch with that name and those commits because branch names must be unique.

  • 29
    git branch -m old_branch_name new_branch_name
    

    上面的命令将更改您的分支名称,但您必须非常小心使用重命名的分支,因为它仍将引用与其关联的旧上游分支(如果有) .

    如果要在将本地分支重命名为new_branch_name(示例名称)后将某些更改推送到master中:

    git push origin new_branch_name:master (现在更改将转到主分支,但您的本地分支名称是new_branch_name)

    有关更多详细信息,请参阅“How to rename your local branch name in Git” .

  • 199
    • 重命名您当地的分行 .

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

    git branch -m new-name
    

    如果你在不同的分支:

    git branch -m old-name new-name
    
    • 删除旧名称远程分支并推送新名称本地分支 .

    git push origin :old-name new-name

    • 重置新名称本地分支的上游分支 . 切换到分支然后:

    git push origin -u new-name

    或者为了快速做到这一点,您可以使用以下3个步骤:

    # Rename branch locally

    git branch -m old_branch new_branch
    

    # Delete the old remote branch

    git push origin :old_branch
    

    # Push the new branch, set local branch to track the new remote

    git push --set-upstream origin new_branch
    

    Referance:https://www.w3docs.com/snippets/git/how-to-rename-git-local-and-remote-branches.html

  • 13

    使用以下命令重命名分支:

    git branch -m [old_branch_name] [new_branch_name]
    

    -m :重命名/移动分支 . 如果已经存在分支,则会出现错误 .

    如果已经存在分支并且您想要使用该分支重命名,请使用:

    git rename -M [old_branch_name] [new_branch_name]
    

    有关帮助的更多信息,请在终端中使用此命令:

    git branch --help
    

    要么

    man git branch
    
  • 26

    简单的方法:

    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
    

    有关更多信息,请参阅this .

  • 138

    所有上述内容都在谈论 git branch -m . 当然,它很容易操作,但对我来说,记住另一个git命令可能有点困难 . 所以我试着通过我熟悉的命令来完成工作 . 是的,你可能已经猜到了 .

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

    我知道它可能有点乏味,但它更容易被理解和记住 . 希望它对你有帮助 .

  • 6

    1.重命名

    If it is your current branch, just do

    git branch -m new_name
    

    If it is another branch you want to rename

    git branch -m old_name new_name
    

    2.跟踪新的远程分支

    - If your branch was pushed, then after renaming you need to delete it from the remote Git repository and ask your new local to track a new remote branch:

    git push origin :old_name
    git push --set-upstream origin new_name
    
  • 71

    到目前为止答案是正确的,但这里有一些额外的信息:可以用'-m'(移动)重命名分支,但必须要小心,因为'-M'强制重命名,即使存在已经有同名的分支 . 以下是'git-branch'手册页的摘录:

    使用-m或-M选项,<oldbranch>将重命名为<newbranch> . 如果<oldbranch>具有相应的reflog,则将其重命名为匹配<newbranch>,并创建reflog条目以记住分支重命名 . 如果<newbranch>存在,则必须使用-M强制重命名 .

  • 7

    另一种选择是根本不使用命令行 . 诸如SourceTree之类的Git GUI客户端消除了大部分语法学习曲线/痛苦,导致诸如此类问题之类的问题成为Stack Overflow中观看次数最多的问题 .

    在SourceTree中,右键单击左侧“分支”窗格中的任何本地分支,然后选择“重命名...” .

  • 13

    以下是三个步骤:您可以在终端内调用并更改分支名称的命令 .

    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
    

    如果您需要更多:一步一步,How To Change Git Branch Name是一篇很好的文章 .

  • 366

    git branch重命名可以通过使用来完成

    • git branch -m oldBranch newBranch

    • git branch -M oldBranch ExistingBranch

    -m-M 之间的区别,

    -m: 如果您尝试使用 -m 重命名分支名称,它会引发错误,分支已经存在 . 你需要给出唯一的名字 .

    但,

    -M: 这将帮助您强制重命名给定名称,即使它存在 . 所以现有的分支将完全用它覆盖......

    这是 git terminal example,

    mohideen@dev:~/project/myapp/sunithamakeup$ git branch 
      master
      master0
      new_master
      test
    * test1
    mohideen@dev:~/project/myapp/sunithamakeup$ git branch -m test1 test
    fatal: A branch named 'test' already exists.
    mohideen@dev:~/project/myapp/sunithamakeup$ git branch -M test1 test
    mohideen@dev:~/project/myapp/sunithamakeup$ git branch 
      master
      master0
      new_master
    * test
    mohideen@dev:~/project/myapp/sunithamakeup$
    
  • 3

    更改分支 locally 非常容易......

    如果您在分支机构上想要更改名称,只需执行以下操作:

    git branch -m my_new_branch
    

    否则,如果您使用的是 masterany other branch 而不是您要更改名称的那个,只需执行以下操作:

    git branch -m my_old_branch my_new_branch
    

    另外,我创建下面的图像以在 command line 中显示此操作,在这种情况下,您在 master 分支上例如:

    Change branch name locally

  • 61

    高级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
    
  • 84

    对于Git GUI用户来说,它简单得多 . 在Git GUI中,从菜单项Branch:Rename创建的“Rename Branch”对话框的下拉列表中选择分支名称,键入New Name,然后单击“Rename” . 我已经突出显示了在哪里可以找到下拉列表 .

    Rename a local Git branch

  • 17

    我愚蠢地命名一个以连字符开头的分支,然后检查出主人 . 我不想 delete 我的分支,我有工作 .

    这些都没有奏效:

    git checkout -dumb-name

    git checkout -- -dumb-name

    " s, ' s和 \ 也没有帮助 . git branch -m 不起作用 .

    这是我最终修复它的方式 . 进入工作副本的.git / refs / heads,找到文件名“-dumb-name”,获取分支的哈希值 . 然后这将检查出来,创建一个具有合理名称的新分支,并删除旧分支 .

    git checkout {hash}
    git checkout -b brilliant-name
    git branch -d -- -dumb-name
    
  • 0
    git branch -m old_branch_name  new_branch_name
    

    要么

    git branch --move old_branch_name new_branch_name
    
  • 56

    PHPStorm:

    VCS-> Git->分支......->本地分支 - > _ your_branch _->重命名

  • 1

    试图专门回答问题(至少 Headers ) .

    您还可以重命名本地分支,但会一直跟踪远程上的旧名称 .

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

    现在,当您运行 git push 时,远程 old_branch ref将使用您的本地 new_branch 进行更新 .

    You have to know and remember 这个配置 . 但是如果你不喜欢它会很有用(哦,我的意思是,你有一个很好的理由不喜欢它!)并且更喜欢你当地分公司更清晰的名字 .

    使用获取配置,你甚至可以重命名本地远程引用 . 即,具有指向分支的 refs/remote/origin/new_branch ref指针,实际上是 old_branch 上的 old_branch . 但是,为了您的安全,我强烈反对这一点 .

  • 13

    如果要更改当前分支的名称,请运行:

    git branch -m [old_branch] [new_branch]
    

    如果要删除旧的远程分支,请运行:

    git push origin :[old_branch]
    

    如果要删除旧的远程分支并创建新的远程分支,请运行:

    git push origin :old_branch new_branch
    
  • 10

    Rename branch:

    git branch -m old_branchname new_branchname

    这里-m选项的长名称是--move . 所以我们也可以使用

    git branch --move old_branchname new_branchname

    如果要重命名当前分支,请使用此分支 .

    git branch -m new_branchname
    

    要么

    git branch -move new_branchname
    

    If you want to move these changes to remote, then use the following.

    git push origin :old_branchname new_branchname
    

    这将删除old_branchname远程分支并推送new_branchname本地分支 .

    git push origin -u new_branchname
    

    这将重置new_branchname本地分支的上游分支 .

  • 0

    如果要在指向任何分支时重命名分支,请执行以下操作:

    git branch -m <oldname> <newname>
    

    如果要重命名当前分支,可以执行以下操作:

    git branch -m <newname>
    

    一种记住这一点的方法是 -m 适用于"move"(或 mv ),这是重命名文件的方式 .

  • 1

    要重命名当前分支(分离的HEAD状态除外),您还可以使用此别名:

    [alias]
        mvh = !sh -c 'git branch -m `git rev-parse --abbrev-ref HEAD` $1'
    
  • 11187

    要重命名当前分支:

    git branch -m <newname>
    
  • 18

    由于您不希望将分支推送到远程服务器,因此该示例将非常有用:

    假设您有一个名为“my-hot-feature”的现有分支,并且您想将其重命名为“feature-15” .

    首先,您要更改本地分支 . 这可能不容易:

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

    有关更多信息,请访问Locally and Remotely Renaming a Branch in Git .

  • 18

    如果您愿意使用SourceTree(我强烈推荐),您可以右键单击您的分支并选择'Rename' .

    enter image description here

  • 13

    要在本地重命名分支:

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

    现在,您还必须在远程服务器上传播这些更改 .

    要推送已删除的旧分支的更改:

    git push origin :[old-branch]
    

    推动创建新分支的更改:

    git push origin [new-branch]
    
  • 10

    如果你想:

    • 重命名git存储库,运行: git branch -m <oldname> <newname>

    • 删除旧分支: git push origin: old-name new-name

    • 使用以下方式提交: git commit <newname>

    • 然后推送使用: git push origin new_branch_name:master

    • 如果要查看状态,请使用: git status

    • 如果您想退房,请使用: git checkout

  • 7

    以下是重命名分支的步骤:

    1. switch to branch which needs to be renamed
    2. git branch -m <new_name>
    3. git push origin :<old_name>
    4. git push origin <new_name>:refs/heads/<new_name>
    

    EDIT(12/01/2017) : 确保运行命令 git status 并检查新创建的分支是否指向自己的ref而不是旧的分支 . 如果找到对旧分支的引用,则需要使用以下命令取消设置上游:

    git branch --unset-upstream
    
  • 260

    分支完成后,重命名分支将非常有用 . 然后新的东西即将到来,你想在同一个分支中开发而不是删除它并创建新的分支 .

    根据我的经验,要在Git中重命名本地和远程分支,您应该执行以下步骤 .

    从多个状态引用 - 在git中重命名本地和远程分支

    1.重命名您的本地分支

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

    git branch -m new-name
    

    如果你在不同的分支:

    git branch -m old-name new-name
    

    2.删除旧名称远程分支并推送新名称本地分支

    git push origin :old-name new-name
    

    3.重置新名称本地分支的上游分支

    git push origin -u new-name
    
  • 0

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

    如果你有这样的情况,我猜你在Windows上也会引导你:

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

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

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

    而已 .

相关问题