Q27 of 40 · Git
How do you rename a local and remote branch in Git without breaking existing pull requests?
Short answer
Short answer: Rename locally with `git branch -m old new`, delete the old remote branch, push the new one. Open PRs targeting the old branch name must be re-targeted manually — GitHub/GitLab may auto-redirect for a period but don't rely on it.
Detail
Local rename is straightforward: git branch -m old-name new-name. If you're currently on the branch, just git branch -m new-name (no old name needed).
Remote rename has no single atomic command — you push the new name and delete the old one. Run git push origin new-name to create the new remote branch, then git push origin --delete old-name to remove the old one. Update the upstream tracking ref on your local branch with git branch --set-upstream-to=origin/new-name new-name.
PR impact: any open pull request targeting the old branch name will have its base branch renamed or broken depending on the platform. GitHub auto-redirects the PR base branch when you rename through the UI, but not through the CLI. If you use gh repo edit or the API, GitHub handles the PR retargeting. For CLI renames, manually update each open PR.
Default branch rename (main/master): renaming the default branch (master → main) requires additional steps: update the default branch setting in the remote, update all CI configuration referencing the old name, and communicate to all teammates so they update their local tracking refs with git branch -m master main && git branch --set-upstream-to=origin/main main.
// EXAMPLE
# --- Rename local branch ---
# Rename the branch you're currently on
git branch -m new-feature-name
# Rename a branch you're not on
git branch -m old-feature-name new-feature-name
# --- Update the remote ---
# Push under the new name
git push origin new-feature-name
# Delete the old remote branch
git push origin --delete old-feature-name
# Update your local tracking ref
git branch --set-upstream-to=origin/new-feature-name new-feature-name
# Verify
git branch -vv
# --- Teammate steps (after you rename) ---
# Each teammate who had the old branch checked out:
git fetch origin
git branch -m old-feature-name new-feature-name
git branch --set-upstream-to=origin/new-feature-name new-feature-name