-
-
Save crispd/a4e6c18b452ea8333c1c329167e70dd7 to your computer and use it in GitHub Desktop.
Simple bash script to create and switch master to main 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 | |
| set -euo pipefail | |
| function git_branch { | |
| git branch 2>&1 | grep '^\*' | sed 's/\* //g' | |
| } | |
| function git_repo { | |
| grep 'url = git@github.com:' .git/config \ | |
| | sed 's/^.*url = git@github.com://' \ | |
| | sed 's/\.git$//' | |
| } | |
| function git_url { | |
| echo "https://github.com/$(git_repo)/settings/branches" | |
| } | |
| if ! git status 2>&1 | grep -q "nothing to commit"; then | |
| echo "Modifications detected. Aborting" | |
| exit 1 | |
| fi | |
| if [[ $(git_branch) != "master" ]]; then | |
| echo "Will only execute from master branch" | |
| exit 1 | |
| fi | |
| if git branch 2>&1 | grep -q 'main'; then | |
| echo "Found existing main branch: switching" | |
| git checkout main | |
| else | |
| echo "Creating and switching to main branch" | |
| git checkout -b main | |
| fi | |
| if [[ $(git_branch) != "main" ]]; then | |
| echo "Why aren't we on the main branch?" | |
| return 1 | |
| fi | |
| git rebase master | |
| git push --set-upstream origin main | |
| echo "Main branch created. Please update default branch now." | |
| open "$(git_url)" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment