7

Orphan Branches in Git

 3 years ago
source link: https://shannoncrabill.com/blog/git-orphan-branches/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Shannon Crabill — Front End Software EngineerSkip to content

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.

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.

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. Whereas git log on your main brain will return a list of all previous commits and commit messages.

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.

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK