Creating an empty branch on an existing git repo

โœ๏ธ

Creating a completly empty git orphan branch

14 Nov, 2021 ยท 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. 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. You already know the steps for a readme, 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

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next ๐Ÿ“–

Undo wrong Git changes

30 Jul, 2022 ยท 3 min read

Undo wrong Git changes

Git basics: Changing your last commit message

19 Jul, 2022 ยท 2 min read

Git basics: Changing your last commit message