Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
git branches: master -> main

https://tools.ietf.org/id/draft-knodel-terminology-00.html

I used Fish shell because it's my favorite. exa and hub make small things more efficient but you could just as easily use ls and open the GitHub URL some other way. I tend to organize projects by putting everything in a "Code" directory so these scripts are meant to iterate over a bunch of project sub-directories (which may or may not be git repos).

The first script iterates over all sub-directories of the current working directory, creates local & remote main branches derived from master, then opens the GitHub home page for the project. From there, you can select Settings > Branches and change the default branch to main.

You run the second script after making the change on GitHub, which unprotects the previous default branch, so now you can delete and prune it. I did find that one repo, in addition to using it as a default branch, also had an entry under Branch protection rules which protected the "master" branch and needed to be removed.

One final note: another way you cannot delete a branch is if there are any open pull requests hanging off it. So it would be best to merge or close all PRs first, then proceed with the steps above.

#!/usr/bin/env fish
# loop over repos and create local & remote "main" branches
set dirs (exa -D) # or could do the less elegant `ls -d */`
for dir in $dirs
set_color --bold
echo "Entering $dir"
set_color normal
cd $dir
# test for git repo
if git status 2&>/dev/null
git checkout -b main master # create and switch to the main branch
git push -u origin main # push the main branch to the remote and track it
git branch -d master # delete local master
hub browse # open repo github in browser
end
cd -
end
#!/usr/bin/env fish
# loop over repos and delete their origin's "master" branch
set dirs (exa -D) # or could do the less elegant `ls -d */`
for dir in $dirs
set_color --bold
echo "Entering $dir repo and removing origin's master branch"
set_color normal
cd $dir
# test for existence of old branch
if git branch -a 2>/dev/null | ack 'remotes/origin/master' >/dev/null
git remote set-head origin main # not sure I need this...
git push origin --delete master
git remote prune origin
end
cd -
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment