git change branch name code example
Example 1: git rename branch
git branch -m # Any Branch
git branch -m # Current Branch
# For windows if you get "Branch already exists" error
git branch -M
Example 2: how to rename git branch fro master to main
# Step 1
# create main branch locally, taking the history from master
git branch -m master main
# Step 2
# push the new local main branch to the remote repo (GitHub)
git push -u origin main
# Step 3
# switch the current HEAD to the main branch
git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main
# Step 4
# change the default branch on GitHub to main
# https://docs.github.com/en/github/administering-a-repository/setting-the-default-branch
# Step 5
# delete the master branch on the remote
git push origin --delete master
Example 3: git rename remote branch
# Rename the local branch to the new name
git branch -m
# Delete the old branch on remote - where is, for example, origin
git push --delete
# Or shorter way to delete remote branch [:]
git push :
# Push the new branch to remote
git push
# Reset the upstream branch for the new_name local branch
git push -u
Example 4: rename branch git
git branch -m
Example 5: git rename local branch
git branch -m
Example 6: git change branch name
git branch –m old-name new-name