How to Create an Empty Branch on GitHub

John Wachira Feb 15, 2024
  1. Create an Empty Branch on GitHub
  2. Conclusion
How to Create an Empty Branch on GitHub

This article outlines the process of creating an empty git branch on the command line. We will then push the empty branch to a GitHub repository.

We know we cannot push an empty branch to a remote repository. However, there is a way around this.

Create an Empty Branch on GitHub

The example below shows a local repository with a master branch. We want to create an empty release branch in our repository.

Of course, we cannot create an empty branch the conventional way. If we do, we will create a release branch with the commit history borrowed from our master branch.

We will use the command below to create an empty release branch.

Command:

$ git switch --orphan release

This should create an empty branch without commits or files. Let’s run the git log command to confirm our case.

check if the created branch does not have commits yet

We now have an empty release branch. Since we cannot push an empty branch to the remote, we will have to create an empty commit in our release branch.

Command:

$ git commit --allow-empty -m "Initial commit on orphan branch"

This will create a commit that does not have any files. We can now push the branch to the remote, as shown below.

create a commit without files and push branch to the remote branch

Command:

$ git push -u origin release

This should push our empty branch to our remote repository on GitHub. Let’s verify if this is the case.

push empty branch to remote repository on GitHub

There you have it. An empty release branch on GitHub.

Conclusion

In a nutshell, you can create an empty branch using the --orphan flag with the git switch command. You can also use the git checkout command, but you must clean your index before committing.

Failure to which you will create a commit with the files present on your index.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Branch