Skip to content

Instantly share code, notes, and snippets.

Last active Jun 15, 2020
What would you like to do?
Change the name of your default Git branch to `trunk`.
#!/bin/sh -e
# This is a terrible hack of a Github script to change the name of your
# default or main branch from 'master' to 'trunk', both in your local
# repo and on Github. I don't usually write shell scripts, so if someone
# wants to clean it up and make it robust, that'd be great.
# As to why you might want to rename your default branch...
# Consider that the entire metaphor of source control is that of a
# tree with branches. The main part of a tree that the branches split
# off from is the trunk. Trees have branches, masters do not, so calling
# the root of a set of branches "master" never made metaphorical sense,
# and "trunk" more clearly expresses the relationship.
# Consider also
# You must be owner of the repo to change the default branch on Github.
# You need the Github 'hub' command line installed,
# see
# People with existing clones of the repo will need to do the following to
# fix up their repos:
# git checkout master
# git branch -m master trunk
# git fetch
# git branch --unset-upstream
# git branch -u origin/trunk
# git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/trunk
# Named elephantoplasty because it's a cosmetic operation, and Monty
# Python.
# Set your Github user name and password here
export GITHUB_USER=myghid
export GITHUB_PASSWORD=12345
git branch -m master trunk
git push -u origin trunk
hub api -t -XPATCH 'repos/{owner}/{repo}' -f default_branch="trunk" > /dev/null
git push origin --delete master

This comment has been minimized.

Copy link
Owner Author

@lpar lpar commented Jun 10, 2020

Thanks to @GaugeK for cleanup.

Note that this also works for Enterprise Github, if you export or whatever your enterprise domain is. You may need to set up an access token to use instead of a password, if your Github Enterprise deployment uses SSO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment