Skip to content

Instantly share code, notes, and snippets.

@escowles
Last active August 11, 2020 19:07
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save escowles/eed4567d818f77811311386df8e71133 to your computer and use it in GitHub Desktop.
Save escowles/eed4567d818f77811311386df8e71133 to your computer and use it in GitHub Desktop.
#!/bin/sh
# usage:
# $ export GITHUB_TOKEN=[token]
# $ rename-master-to-main.sh [org] [repo]
# generate GITHUB_TOKEN at https://github.com/settings/tokens/new with "repo" privileges
ORG="$1"
REPO="$2"
rm -rf $REPO
git clone git@github.com:$ORG/$REPO.git
cd $REPO
# 1. make sure master exists and main does not # XXX fail if not true
MASTER=`git branch --list master`
MAIN=`git branch --list main`
if [ -z "$MASTER" ]; then
echo "The 'master' branch does not exist, exiting"
exit 1
fi
if [ ! -z "$MAIN" ]; then
echo "The 'main' branch already exists, exiting"
exit 1
fi
git switch master
git pull
if [ $? -gt 0 ]; then
echo "Problems pulling changes from 'master' branch, exiting"
exit 1
fi
git branch -m master main
git symbolic-ref --short refs/heads/master refs/heads/main
git push origin main
if [ $? -gt 0 ]; then
echo "Problems pushing changes to 'main' branch, exiting"
exit 1
fi
JSON="{\"name\":\"$REPO\", \"default_branch\":\"main\"}"
TOKEN="token $GITHUB_TOKEN"
curl -X PATCH -H "Content-Type: application/json" -H "Authorization: $TOKEN" -d "$JSON" https://api.github.com/repos/$ORG/$REPO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment