Renaming a branch in GitHub

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

    On the Git branch, run:

    git branch -m old_name  new_name
    

    This will modify the branch name on your local repository:

    git push origin :old_name new_name
    

    This will push the modified name to the remote and delete the old branch:

    git push origin -u new_name
    

    It sets the local branch to track the remote branch.

    This solves the issue.

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

    Just remove the old branch and create new one.

    Example (solely renaming the remote branch):

    git push origin :refs/heads/oldname
    git push origin newname:refs/heads/newname
    

    You also probably should rename local branch and change settings for where to push/pull.

    0 讨论(0)
  • 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

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

    You can do that without the terminal. You just need to create a branch with the new name, and remove the old after.

    Create a branch

    In your repository’s branch selector, just start typing a new branch name. It’ll give you the option to create a new branch:

    It’ll branch off of your current context. For example, if you’re on the bugfix branch, it’ll create a new branch from bugfix instead of master. Looking at a commit or a tag instead? It’ll branch your code from that specific revision.

    Delete a branch

    You’ll also see a delete button in your repository’s Branches page:

    As an added bonus, it’ll also give you a link to the branch’s Pull Request, if it has one.

    I just copied this content from: Create and delete branches

    0 讨论(0)
  • 2020-12-02 04:18
    1. Download Atlassian Sourcetree (free).
    2. Import your local clone of the repository.
    3. Right click your branch to rename, in the sidebar. Select "Rename branch..." from context menu, and rename it.
    4. Push to origin.
    0 讨论(0)
  • 2020-12-02 04:20

    Rename branches in Git local and remote

    1. Rename your local branch.

    If you are on the branch you want to rename:

    git branch -m new-name
    

    If you are on a different branch:

    git branch -m old-name new-name
    

    2. Delete the old-name remote branch and push the new-name local branch.

    git push origin :old-name new-name
    

    3. Reset the upstream branch for the new-name local branch.

    Switch to the branch and then:

    git push origin -u new-name
    

    So the conclusion is:

    git branch -m new-name
    git push origin :old-name new-name
    git push origin -u new-name
    
    0 讨论(0)
提交回复
热议问题