Last Updated: December 26, 2018
·
4.886K
· aebsr

Git Checkout Orphan

This little alias will automate git checkout of orphan branches as well as:

  • remove the previous branches' contents
  • add a README.md and commit it (for future use & to avoid an empty commit)
  • and lastly push your new branch to git and set the upstream
coo() {
    if [ $1 ]; then
        git checkout --orphan $1;
        git rm -rf .;
        touch README.md;
        git add README.md;
        git commit -m 'orphan branch initial commit';
        git push --set-upstream origin $1;
    fi
}

usage, without the $: $ coo new-branch-name

Now at any point in a project you can start fresh with a clean branch. I tend to use this when prototyping or doing a complete rewrite.

Caveat: Orphan branches can't be (reasonably) merged, though that's kind of the point. via @lettertwo