Git Rename Branch Example |Video upload date:  · Duration: PT4M35S  · Language: EN

Step by step guide to rename a Git branch locally and on remote with safe commands and cleanup tips for teams

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.

Quick idea of what will happen

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.

Step by step commands

Run these commands from your repository. Yes this is the boring manual part. No magic is involved.

1 Switch to the branch you want to rename

git switch old-branch
# or the old school way
git checkout old-branch

2 Rename the branch locally

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.

3 Push the new name and set upstream

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.

4 Remove the old name from the remote

git push origin --delete old-branch

Then clean up local stale remote tracking branches

git fetch --prune

How to ask your teammates to update their clones

Communication is not optional here. Send a short note with these commands so people can tidy their local repos without drama.

  • Fetch the updated remote refs
git fetch origin
  • Either rename their local branch if they kept the old name
git branch -m old-branch new-branch
  • Or check out the new branch name and delete the obsolete local name
git checkout new-branch
git branch -d old-branch

Troubleshooting and safety tips

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.

Wrap up

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.