DEV Community

Cover image for Creating an empty branch on an existing git repo
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

Creating an empty branch on an existing git repo

Sometimes you need a separate empty branch for your project.
In my case, because I'm going to switch frameworks. And it's easier to start with an empty repo and work my way up from there.

This repo should have nothing in it, and if it has no git history, that's a win!

Introducing git orphan branches

For this purpose, we can use orphan branches.
These branches are created with nothing in them, as they are orphaned from their ancestor.

Let's go through this process on our existing git test project, which you can find here on GitHub.

Git branch with files and history

As you can see in the image, it has some files and a commit history here.

From our terminal, navigate to the root of this project.
In there, you can run the following command to create an orphan branch.

git checkout --orphan version-2
Enter fullscreen mode Exit fullscreen mode

We need to remove all files that might have been staged in this process by running the following command.

git rm -rf .
Enter fullscreen mode Exit fullscreen mode

Then we have two options. We could add a new readme file or push an empty commit.
The steps for a readme you already know, so let's try out an empty commit.

git commit --allow-empty -m "Starting a new version"
Enter fullscreen mode Exit fullscreen mode

And then we can push this new branch.

git push origin version-2
Enter fullscreen mode Exit fullscreen mode

Fully empty git orphan

As you can see, this branch is empty and has no git history!
This is a perfect way to get started on a new framework or complete rework of your application.

You can view the branch on GitHub.

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Top comments (2)

Collapse
 
waylonwalker profile image
Waylon Walker

This is how I deployed my site for a long time, the rendered version of the site just lived on a pages or site brancch. Then you can point a static hosting platform at that branch. I use gh actions to do the build so that I am not limited on builds. Some days I tend to do a bunch of small deploys.

Collapse
 
dailydevtips1 profile image
Chris Bongers

Still building my current site like that!

GitHub actions builds the static output to a "Netlify" branch and that branch triggers the build on Netlify which only has to pull it.