-
-
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
This file contains hidden or 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
| #!/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