Skip to content

Instantly share code, notes, and snippets.

@jaffarc
Forked from mislav/gh-rename-master
Created June 12, 2020 15:31
Show Gist options
  • Save jaffarc/afa7ababd13282a0c17360c10882ca9e to your computer and use it in GitHub Desktop.
Save jaffarc/afa7ababd13282a0c17360c10882ca9e to your computer and use it in GitHub Desktop.
Rename the default branch of a repository using GitHub CLI https://github.com/cli/cli/releases/tag/v0.10.0
#!/bin/bash
# Usage: gh-rename-master <newbranch> [<remote>]
#
# Renames the "master" branch of the current repository both locally and on GitHub.
#
# dependencies: GitHub CLI v0.10
set -e
newbranch="${1?}"
remote="${2:-origin}"
git fetch "$remote" master
git checkout -b "$newbranch" "${remote}/master" --no-track
git push -u "$remote" "$newbranch"
git remote set-head "$remote" "$newbranch"
# update the default branch
gh api -XPATCH "repos/:owner/:repo" -f default_branch="$newbranch" >/dev/null
# update the base branch of all open pull requests
for num in `gh pr list -B master -L999 | cut -f1`; do
gh api -XPATCH "repos/:owner/:repo/pulls/${num}" -f base="$newbranch" >/dev/null
echo -n .
done
printf '\nDone!\n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment