Skip to content

Instantly share code, notes, and snippets.

@hisaac
Last active September 2, 2021 20:46
Show Gist options
  • Save hisaac/eb935a923e4656629d19b71725089467 to your computer and use it in GitHub Desktop.
Save hisaac/eb935a923e4656629d19b71725089467 to your computer and use it in GitHub Desktop.
This is a little script I wrote for creating a new, empty default branch for personal forks of a repo. This is to reduce confusion, so that `main`/`master` always points to the upstream version, never your fork's.
function git-orphan --description "Creates an orphan branch and a README.md file"
# Create an orphan branch named "default"
git checkout --orphan default
# Delete everything in the directory that is tracked by git
git rm -rf .
# Delete everything in the directory that is NOT tracked by git
git clean -dfx
# Setting `IFS` to empty allows strings to contain newlines
# source: https://stackoverflow.com/a/34186172/4118208
set -l IFS
# Grab the name of the git repo for the README's title
set -l REPO_TOP_LEVEL (git rev-parse --show-toplevel)
set -l REPO_NAME (basename $REPO_TOP_LEVEL)
# Get the contents of this function to include in the README
set -l FUNCTION_CONTENTS (functions git-orphan | sed "1 d")
# Define the contents of the README
set -l README_CONTENTS (string replace -a ' ' '' "\
# $REPO_NAME
This is an empty branch meant to serve as the default branch for my fork so that `main`/`master` always points to the original repo's default branch.
Here is a gist containing the `fish` function I use to accomplish this: [`git-orphan.fish`](https://gist.github.com/hisaac/eb935a923e4656629d19b71725089467)
After the script runs:
1. Push the new branch up to your fork
1. In GitHub's web UI, set your fork's default branch to this newly created `default` branch
1. Delete the `main`/`master` branch from your fork")
echo $README_CONTENTS > README.md
git add README.md
git commit -m "Create empty default branch"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment