Subscribe

Git create an empty branch without history

✍️

Tutorial to create an orphan branch without history, making your GitHub repository ready for new beginnings.

10 Feb, 2024 · 2 min read

Sometimes you need a separate empty branch in your git repository.

In my case, because I’m going to switch frameworks. So it’s easier to start with an empty repo and work my way up from there.

This repo should have no files in it, and no git history:

empty git branch

Let’s get started!

Introducing Orphan Branches

For this purpose, we can use an orphan branch. A Git orphan branch is a new branch that starts without any commit history, effectively making it an independent line of development separate from the rest of the repository.

Typically you use an orphaned git branch to create new unrelated files in the repo, for example when you code something up from scratch again.

How to create an empty branch without history

Let’s go through the process of creating an empty orphan branch.

We’ll use 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 folder of the repository.

Run this command to create an empty orphan branch:

git checkout --orphan version-2

Now we need to remove files that might have been staged in this process.

Next, run the following git command:

git rm -rf .

The command is used to recursively (-r) and forcefully (-f) remove every file and directory in the current directory (.)

Then we have two options:

  • We could add a new readme file
  • push an empty commit as first commit

You probably already know the steps to add a readme file, so let’s do the empty commit.

git commit --allow-empty -m "Starting a new version"

This git command creates a new commit. The --allow-empty doesn’t require any changes to be committed.

Next, we do an empty push to the new branch.

git push origin version-2

empty git branch

As you can see, the branch is empty and has no git history!

This is a perfect way to get started on a new framework or to recode of your application.

On Github you can view the orphan branch.

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

Join 2097 devs and subscribe to my newsletter