How do I rename a local Git branch?

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

提交回复
热议问题