Skip to content

Instantly share code, notes, and snippets.

@teknoraver
Last active September 22, 2020 10:59
Show Gist options
  • Save teknoraver/f09f4ec2267e744f73e9121afad939d6 to your computer and use it in GitHub Desktop.
Save teknoraver/f09f4ec2267e744f73e9121afad939d6 to your computer and use it in GitHub Desktop.
easily switch the default GIT branch from master to main
#!/bin/sh -e
# Switch the default GIT branch from `master` to `main`
# for GitHub users, it can do the remote change too, but:
# 1. assumes that the name of the current directory name is the repo name
# 2. you must provide valid user and token to authenticate
user=
token=
git checkout master
git pull
git checkout -b main
git push -u origin main
# optional, github only
if [ -n "$user" ] && [ -n "$token" ]; then
curl -s -u "$user:$token" \
-H 'Accept: application/vnd.github.v3+json' \
-X PATCH \
"https://api.github.com/repos/$user/${PWD##*/}" \
-d '{"default_branch": "main"}' | grep default_branch
fi
# optional, delete old master
git branch -d master
git push origin :master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment