Last active
June 23, 2020 20:04
-
-
Save sjml/5c4bcc61193538cd018a0e1e7a67d1c8 to your computer and use it in GitHub Desktop.
Rename Git default branch to "main"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/usr/bin/env bash | |
# This is a script to automate changing the name of the | |
# default branch of your git repository from "master" | |
# to "main". Run it in the root directory of your local | |
# clone. | |
# | |
# If you don't want to run this directly (and I wouldn't | |
# blame you!) it can at least serve as a guide to what | |
# you would want to do. | |
# | |
# Note that changing the default branch might mess things up | |
# for collaborators or pull requests. I've only done this | |
# in personal repos where I am the only contributor. | |
# I make no promises for how it might affect your repo. | |
# | |
# If you don't care about updating a central host like | |
# GitHub, then you only need the first two lines. Pretty easy! | |
# | |
# If you're using GitHub, it will also update the remote repo | |
# to make it the new default branch there, but you need to | |
# have their CLI client "hub" installed. | |
# https://hub.github.com | |
# | |
# If you don't trust that process (and again, I wouldn't blame | |
# you!) you can change the default branch manually at | |
# https://github.com/{REPO_OWNER}/{REPO_NAME}/settings/branches | |
# | |
# If you're using another hosting service, I imagine that could | |
# be easily automated, but I don't know how. If you do and feel | |
# inclined to share, I'm happy to add that to this script. | |
# | |
git branch -m master main | |
git push -u origin main | |
# Now to fix up GitHub | |
# Check that you have "hub" installed. There are probably | |
# other ways to do this, but this one is easy. | |
HUB=$(which hub) | |
if [[ $? -ne 0 ]]; then | |
echo "You need to have the GitHub \"hub\" command installed." | |
echo "https://hub.github.com/" | |
exit 1 | |
fi | |
# figure out the owner/reponame | |
repoURL=$($HUB browse --url) | |
parts=(${repoURL//\// }) | |
repoOwner=${parts[2]} | |
repoName=${parts[3]} | |
# use the GitHub API to change the default branch | |
# NB: assumes you have permissions to do so | |
$HUB api -X PATCH /repos/$repoOwner/$repoName --raw-field 'default_branch=main' | |
# gets rid of the old default | |
git push -d origin master |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
๐