Renaming branches remotely in Git

后端 未结 9 1203
执笔经年
执笔经年 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  /:refs/heads/ :
    

    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]}   "
        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 /:refs/heads/ to push a new remote branch based on the old remote tracking branch and then git push : to delete the old remote branch.

提交回复
热议问题