How can I rename a git branch locally and remotely?
1. branch not pushed on remote 2. branch already pushed on remote
Please pay attention to update your answer.
Update Your Answer care fully. After saving you can not recover your old answer
Branch Not Pushed (Local Only)
# Rename current branch
git branch -m new-branch-name
# Or rename from another branch
git branch -m old-branch-name new-branch-name
Branch Already Pushed (Local + Remote)
# Rename locally
git branch -m old-branch-name new-branch-name
# Delete old branch on remote
git push origin --delete old-branch-name
# Push renamed branch to remote
git push origin new-branch-name
# Set upstream
git push --set-upstream origin new-branch-name
Please pay attention to update your answer.
Update Your Answer care fully. After saving you can not recover your old answer
Branch Not Pushed to Remote
If the branch exists only locally:
# Rename the current branch
git branch -m new-branch-name
Or, if you are on a different branch:
git branch -m old-branch-name new-branch-name
No remote updates are needed since the branch hasn’t been pushed.
Please pay attention to update your answer.
Update Your Answer care fully. After saving you can not recover your old answer
Branch Already Pushed to Remote
If the branch exists on remote, follow these steps:
Step 1: Rename locally
git branch -m old-branch-name new-branch-name
Step 2: Delete the old branch on remote
git push origin --delete old-branch-name
Step 3: Push the renamed branch
git push origin new-branch-name
Step 4: Reset upstream tracking (optional)
git push --set-upstream origin new-branch-name
Now your branch is renamed locally and on the remote and future pushes will track the new branch.
