Renaming branches remotely in Git

后端 未结 9 1198
执笔经年
执笔经年 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:32

    TL;DR

    "Renaming" a remote branch is actually a 2 step process (not necessarily ordered):

    • deletion of the old remote branch (git push [space]: as ksrb explained);
    • push into a new remote branch (difference between a couple of answers commands below).

    Deleting

    I use TortoiseGit and when I first tried to delete the branch through the command line, I got this:

    $ git push origin :in
    
    • fatal: 'origin' does not appear to be a git repository

    • fatal: Could not read from remote repository.

    Please make sure you have the correct access rights and the repository exists.

    This was likely due to pageant not having the private key loaded (which TortoiseGit loads automatically into pageant). Moreover, I noticed that TortoiseGit commands do not have the origin ref in them (e.g. git.exe push --progress "my_project" interesting_local:interesting).

    I am also using Bitbucket and, as others web-based online git managers of the sort (GitHub, GitLab), I was able to delete the remote branch directly through their interface (branches page):

    However, in TortoiseGit you may also delete remote branches through Browse References:

    By right-clicking on a remote branch (remotes list) the Delete remote branch option shows up:

    Pushing

    After deleting the old remote branch I pushed directly into a new remote branch through TortoiseGit just by typing the new name in the Remote: field of the Push window and this branch was automatically created and visible in Bitbucket.

    However, if you still prefer to do it manually, a point that has not been mentioned yet in this thread is that -u = --set-upstream.

    From git push docs, -u is just an alias of --set-upstream, so the commands in the answers of Sylvain (-set-upstream new-branch) and Shashank (-u origin new_branch) are equivalent, since the remote ref defaults to origin if no other ref was previously defined:

    • git push origin -u new_branch = git push -u new_branch from the docs description:

      If the configuration is missing, it defaults to origin.

    In the end, I did not manually type in or used any of the commands suggested by the other answers in here, so perhaps this might be useful to others in a similar situation.

提交回复
热议问题