How to Rename a Local and Remote Git Branch
Rename your current local branch and update the upstream remote tracking branch on GitHub or GitLab.
git branch -m <old-name> <new-name>Step-by-Step Execution Guide
git branch -m new-branch-nameRenames your currently checked-out branch to the new name.
git push origin --delete old-branch-nameRemoves the old branch from GitHub/GitLab to avoid stale branch clutter.
git push origin -u new-branch-namePushes the renamed branch and links your local branch to track origin/new-branch-name.
Critical Pitfalls & Precautions
- β’If you have open Pull Requests on GitHub for the old branch, changing the branch name will automatically close or update the PR branch target.
Rename Branch - Frequently Asked Questions
Common questions about safe rollback, remote history implications, and troubleshooting.
Run "git branch -m master main", push with "git push -u origin main", then change the default branch setting in GitHub Repository Settings.
Related Git Command Guides
Browse All Git RecipesUndo your most recent Git commit while keeping all your edited files staged or unstaged in your working directory.
Discard all unstaged and uncommitted file modifications and restore your working tree to the latest commit.
Delete a branch from remote origin (GitHub, GitLab, Bitbucket) after a pull request has been merged.
Amend the commit message of your latest commit before or after pushing.
Remove files from the Git staging area (index) without losing any of your local changes.
Temporarily shelve both modified and newly created untracked files to switch branches with a clean working copy.