You may run into a situation where a branch of a project doesn’t need to share history with the main branch. I had this happen when I wanted to create a temporary landing page for a project I was working on deploying. The landing page would be a “coming soon” page with a short blurb, hosted on the domain the project will eventually live on. I had been working on this project for months, including building React for the frontend and Ruby on Rails for the backend. That’s a lot of files and git history for a temporary, static landing page.
I did some Googling and learned that git supports
orphan branches. An orphan branch, not surprisingly, has no parents (meaning, git history) when it is created. The history of the orphan branch is separate from other branches in the repository, including the main or root branch it was created from.
Here’s a quick example of how to create an orphan branch and use cases.
In your project repository, an orphan branch can be created with the following Terminal command.
git checkout --orphan BRANCH-NAME
In my case, I want my new branch to be named
landing-page. With the
git checkout --orphan landing-page command, a
landing-page branch is created and checked out.
Your terminal will print out something like this:
On branch main nothing to commit, working tree clean (base) mycomputer:orphan-branches myname$ git checkout --orphan landing-page Switched to a new branch 'landing-page'
At first, nothing will look different than if we created a branch without the
While on your orphan branch, running
git log will return
your current branch 'landing-page' does not have any commits yet.
(base) mycomputer:orphan-branches myname$ git log fatal: your current branch 'landing-page' does not have any commits yet
git log on your main brain will return a list of all previous commits and commit messages.
(base) mycomputer:orphan-branches myname$ git checkout main Switched to branch 'main' (base) mycomputer:orphan-branches myname$ git log commit 123.........................abc (HEAD -> main) Author: myname <email@example.com> Date: Mon Dec 28 16:08:50 2020 initial commit of files to git repo (base) mycomputer:orphan-branches myname$
In a use case like mine where your orphan branch does not need the files from the previous branch, type
git rm -rf . to remove all content from that branch.
Note that this will also delete your
.gitignorefile so you’ll to have create a new one, if it’s needed.
You can then commit that change (removing all previous files) and start working in your empty, orphan branch.
What I liked about creating an orphan branch for the landing page, was that it made for one less repository or set of files to keep track of. I can still commit changes on the
landing-page branch and push them to a remote server just like my main or primary branch.
Another plus is that apps like continuous deployment solutions like Digital Ocean or Netlify allow you to select which branch in your git repository it should deploy. Later, when I’ve finished my app on the
main branch, I can make that the branch it listens to without having to do any additional configuration.