How do I rename a local Git branch?

后端 未结 30 1163
轻奢々
轻奢々 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:23

    Rename the branch using this command:

    git branch -m [old_branch_name] [new_branch_name]
    

    -m: It renames/moves the branch. If there is already a branch, you will get an error.

    If there is already a branch and you want to rename with that branch, use:

     git rename -M [old_branch_name] [new_branch_name]
    

    For more information about help, use this command in the terminal:

    git branch --help
    

    or

    man git branch
    
    0 讨论(0)
  • 2020-11-22 12:23

    Actually you have three steps because the local branch has a duplicate on the server so we have one step for local on two steps on the server:

    1. Rename local: just use the following command to rename your current branch, even you checked it out:
      git branch -m <old-branch-name> <new-branch-name>
      
    2. Delete the server one: use the following command to delete the old name branch on the server:
      git push <remote-name[origin by default]> :<old-branch-name>
      
    3. Push the new one: now it's time to push the new branch named on the server:
      git push -u <new-branch-name>
      
    0 讨论(0)
  • 2020-11-22 12:24

    Git branch rename can be done by using:

    1. git branch -m oldBranch newBranch

    2. git branch -M oldBranch ExistingBranch

    The difference between -m and -M:

    -m: if you're trying to rename your branch with an existing branch name using -m. It will raise an error saying that the branch already exists. You need to give unique name.

    But,

    -M: this will help you to force rename with a given name, even it is exists. So an existing branch will overwrite entirely with it...

    Here is a 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$
    
    0 讨论(0)
  • 2020-11-22 12:25
    1. Rename your local branch.

    If you are on the branch you want to rename:

    git branch -m new-name
    

    If you are on a different branch:

    git branch -m old-name new-name
    
    1. Delete the old-name remote branch and push the new-name local branch.

    git push origin :old-name new-name

    1. Reset the upstream branch for the new-name local branch. Switch to the branch and then:

    git push origin -u new-name

    Or for a fast way to do that, you can use these 3 steps:

    # 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

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

    I foolishly named a branch starting with a hyphen, and then checked out master. I didn't want to delete my branch, I had work in it.

    Neither of these worked:

    git checkout -dumb-name

    git checkout -- -dumb-name

    "s, 's and \s didn't help either. git branch -m doesn't work.

    Here's how I finally fixed it. Go into your working copy's .git/refs/heads, find the filename "-dumb-name", get the hash of the branch. Then this will check it out, make a new branch with a sane name, and delete the old one.

    git checkout {hash}
    git checkout -b brilliant-name
    git branch -d -- -dumb-name
    
    0 讨论(0)
  • 2020-11-22 12:28

    Advanced Git users can rename manually using:

    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
    
    0 讨论(0)
提交回复
热议问题