rename git branches

Let’s rename a git branch locally & remotely.

Let’s assume that you got git repository with multiple branches and you want to mark a branch as a master whereas existing master to something else.

  • Make note of the branch names and their respective remote
git branch -vv
  • Goto server repo and switch default branch
git symbolic-ref HEAD refs/heads/new-master
  • Goto local repo, checkout master
git checkout master
  • Rename it locally
git branch -mv master something
  • Remove remote branch
git push origin --delete master
  • Set new remote
git branch --unset-upstream
git push --set-upstream origin something
  • Goto server repo and switch default branch
git symbolic-ref HEAD refs/heads/something
  • Switch to branch which is going to be new master
git checkout new-master
  • Rename this branch
git branch -mv new-master master
  • Remove remote branch
git push origin --delete new-master
  • Set new remote
git branch --unset-upstream
git push --set-upstream origin master
  • Goto server repo and switch default branch to master
git symbolic-ref HEAD refs/heads/master

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s