Skip to content

Instantly share code, notes, and snippets.

@lpar
Last active June 15, 2020 16:47
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lpar/c74f661a17d52b942bfb8b1fd1496972 to your computer and use it in GitHub Desktop.
Save lpar/c74f661a17d52b942bfb8b1fd1496972 to your computer and use it in GitHub Desktop.
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
# https://tools.ietf.org/id/draft-knodel-terminology-00.html
#
# You must be owner of the repo to change the default branch on Github.
#
# You need the Github 'hub' command line installed,
# see https://hub.github.com/
#
# 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
@lpar
Copy link
Author

lpar commented Jun 10, 2020

Thanks to @GaugeK for cleanup.

Note that this also works for Enterprise Github, if you export GITHUB_HOST=github.example.com 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