Undo a changeAlways fixable

Rename a branch locally and on the remote

How do I rename a Git branch?

Short answer

git branch -m <new-name> renames the branch you're on. Renaming on the server takes two more steps, because Git has no rename operation for remote branches — you push the new name and delete the old one.

quick fix
git branch -m new-name
git push -u origin new-name
git push origin --delete old-name

Rename locally, publish the new name, remove the old one

Does this match your situation?

  • You made a typo in the branch name.
  • The branch name doesn't match your team's convention or ticket number.
  • You want to rename master to main.

Step-by-step fix

  1. Rename the local branch

    With -m you rename the branch you're currently on. Give two names to rename a different branch without switching to it.

    step 1
    git branch -m new-name
    
    # rename another branch from where you are:
    git branch -m old-name new-name
    
  2. Push the new name and set it as upstream

    The server still has the old branch; this creates the new one alongside it and points your local branch at it.

    step 2
    git push -u origin new-name
    
  3. Delete the old branch on the server

    Only once the new name is pushed and anything depending on it (open pull requests, CI) has been repointed.

    step 3
    git push origin --delete old-name
    

    If the old branch is protected, or a PR still targets it, change that in your host's settings first.

  4. Tell everyone else to update

    Other clones still track the old name and will keep showing it until they prune. This is the step that causes confusion if skipped.

    step 4
    # each teammate runs:
    git fetch --prune
    git branch -m old-name new-name
    git branch -u origin/new-name new-name
    
  5. Renaming master to main

    Same mechanics, plus updating the default branch in your hosting platform's settings so new clones check out the right thing.

    step 5
    git branch -m master main
    git push -u origin main
    # change the default branch in the repo settings on GitHub/GitLab FIRST
    git push origin --delete master
    

Why this works

A branch is a file under .git/refs/heads containing one commit hash, so renaming locally is genuinely just renaming that file — no commits move and nothing is rewritten. Remote branches have no rename operation in the Git protocol at all; you can only create and delete refs. That's why the remote 'rename' is really a create-then-delete, and why everyone else's clone keeps the stale name until they fetch with pruning.

How to stop it happening again

  • Agree a branch naming convention (feature/, fix/, chore/) and stick to it.
  • Set init.defaultBranch main globally so new repositories start correctly.
  • Create branches from the ticket ID to avoid inventing names under pressure.

Commands used in this guide

git branch

List, create, rename and delete branches.

git push

Upload your commits to the remote repository.

git fetch

Download new commits from the remote without changing any of your files.

git switch

Move to another branch - the modern, safer half of git checkout.

Still stuck?

Search the full command reference and every other rescue guide — there are 56 of them, covering everything from detached HEAD to force-push disasters.

Browse all guides →

Frequently asked questions

How do I rename a Git branch?

git branch -m new-name renames the branch you're on. To rename one you're not on, pass both names: git branch -m old-name new-name. Neither touches the remote.

How do I rename a branch on GitHub?

Push the new name with git push -u origin new-name, then delete the old one with git push origin --delete old-name. GitHub also has a rename button in the branch settings, which additionally retargets open pull requests for you.

Will renaming a branch lose my commits?

No. A branch is just a pointer to a commit; renaming changes the label, not the history. Every commit stays exactly where it is.

Related rescue guides

← All Git Rescue guides