Skip to content

Instantly share code, notes, and snippets.

@matschaffer
Last active January 26, 2022 20:29
Show Gist options
  • Save matschaffer/58d224bd18bba3a84271ddca8c362efe to your computer and use it in GitHub Desktop.
Save matschaffer/58d224bd18bba3a84271ddca8c362efe to your computer and use it in GitHub Desktop.
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
else
echo ":: No upstream remote detected, setting main to track origin"
git branch -u origin/main main
fi
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'
else
echo ":: no fork detected, skipping rename"
fi
else
echo ":: gh not found, skipping fork rename"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment