Skip to content

Instantly share code, notes, and snippets.

@konrad
Last active June 15, 2020 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save konrad/0ad0358cc3e0a2d6427e92fcd9b6e72e to your computer and use it in GitHub Desktop.
Save konrad/0ad0358cc3e0a2d6427e92fcd9b6e72e to your computer and use it in GitHub Desktop.
Script to mass rename the master branch to main branch in git repos.
#!/bin/env bash
#
# Copyright 2020 Konrad Förstner
#
# Permission to use, copy, modify, and/or distribute this software for
# any purpose with or without fee is hereby granted, provided that the
# above copyright notice and this permission notice appear in all
# copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
# AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
# DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
# PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.b
# Inspired by https://www.hanselman.com/blog/EasilyRenameYourGitDefaultBranchFromMasterToMain.aspx
# Adapt if needed
ROOT_FOLDER=~/
echo "This script will rename local and remote git branches from 'master' to 'main'!"
echo "Use at your own risk!"
for FOLDER in $(find ${ROOT_FOLDER} -name ".git" | sed "s/.git$//")
do
echo ${FOLDER}
read -p "Rename master to main? " yn
case $yn in
[Yy]* ) cd ${FOLDER} && git branch -m master main && git push -u origin main && git push origin --delete master;;
[Nn]* ) echo Skipped repo; continue;;
* ) echo "Please answer yes or no.";;
esac
echo ""
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment