Orphan Branches in Git
Tags: git, tutorial, beginners, webdev
Published: 2020-12-28 21:21:32

---
title: Orphan Branches in Git
published: true
date: 2020-12-14 03:34:04 UTC
description: Did you know that git supports orphan branches that do not share a commit history of the branch it was created from?
tags: git, tutorial, beginner, webdev
canonical_url: https://shannoncrabill.com/blog/git-orphan-branches/
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/f1xiqoekwe925i6vorfo.jpg
---

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.

## Creating an Orphan Branch

In your project repository, an orphan branch can be created with the following Terminal command.

```shell
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:

```shell
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 `--orphan` flag.

While on your orphan branch, running `git log` will return `your current branch 'landing-page' does not have any commits yet`.

```shell
(base) mycomputer:orphan-branches myname$ git log
fatal: your current branch 'landing-page' does not have any commits yet
```
Whereas `git log` on your main brain will return a list of all previous commits and commit messages.

```shell
(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 <myname@email.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 `.gitignore` file 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.

## Use Cases

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.

## Resources

- [https://git-scm.com/docs/git-checkout](https://git-scm.com/docs/git-checkout)

The post [Orphan Branches in Git](https://shannoncrabill.com/blog/git-orphan-branches/) appeared first on [Shannon Crabill — Front End Software Engineer](https://shannoncrabill.com/blog).