Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A bash helper for switching a local clone and a github fork from master to main.
#!/usr/bin/env bash
set -euo pipefail
echo ":: Renaming local master to main"
git branch -m master main || true
git fetch origin
git remote set-head origin -a
if git remote show upstream >/dev/null 2>&1; then
echo ":: Upstream remote detected, setting main to track upstream"
git fetch upstream
git branch -u upstream/main main
git remote set-head upstream -a
echo ":: No upstream remote detected, setting main to track origin"
git branch -u origin/main main
if command -v gh &> /dev/null; then
echo ":: gh available, attempting fork rename"
gh_user=$(gh api /user -q .login)
gh_repo=$(gh repo view --json nameWithOwner -q .nameWithOwner)
fork_name=$(gh api "repos/${gh_repo}/forks" -F "owner=${gh_user}" -q ".full_name")
if [[ -n "${fork_name}" ]]; then
echo ":: renaming ${fork_name} master to main"
gh api --silent "repos/${fork_name}/branches/master/rename" --method POST --field 'new_name=main'
echo ":: no fork detected, skipping rename"
echo ":: gh not found, skipping fork rename"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment