Created
September 27, 2020 12:14
-
-
Save clockworksoul/94830a228b49212d3ce88400058e2ea8 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 = [email protected]:' .git/config \ | |
| sed 's/^.*url = [email protected]://' \ | |
| 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