Renaming a branch in GitHub

前端 未结 15 2298
面向向阳花
面向向阳花 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:12

    Another way is to rename the following files:

    1. Navigate your project directory.
    2. Rename .git/refs/head/[branch-name] to .git/refs/head/new-branch-name.
    3. Rename .git/refs/remotes/[all-remote-names]/[branch-name] to .git/refs/remotes/[all-remote-names]/new-branch-name.

    Rename head and remotes both on your local PC and on origins(s)/remote server(s).

    Branch is now renamed (local and remote!)


    Attention

    If your current branch-name contains slashes (/) Git will create the directories like so:

    current branch-name: "awe/some/branch"

    • .git/refs/head/awe/some/branch
    • .git/refs/remotes/[all-remote-names]/awe/some/branch

    wish branch-name: "new-branch-name"

    1. Navigate your project directory.
    2. Copy the branch file from .git/refs/*/awe/some/.
    3. Put it in .git/refs/head/.
    4. Copy the branch file from all of .git/refs/remotes/*/awe/some/.
    5. Put them in .git/refs/remotes/*/.
    6. Rename all copied branch files to new-branch-name.
    7. Check if the directory and file structure now looks like this:
    • .git/refs/head/new-branch-name
    • .git/refs/remotes/[all-remote-names]/new-branch-name
    1. Do the same on all your remote origins/servers (if they exist)
    • Info: on remote-servers there are usually no refs/remotes/* directories because you're already on remote-server ;) (Well, maybe in advanced Git configurations it might be possible, but I have never seen that)

    Branch is now renamed from awe/some/branch to new-branch-name (local and remote!)

    Directories in branch-name got removed.


    Information: This way might not be the best, but it still works for people who might have problems with the other ways

提交回复
热议问题