Rename git branch on Local and GitHub Remove using Command


In order to rename a git branch locally make use of the git branch command with -m or --move option.

Renaming Local Git Branch

    Command Syntax:
    % git branch -m old-branch-name new-branch-name
    % git branch --move old-branch-name new-branch-name


    Examples:
    % git branch -m release/ticket01 release/ticket-1
    % git branch -m master main
    Git - Rename Local Branch

Renaming Remote GitHub Branch

    In order to rename a git branch on the remote git server like GitHub, make use of the git push command with the following syntax,


    Command Syntax:
    % git push origin :old-branch-name new-branch-name

    Examples:
    % git branch --remote
    
      origin/HEAD -> origin/main
      origin/main
      origin/release01

    As you can see I have three branches at the remote repo and I want to rename release01 to release-1,

    % git push origin :release01 release-1
    Username for 'https://github.com': gituser
    Password for 'https://gituser@github.com': 
    Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
    To https://github.com/gituser/myrepo.git
     - [deleted]         release01
     * [new branch]      release-1 -> release-1

    Note: Make sure that you have the branch renamed on your local repo before trying this or else you will get an error,

    % git push origin :release01 release-11
    error: src refspec release-11 does not match any
    error: failed to push some refs to 'https://github.com/gituser/myrepo.git'
    Git - Renaming Remote GitHub Branch Name

This is not an AI-generated article but is demonstrated by a human on an M1 Mac running macOS Ventura 13.

Please support independent contributors like Code2care by donating a coffee.

Buy me a coffee!

Buy Code2care a Coffee!

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org



















Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap