Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
help switching a git repo's default branch from master to main
#!/bin/bash
# this script will assist you in migrating git-repositories from
# the primary branch 'master' to the primary branch 'main'.
# It currently works with only one remote (adjust accordingly if you
# have more than one), the name of which is taken as the first argument.
#
# Example: bash switch_branch.sh origin
#
# It will
# 1. create a new branch main
# 2. push this new branch to the specified remote (if you confirm)
# 3. delete the upstrem master-branch (if you confirm again)
# 4. set the local default branch for a remote (optional, does not
# if you haven't confirmed 3 and 4 due to how shellscripts work.
git checkout -b main
echo ""
echo " :: git push -u $1 main? (Enter to proceed, Ctrl+C to abort)"
read
git push -u $1 main
echo ""
echo " :: Now switch the default branch upstream, e.g. in the GitHub-Settings, before proceeding."
echo " :: delete upstream master? (Enter to proceed, Ctrl+C to abort)"
read
git push $1 --delete master
# finally query the remote for the new head and set it
# if you want to do this manually, take a look at git remote set-head
# in man git-remote
git remote set-head --auto $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.