Created
June 24, 2020 16:40
-
-
Save mark-raymond/83a840163b49fc906746e54af2c688d5 to your computer and use it in GitHub Desktop.
Script to reset the default branch
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 | |
| if [ $# -ne 1 ]; then | |
| echo "Usage: $0 GITHUB_TOKEN" >&2 | |
| exit 1 | |
| fi | |
| TOKEN="$1" | |
| ORIGIN="$(git remote get-url origin)" | |
| if [ $? -ne 0 ]; then | |
| echo "Could not get origin url in $(pwd)" >&2 | |
| exit 1 | |
| fi | |
| if ! grep '^\(git@github.com:\|https://github.com/\)\([^/]\+/[^/]\+\)\.git$' <<< "$ORIGIN" >/dev/null 2>/dev/null; then | |
| echo "origin url $ORIGIN is not a recognzied GitHub repository url" >&2 | |
| exit 1 | |
| fi | |
| REPO="$(sed -e 's#^\(git@github.com:\|https://github.com/\)\([^/]\+/[^/]\+\)\.git$#\2#' <<< "$ORIGIN")" | |
| DEFAULT_BRANCH="$(curl -s -L -H "Authorization: token $TOKEN" -H 'Accept: application/vnd.github.v3+json' "https://api.github.com/repos/$REPO" | grep 'default_branch' | sed -e 's/.*"default_branch"\s*:\s*"\([^"\]\+\)".*/\1/')" | |
| git symbolic-ref refs/remotes/origin/HEAD "refs/remotes/origin/$DEFAULT_BRANCH" |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can run this in all repos in a folder using: