Skip to content

Instantly share code, notes, and snippets.

@cjtu
Last active June 18, 2020 00:01
Show Gist options
  • Save cjtu/5af27a5fc573e68ebebec8be9f1c9230 to your computer and use it in GitHub Desktop.
Save cjtu/5af27a5fc573e68ebebec8be9f1c9230 to your computer and use it in GitHub Desktop.
Rename master branch to trunk for all git repos in a directory.

Trunkify

Tl;DR

  1. Run trunkify.sh <GH_username> to rename the master branch of all repos in a directory.
  2. You need to update the default branch for each repo on GitHub (follow urls printed out).

More instructions

First download the .trunkify.sh script and place it in a folder that has git repo(s) in it (e.g. your projects/ folder, NOT in a Git repo itself).

Run from bash with ./trunkify.sh <GH_username>. Username is optional, but will give you links to update your GitHub default branch. The script will rename the master branch in each git repo it encounters to trunk. No pull requests, no messing with Git history.

Renames master to trunk by default, but you can specify an optional second parameter to rename master to something else (ex. ./trunkify.sh <GH_username> main). Other good names are main, latest, default, etc.

All credit to Scott Hanselman, see his blog post here for why I used git branch -m and further instructions on how to tell others to update their local repositories.

This comes with no warranties! Use at own risk.

#!/bin/bash
github_user=${1:-<USERNAME>}
branch_name=${2:-trunk}
GREEN="\033[1;32m"
NOCOLOR="\033[0m"
echo -e "Don't forget to update your default branch on GitHub for the ${GREEN}following repos${NOCOLOR}:"
for d in $(find . -maxdepth 1 -type d); do
if [ -d "$d/.git" ]; then
cd $d
repo=$(basename "$PWD")
git branch -m master $branch_name
git push -u origin $branch_name
echo -e $GREEN
echo "https://github.com/$github_user/$repo/settings/branches"
echo -e $NOCOLOR
cd ..
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment