Renaming a branch in GitHub

前端 未结 15 2235
面向向阳花
面向向阳花 2020-12-02 03:28

I just renamed my local branch using

git branch -m oldname newname

but this only renames the local version of the branch. How can I rename

相关标签:
15条回答
  • 2020-12-02 04:00

    As mentioned, delete the old one on GitHub and re-push, though the commands used are a bit more verbose than necessary:

    git push origin :name_of_the_old_branch_on_github
    git push origin new_name_of_the_branch_that_is_local
    

    Dissecting the commands a bit, the git push command is essentially:

    git push <remote> <local_branch>:<remote_branch>
    

    So doing a push with no local_branch specified essentially means "take nothing from my local repository, and make it the remote branch". I've always thought this to be completely kludgy, but it's the way it's done.

    As of Git 1.7 there is an alternate syntax for deleting a remote branch:

    git push origin --delete name_of_the_remote_branch
    

    As mentioned by @void.pointer in the comments

    Note that you can combine the 2 push operations:

    git push origin :old_branch new_branch

    This will both delete the old branch and push the new one.

    This can be turned into a simple alias that takes the remote, original branch and new branch name as arguments, in ~/.gitconfig:

    [alias]
        branchm = "!git branch -m $2 $3 && git push $1 :$2 $3 -u #"
    

    Usage:

    git branchm origin old_branch new_branch
    

    Note that positional arguments in shell commands were problematic in older (pre 2.8?) versions of Git, so the alias might vary according to the Git version. See this discussion for details.

    0 讨论(0)
  • 2020-12-02 04:00

    I've found three commands on how you can change your Git branch name, and these commands are a faster way to do that:

    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 step-by-step you can read this great article:

    How to Rename Git Local and Remote Branches

    0 讨论(0)
  • 2020-12-02 04:01

    Simple as that. In order to rename a Git branch locally and remotely use this snippet (tested and works like a charm):

    git branch -m <oldBranchName> <newBranchName>
    git push origin :<oldBranchName>
    git push --set-upstream origin <newBranchName>
    

    Explanation:

    1. Rename step:

    Git reference: With a -m or -M option, <oldbranch> will be renamed to <newbranch>. If <oldbranch> had a corresponding reflog, it is renamed to match <newbranch>, and a reflog entry is created to remember the branch renaming. If <newbranch> exists, -M must be used to force the rename to happen.

    1. Delete step:

    Git reference: git push origin :experimental Find a ref that matches experimental in the origin repository (e.g. refs/heads/experimental), and delete it.

    1. Update on remote repository step (upstream reference for tracking):

    Git reference: --set-upstream For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull[1] and other commands. For more information, see branch.<name>.merge in git-config[1].

    0 讨论(0)
  • 2020-12-02 04:01

    The following commands rename the branch locally, delete the old branch on the remote location and push the new branch, setting the local branch to track the new remote:

    git branch -m old_branch new_branch
    git push origin :old_branch
    git push --set-upstream origin new_branch
    
    0 讨论(0)
  • 2020-12-02 04:01

    Three simple steps

    • git push origin head

    • git branch -m old-branch-name new-branch-name

    • git push origin head

    0 讨论(0)
  • 2020-12-02 04:05

    In my case, I needed an additional command,

    git branch --unset-upstream
    

    to get my renamed branch to push up to origin newname.

    (For ease of typing), I first git checkout oldname. Then run the following:

    git branch -m newname <br/> git push origin :oldname*or*git push origin --delete oldname
    git branch --unset-upstream
    git push -u origin newname or git push origin newname

    This extra step may only be necessary because I (tend to) set up remote tracking on my branches via git push -u origin oldname. This way, when I have oldname checked out, I subsequently only need to type git push rather than git push origin oldname.

    If I do not use the command git branch --unset-upstream before git push origin newbranch, git re-creates oldbranch and pushes newbranch to origin oldbranch -- defeating my intent.

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