[git] Renaming a branch in GitHub

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 the one on GitHub?

This question is related to git github branch rename

The answer is


On GitHub side, you can use the new (Jan. 2021) "Support for renaming an existing branch"

Follow this tutorial: https://docs.github.com/en/github/administering-a-repository/renaming-a-branch

rename branch dialog -- https://i2.wp.com/user-images.githubusercontent.com/2503052/105069955-a231fa80-5a50-11eb-982c-a114c9c44c57.png?ssl=1

See "How do I rename branch on the GitHub website?".

This is a better approach, because renaming a branch that way (on github.com) will (source):

  • Re-target any open pull requests
  • Update any draft releases based on the branch
  • Move any branch protection rules that explicitly reference the old name
  • Update the branch used to build GitHub Pages, if applicable
  • Show a notice to repository contributors, maintainers, and admins on the repository homepage with instructions to update local copies of the repository
  • Show a notice to contributors who git push to the old branch
  • Redirect web requests for the old branch name to the new branch name
  • Return a "Moved Permanently" response in API requests for the old branch name

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].


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.


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.


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

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.


This article shows how to do it real easy.

  1. To rename a local Git branch, we can use the Git branch -m command to modify the name:

     git branch -m feature1 feature2
    
  2. If you’re just looking for the command to rename a remote Git branch, this is it:

     git push -u origin feature2:feature3
    

    Check that you have no tags on the branch before you do this. You can do that with git tag.


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


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


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

The following commands worked for me:

git push origin :old-name-of-branch-on-github
git branch -m old-name-of-branch-on-github new-name-for-branch-you-want
git push origin new-name-for-branch-you-want

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:

Create a 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:

Delete a branch

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


Here is what worked for me:

  1. Create the new branch first:

    git push github newname :refs/heads/newname
    
  2. On the GitHub site, go to settings and change the Default branch to newname

  3. Delete the oldname

    git push github --delete oldname
    

Three simple steps

  • git push origin head

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

  • git push origin head


  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.

Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to github

Does the target directory for a git clone have to match the repo name? Issue in installing php7.2-mcrypt How can I switch to another branch in git? How to draw checkbox or tick mark in GitHub Markdown table? How to add a new project to Github using VS Code git clone error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054 How to add empty spaces into MD markdown readme on GitHub? key_load_public: invalid format git - remote add origin vs remote set-url origin Cloning specific branch

Examples related to branch

Get git branch name in Jenkins Pipeline/Jenkinsfile Why do I have to "git push --set-upstream origin <branch>"? Your configuration specifies to merge with the <branch name> from the remote, but no such ref was fetched.? When does Git refresh the list of remote branches? Fix GitLab error: "you are not allowed to push code to protected branches on this project"? Git push: "fatal 'origin' does not appear to be a git repository - fatal Could not read from remote repository." Git: Merge a Remote branch locally git pull from master into the development branch Depend on a branch or tag using a git URL in a package.json? How can I copy the content of a branch to a new local branch?

Examples related to rename

Gradle - Move a folder from ABC to XYZ How to rename a component in Angular CLI? How do I completely rename an Xcode project (i.e. inclusive of folders)? How to rename a directory/folder on GitHub website? How do I rename both a Git local and remote branch name? Convert row to column header for Pandas DataFrame, Renaming files using node.js Replacement for "rename" in dplyr Rename multiple columns by names Rename specific column(s) in pandas