Skip to content

Instantly share code, notes, and snippets.

@cheecheeo
Created September 3, 2025 22:49
Show Gist options
  • Select an option

  • Save cheecheeo/716a543f760cdd0100179b170bc91006 to your computer and use it in GitHub Desktop.

Select an option

Save cheecheeo/716a543f760cdd0100179b170bc91006 to your computer and use it in GitHub Desktop.
rename main branch to master
#!/bin/bash
# Exit on any error
set -e
# Script to rename the main branch to master and update the default branch on GitHub via API
# TODO, it'd be nice to have this script use `readonly` for variables that should not be changed
# Configuration
# Load environment variables if .env exists
if [ -f .env ]; then
export "$(grep -v '^#' .env | xargs)"
fi
# Get repository owner and name from git remote if not set
if [ -z "$REPO_OWNER" ] || [ -z "$REPO_NAME" ]; then
# Get the remote URL and extract owner/repo
GIT_REMOTE_URL=$(git config --get remote.origin.url)
if [[ $GIT_REMOTE_URL =~ ^https?:// ]]; then
# For HTTPS URLs: https://github.com/owner/repo.git
REPO_PATH=${GIT_REMOTE_URL#*github.com/}
else
# For SSH URLs: [email protected]:owner/repo.git
REPO_PATH=${GIT_REMOTE_URL#*:}
fi
REPO_PATH=${REPO_PATH%.git} # Remove .git suffix if present
# Only extract the values we need
IFS='/' read -r GIT_OWNER GIT_REPO <<< "$REPO_PATH"
[ -z "$REPO_OWNER" ] && REPO_OWNER="$GIT_OWNER"
[ -z "$REPO_NAME" ] && REPO_NAME="$GIT_REPO"
fi
# Set defaults if not provided
GITHUB_TOKEN=${GITHUB_TOKEN:-"your_personal_access_token"}
REPO_OWNER=${REPO_OWNER:-"your_username"}
REPO_NAME=${REPO_NAME:-"your_repo_name"}
# Check if current branch is main
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ "$CURRENT_BRANCH" != "main" ]; then
echo "Error: Current branch is not 'main'. Please switch to the 'main' branch first."
exit 1
fi
# Rename main to master locally
echo "Renaming branch 'main' to 'master'..."
git branch -m main master
# Push the master branch to remote
echo "Pushing 'master' branch to remote..."
git push origin master
# Check if using default token
if [ "$GITHUB_TOKEN" = "your_personal_access_token" ]; then
# Instructions for manual GitHub default branch update
echo "!!! The next time you run this script you can run it with a valid GITHUB_TOKEN to automate this step."
echo "Branch renamed and pushed. Now, update the default branch on GitHub:"
echo "1. Go to your repository on GitHub."
echo "2. Navigate to the 'Settings' tab."
echo "3. Scroll to 'Default Branch'."
echo "4. Click 'Switch to another branch', select 'master', and update."
else
# Update the default branch to master via GitHub API
echo "Setting 'master' as the default branch via GitHub API..."
# Store the response and status code
RESPONSE=$(curl -s -w "\n%{http_code}" -X PATCH \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/$REPO_OWNER/$REPO_NAME" \
-d "{\"default_branch\": \"master\"}" 2>&1)
# Extract HTTP status code and response body
HTTP_STATUS=$(echo "$RESPONSE" | tail -n1)
RESPONSE_BODY=$(echo "$RESPONSE" | sed '$d')
# Handle different HTTP status codes
case $HTTP_STATUS in
200|201|204)
echo "Success: Default branch updated to 'master' on GitHub."
;;
401)
echo "Error: Authentication failed. Please check your GitHub token."
echo "Details: $RESPONSE_BODY"
exit 1
;;
403)
echo "Error: Permission denied. The token may not have the required scopes."
echo "Details: $RESPONSE_BODY"
exit 1
;;
404)
echo "Error: Repository not found. Please check REPO_OWNER and REPO_NAME."
echo "Details: $RESPONSE_BODY"
exit 1
;;
*)
echo "Error: Failed to update default branch. HTTP Status: $HTTP_STATUS"
echo "Response: $RESPONSE_BODY"
exit 1
;;
esac
fi
# Optional: Delete the old main branch
read -p "Do you want to delete the old 'main' branch from the remote? (y/n): " DELETE_CONFIRM
if [ "$DELETE_CONFIRM" = "y" ]; then
echo "Deleting remote 'main' branch..."
git push origin --delete main
echo "Remote 'main' branch deleted."
else
echo "Skipping deletion of remote 'main' branch."
fi
echo "Done! Inform collaborators to update their local repositories with:"
echo "git fetch origin"
echo "git checkout master"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment