Rename a branch with git branch -m old-name new-name while you're on a different branch, or git branch -m new-name to rename the current one; then push the new name and delete the old remote with git push origin -u new-name followed by git push origin --delete old-name. These commands work in Git 2.30 and later on Windows, macOS, and Linux, and match what GitHub, GitLab, and Bitbucket expect when a rename must be synced remotely.

Rename a Branch Locally with git branch -m

The -m flag means "move," and it renames a branch ref inside your local repository. Nothing is transmitted to a remote until you explicitly push, so the change is safe to test and easy to reverse.

  1. Confirm your working tree is clean or your changes are committed. Run git status; if files appear under "Changes not staged for commit," commit or git stash them first so the rename can't be confused with in-progress work.
  2. Check the exact current branch name with git branch --show-current. If you're on the branch you want to rename, skip to step 4.
  3. Switch away from the branch you intend to rename only if you're renaming a different branch: git switch main (or whichever branch you want to be active).
  4. Rename the branch with either of these forms:
    git branch -m new-name          # renames the branch you're currently on
    git branch -m old-name new-name # renames a branch by explicit name
    
    The command exits silently on success. Any output usually means the new name already exists, the old name wasn't found, or it equals the new name.
  5. Verify the result with git branch --list or git branch -a. The old name should be gone locally, and the new name should carry a * if it's your current branch.
  6. If you renamed the branch you're on, refresh your shell prompt or editor's branch indicator by reopening the repository folder in VS Code or running git status. Tools cache branch names and can show the stale one until reloaded.

Renaming the branch you're on is a single step because Git relocates the HEAD pointer automatically. Renaming a branch you're not on requires the two-name form, which is the source of most confusion and the reason many users get a "branch not found" error.

Push the Renamed Branch and Delete the Old Remote

A local rename doesn't touch the remote, so both names coexist on GitHub until you clean up. Push the new branch with upstream tracking so future git pull and git push calls resolve automatically.

  1. Push the new name and set it as upstream:
    git push origin -u new-name
    
    The -u flag (short for --set-upstream-to) links your local branch to the remote branch by name, so later pulls and pushes don't prompt again.
  2. Confirm the remote now carries the new branch by visiting the repository's branch list or running git ls-remote --heads origin. You should see refs/heads/new-name.
  3. Delete the stale remote branch:
    git push origin --delete old-name
    
    The remote rejects this if the old branch is the repository's default branch (see the section below) or if branch protection blocks deletion.
  4. Tell collaborators to update their local clones. Anyone with the old name checked out should run:
    git fetch origin
    git branch -u origin/new-name old-name
    git branch -m new-name
    
    This retargets their tracking branch, renames locally, and leaves their working tree untouched.

If the push in step 1 is rejected as ! [rejected] new-name -> new-name (non-fast-forward), your local history diverged from an existing remote new-name. Resolve it by pulling first with git pull --rebase origin new-name, or rename to a genuinely new name.

Rename a Default Branch (Main or Master)

Renaming the default branch carries extra weight: GitHub updates open pull request base branches, redirected URLs, and branch protection policies, but it also closes any open pull request whose head is that branch. That PR closure is automatic and cannot be undone by renaming back.

  1. Rename locally with git branch -m main (or your new name) while checked out on the old default.
  2. Push the new default: git push origin -u main. The remote now has both main and the old name.
  3. Change the repository's default branch on the host, not just locally. On GitHub, open Settings > Branches > Default branch, click the switch icon, choose the new name, and click Update. This step is the actual switch; deleting the old name before it will be refused.
  4. Delete the old name once the default is confirmed changed: git push origin --delete old-name.
  5. In each collaborator's clone, run git fetch --prune origin and git branch -m old-name new-name to align local names with the new remote default.

For the full behavior on redirected URLs, protection-policy migration, and PR closure when the renamed branch is a PR head, see the GitHub branch-renaming documentation. GitLab and Bitbucket expose the default branch in their project settings under similar Repository > Branches paths, but their PR/merge request handling differs slightly, so verify open merge requests before deleting there.

Post-Rename Adjustments and Common Blockers

After the rename looks done, a few loose ends still bite users. These are the ones worth checking:

  • CI configs and scripts: files like .github/workflows/*.yml, Jenkinsfiles, and deploy scripts often hardcode a branch name in on: push: branches: [main]. Rename those references or the pipeline stops triggering.
  • Protected branches: branch protection rules on GitHub are migrated with the rename, but if you delete the old remote before the host confirms the rename, protection can be lost. Always confirm the switch first, then delete.
  • Open pull requests: a PR whose head branch was renamed is closed automatically; a PR whose base is the renamed branch is retargeted. Reopen closed PRs against the new name if work continues.
  • origin/HEAD staleness: run git remote set-head origin -a to rescore the remote default pointer after a default-branch rename, otherwise git branch -r may still flag the old name.
  • Local remote-tracking refs: run git fetch --prune so origin/old-name disappears from your branch list; without it, git branch -a keeps showing the ghost.
  • A rename that "doesn't stick": if the name reverts after a pull, your local branch still tracks the old remote branch. Check with git branch -vv and retarget with git branch -u origin/new-name.

Frequently Asked Questions

Can I rename a branch after pushing it without breaking others' clones?

Yes, but collaborators keep the old name until they fetch and rename locally. Push the new name first, update the host's default branch if applicable, then delete the old remote; anyone who hasn't pulled will see a remote ref does not exist error on their next push and needs to retarget their tracking branch (see the steps above).

What happens if I run git branch -m with a name that already exists?

Git refuses and prints an error such as fatal: A branch named 'new-name' already exists. Pick a different name, delete the conflicting local branch with git branch -D new-name if it's an abandoned duplicate, or use git branch -M old-name new-name to force-overwrite the destination. -M is destructive to the existing destination branch ref, so confirm it has no unmerged work.

Does renaming a branch rewrite commit history?

No. A rename only moves the branch label; commit SHAs, messages, and content are unchanged. That's why a rename is cheap and reversible—rename back with the same git branch -m command and git push origin -u to restore the remote state, provided you haven't already deleted the old remote and no collaborator has pushed under the new name.

How do I rename a branch in a detached HEAD state?

You can't rename the current branch via -m while detached because the current "branch" is a commit, not a ref. Check out the branch you want to rename (git switch old-name), then run git branch -m new-name. If you need to preserve a detached commit as a new named branch, use git switch -c new-name to create one instead.