If you need to rename a Git branch and you do not want to summon the fury of your team this guide helps you do it safely. This covers local rename, pushing the new name to the remote, removing the old remote name, and the tiny etiquette required so your coworkers do not hate you.
You will switch to the branch you want to rename then rename it locally with git branch -m. Next you push the new branch name and set upstream with git push origin -u new-branch. After that you remove the old remote name with git push origin --delete old-branch and tidy things up with git fetch --prune. Finally ask teammates to update their clones so nobody keeps committing to a ghost.
Run these commands from your repository. Yes this is the boring manual part. No magic is involved.
git switch old-branch
# or the old school way
git checkout old-branch
If you are on the branch use this
git branch -m new-branch
If you are not on the branch name it explicitly
git branch -m old-branch new-branch
Commit history stays exactly the same. This is just a label change on your local clone.
git push origin -u new-branch
That pushes the new branch to the remote and makes future git push and git pull use the remote branch by default.
git push origin --delete old-branch
Then clean up local stale remote tracking branches
git fetch --prune
Communication is not optional here. Send a short note with these commands so people can tidy their local repos without drama.
git fetch origin
git branch -m old-branch new-branch
git checkout new-branch
git branch -d old-branch
If you are nervous make a quick backup branch first
git branch backup/old-branch old-branch
git push origin backup/old-branch
This gives you a rollback point that is easy to find if things go sideways.
If someone still sees the old branch after they fetch tell them to prune remote tracking branches
git fetch --prune
And if they have unmerged work on the old branch they can rename it locally then rebase or merge onto the new branch as needed. That is normal Git hygiene and not an apocalyptic event.
The workflow for a safe branch rename in Git is simple and predictable. Switch to the branch, rename locally with git branch -m, push the new name and set upstream with git push origin -u new-branch, delete the old remote name with git push origin --delete old-branch, and ask teammates to fetch and update. Follow those steps and life in your version control system will be marginally less chaotic.
Keywords you will find useful while searching for help on this topic include git, rename branch, git branch rename, git remote, git push, git delete branch, git branch -m, version control, and git workflow. Now go rename that branch and try not to break anything important.
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.