All Articles Kinsa Creative

Rename a Git Branch on BitBucket or GitHub

GitHub has a convenient renaming function and outlined process.

Begin by renaming the repository on GitHub. On the web GUI, from the repository's branches, use the menu to rename the branch:

Then, locally, rename old-branch-name with new-branch-name:

git branch -m old-branch-name new-branch-name

Fetch the branches and histories for the repo from GitHub:

git fetch origin

Align the local and remote branches. Set the upstream branch name to match:

git branch -u origin/new-branch-name new-branch-name

Optionally, set the remote default branch to match the new branch name. Having a default branch for a remote is not required, but allows the name of the remote to be specified in lieu of a specific branch.

git remote set-head origin -a

BitBucket

BitBucket lacks the renaming tool. The process is similar to GitHub but cal all be done entirely locally beginning with renaming the branch. This same process can be used to do things entirely locally for GitHub.

Rename old-branch-name with new-branch-name:

git branch -m old-branch-name new-branch-name

Align the local and remote branches. Set the upstream branch name to match:

git branch -u origin/new-branch-name new-branch-name

Push to remote:

git push origin new-branch-name

Optionally, set the remote default branch to match the new branch name. Having a default branch for a remote is not required, but allows the name of the remote to be specified in lieu of a specific branch.

git remote set-head origin new-branch-name

If the old branch name was the Main branch on BitBucket, log in to BitBucket and change the Main branch to the new name or it will not allow the old branch to be deleted. This is done in the Advanced panel of the Repository Settings page.

Delete the old branch from BitBucket:

git push origin -d old-branch-name

Feedback?

Email us at enquiries@kinsa.cc.