Skip to content

Instantly share code, notes, and snippets.

@iAugur
Last active February 4, 2021 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save iAugur/2f5d98d7f1dc525030c9a0c30a77bd30 to your computer and use it in GitHub Desktop.
Save iAugur/2f5d98d7f1dc525030c9a0c30a77bd30 to your computer and use it in GitHub Desktop.
# Steps for renaming your master branch to 'main'
# Adapted from
# https://dev.to/rhymu8354/git-renaming-the-master-branch-137b
# https://www.hanselman.com/blog/EasilyRenameYourGitDefaultBranchFromMasterToMain.aspx
#---------------------------------------------------------------------------------------------#
# A) - Steps if you have no remote / upstream
# B) - Steps if you are the repo owner and have a remote / upstream
# C) - Steps if you are using a repo where the upstream main branch has been renamed
# D) - Steps to make new repos use 'main' by default
#---------------------------------------------------------------------------------------------#
# A) if you have no remote/upstream just rename the branch.
git branch -m master main
#---------------------------------------------------------------------------------------------#
# B) You are the maintainer and want to rename your main branch
# 1. Pull remote first
git pull origin master
# 2.Rename your local branch.
git branch -m master main
# 3. Push renamed branch upstream and set remote tracking branch.
git push -u origin main
# 4. Log into the upstream repository host (GitHub, GitLab, Bitbucket, etc.)
# & change the "default branch".
# & check for any branch protection you have
# https://help.github.com/en/github/administering-a-repository/configuring-protected-branches
# In Bitbucket you will need to change the main branch to the new 'main' branch
# Repository Details->Advanced
# 5. Delete the old branch upstream.
git push origin --delete master
# 6. Update the upstream remote's HEAD.
git remote set-head origin -a
#---------------------------------------------------------------------------------------------#
# C) Clients need to update their local repos
# 1. Fetch the latest branches from the remote.
git fetch --all
# 2. Update the upstream remote's HEAD.
git remote set-head origin -a
# 3. Switch your local branch to track the new remote branch.
git branch --set-upstream-to origin/main
# 4. Rename your branch locally.
git branch -m master main
# 5. Prune orphaned refs for deleted remote branch
git fetch --all -p
#---------------------------------------------------------------------------------------------#
# D) For new repos you can make 'main' the default
# 1) when initialising a new repo
git init --initial-branch=main
git checkout -b main
# 2) To make this the default from now on
mkdir -p ~/.config/git/template
echo 'ref: refs/heads/main' > ~/.config/git/template/HEAD
git config --global init.templateDir ~/.config/git/template/
# After that, you can just use 'git init'
# these last two tips come from:
# https://twitter.com/spences10/status/1277884527629623296
# https://twitter.com/mathias/status/1277896371455090688
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment