How do I rename a local Git branch?

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

    To rename a branch locally:

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

    Now you'll have to propagate these changes on your remote server as well.

    To push changes of the deleted old branch:

    git push origin :[old-branch]
    

    To push changes of creation of new branch:

    git push origin [new-branch]
    
    0 讨论(0)
  • 2020-11-22 12:01

    If you are willing to use SourceTree (which I strongly recommend), you can right click your branch and chose 'Rename'.

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

    1. Rename

    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. Track a new remote branch

    - 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
    
    0 讨论(0)
  • 2020-11-22 12:04

    Since you do not want to push the branch to a remote server, this example will be useful:

    Let's say you have an existing branch called "my-hot-feature," and you want to rename it to "feature-15."

    First, you want to change your local branch. This couldn't be easier:

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

    For more information, you can visit Locally and Remotely Renaming a Branch in Git.

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

    Here are three steps: A command that you can call inside your terminal and change branch name.

    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
    

    If you need more: step-by-step, How To Change Git Branch Name is a good article about that.

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

    A simple way to do it:

    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
    

    For more, see this.

    0 讨论(0)
提交回复
热议问题