Creating an empty branch on an existing git repo

Creating an empty branch on an existing git repo

Β·

2 min read

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

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

git rm -rf .

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"

And then we can push this new branch.

git push origin version-2

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

Did you find this article valuable?

Support Daily Dev Tips by becoming a sponsor. Any amount is appreciated!