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
Advertisements