Renaming branches remotely in Git

后端 未结 9 1157
执笔经年
执笔经年 2020-11-28 17:07

If there is a repository that I only have git:// access to (and would usually just push+pull), is there a way to rename branches in that repository in the same

相关标签:
9条回答
  • 2020-11-28 17:51

    If you really just want to rename branches remotely, without renaming any local branches at the same time, you can do this with a single command:

    git push <remote> <remote>/<old_name>:refs/heads/<new_name> :<old_name>
    

    I wrote this script (git-rename-remote-branch) which provides a handy shortcut to do the above easily.

    As a bash function:

    git-rename-remote-branch(){
      if [ $# -ne 3 ]; then
        echo "Rationale : Rename a branch on the server without checking it out."
        echo "Usage     : ${FUNCNAME[0]} <remote> <old name> <new name>"
        echo "Example   : ${FUNCNAME[0]} origin master release"
        return 1 
      fi
    
      git push $1 $1/$2\:refs/heads/$3 :$2
    }
    

    To integrate @ksrb's comment: What this basically does is two pushes in a single command, first git push <remote> <remote>/<old_name>:refs/heads/<new_name> to push a new remote branch based on the old remote tracking branch and then git push <remote> :<old_name> to delete the old remote branch.

    0 讨论(0)
  • 2020-11-28 17:51

    You can create a new branch based on old-name branch. Just like this, then delete the old branch, over!!!

    0 讨论(0)
  • 2020-11-28 17:52

    I don't know if this is right or wrong, but I pushed the "old name" of the branch to the "new name" of the branch, then deleted the old branch entirely with the following two lines:

    git push origin old_branch:new_branch
    git push origin :old_branch
    
    0 讨论(0)
提交回复
热议问题