Skip to content

Instantly share code, notes, and snippets.

@cherti
Created June 20, 2020 20:31
Show Gist options
  • Save cherti/e1377945e0205d5a949da8e51debd396 to your computer and use it in GitHub Desktop.
Save cherti/e1377945e0205d5a949da8e51debd396 to your computer and use it in GitHub Desktop.
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