Categories
git git-branch version-control

How do I rename a local Git branch?

10601

How do I rename a local branch which has not yet been pushed to a remote repository?

Related:

0

    17281

    To rename a branch while pointed to any branch:

    git branch -m <oldname> <newname>
    

    To rename the current branch:

    git branch -m <newname>
    

    -m is short for --move.


    To push the local branch and reset the upstream branch:

    git push origin -u <newname>
    

    To delete the remote branch:

    git push origin --delete <oldname>
    

    To create a git rename alias:

    git config --global alias.rename 'branch -m'
    

    On Windows or another case-insensitive filesystem, use -M if there are only capitalization changes in the name. Otherwise, Git will throw a “branch already exists” error.

    git branch -M <newname>
    

    14

    • 233

      @PandaWood: it will add the new branch when you push, but won’t delete the old branch. If you use git push -f --mirror, then it will rename the branch on the remote, but you should only use this method if the remote is simply to be a copy of your current repository. See also this question: stackoverflow.com/questions/1526794/git-rename-remote-branch

      – siride

      Jan 23, 2012 at 6:02

    • 28

      @PandaWood, it depends on how push.default is configured. By default (matching) it will push to a remote whose name matches. You would have to do git push origin <newname>:<oldname> or you will create a new remote branch. However, if push.default is set to upstream, then you can push origin head and things will go to the oldname on the remote.

      Oct 31, 2013 at 23:46


    • 62

      The long name of the -m option is --move, e.g., git branch --move master renames the current branch to be called “master”.

      Sep 22, 2015 at 17:56

    • 1

      interesting, it seems that the rename is temporarily case insensitive, even on OSX. Starting from FB12.show_hide_cols, -m fb12.show_hide_cols got me an existing branch error. But -m fb12.show_hide_col, then -m fb12.show_hide_cols got me where I wanted. afterwards, git branch only shows the one renamed branch, just like I wanted.

      – JL Peyret

      Mar 22, 2016 at 1:01


    • 4

      After doing this, you should also run git push origin :<old_name> <new_name> to update your remote branch names.

      Apr 1, 2016 at 14:52


    559

    git branch -m old_branch_name new_branch_name
    

    The above command will change your branch name, but you have to be very careful using the renamed branch, because it will still refer to the old upstream branch associated with it, if any.

    If you want to push some changes into master after your local branch is renamed into new_branch_name (example name):

    git push origin new_branch_name:master (now changes will go to master branch but your local branch name is new_branch_name)

    For more details, see “How to rename your local branch name in Git.”

    0

      431

      To rename your current branch:

      git branch -m <newname>
      

      2

      • 202

        You will need to use -M to rename if you are only changing capitalization, as git will tell you that branch already exists.

        May 8, 2015 at 21:04

      • and afterwards git push origin HEAD:<oldname>

        – techkuz

        Jan 28, 2021 at 17:57