Skip to content

Instantly share code, notes, and snippets.

@clockworksoul
Created September 27, 2020 12:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save clockworksoul/94830a228b49212d3ce88400058e2ea8 to your computer and use it in GitHub Desktop.
Save clockworksoul/94830a228b49212d3ce88400058e2ea8 to your computer and use it in GitHub Desktop.
Simple bash script to create and switch master to main branch
#!/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