How do I rename a local Git branch?

后端 未结 30 1161
轻奢々
轻奢々 2020-11-22 11:48

I don\'t want to rename a remote branch, as described in Rename master branch for both local and remote Git repositories.

How can I rename a local branch wh

相关标签:
30条回答
  • 2020-11-22 12:19

    The answers so far have been correct, but here is some additional information:

    One can safely rename a branch with '-m' (move), but one has to be careful with '-M', because it forces the rename, even if there is an existing branch with the same name already. Here is the excerpt from the 'git-branch' man page:

    With a -m or -M option, <oldbranch> will be renamed to <newbranch>. If <oldbranch> had a corresponding reflog, it is renamed to match <newbranch>, and a reflog entry is created to remember the branch renaming. If <newbranch> exists, -M must be used to force the rename to happen.

    0 讨论(0)
  • 2020-11-22 12:20

    Here are the steps to rename the branch:

    1. Switch to the 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): Make sure you run command git status and check that the newly created branch is pointing to its own ref and not the older one. If you find the reference to the older branch, you need to unset the upstream using:

    git branch --unset-upstream
    
    0 讨论(0)
  • 2020-11-22 12:21

    To rename your current branch:

    git branch -m <newname>
    
    0 讨论(0)
  • 2020-11-22 12:22

    To rename the current branch (except for detached HEAD state) you can also use this alias:

    [alias]
        mvh = !sh -c 'git branch -m `git rev-parse --abbrev-ref HEAD` $1'
    
    0 讨论(0)
  • 2020-11-22 12:22

    Git version 2.9.2

    If you want to change the name of the local branch you are on:

    git branch -m new_name
    

    If you want to change the name of a different branch:

    git branch -m old_name new_name
    

    If you want to change the name of a different branch to a name that already exists:

    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.

    0 讨论(0)
  • 2020-11-22 12:22

    Before we begin, make sure you’ve selected the branch you want to rename:

    git checkout old-name
    

    If you want to see all of your local branches, use the following command:

    git branch --list
    

    When you’re all clear, follow these steps:

    1. Using the Git rename branch command will require you to add an -m option to your command:

      git branch -m new-name
      
    2. You can also rename a local branch from another branch by using the following two commands:

      git checkout master
      
      git branch -m old-name new-name
      
    3. Lastly, this command will list all — both local and remote — branches to verify that it has been renamed:

      git branch -a
      

    Although it isn’t possible to rename a remote branch directly, the process of renaming one involves these three easy steps:

    1. To start, you will need to rename a local branch by following the previous steps. 2.Then delete the old branch and push the new one. You can do this easily with the following commands:

       git push origin --delete old-name
       git push origin :old-name new-name
      
    2. Reset the upstream branch for your new local branch and you will be all set:

      git push origin -u new-name
      
    0 讨论(0)
提交回复
热议问题