Skip to content

How to Create Empty Branch in Git (Without Existing Code)

Covers
Git
Versions & support
About Git →
On this page

Git is a very popular tool among the developers to keep track of changes in their code and manage their projects better. One of the most important feature of Git is called branching. Which helps us to create different versions of our project and work on them separately. In this article, we will learn how to make an empty branch in Git using the --orphan option.

An “orphan” branch in Git is a branch that doesn’t have a parent branch. This means it doesn’t include any of the history from the main or any other branch. This is useful when you want to start a fresh new branch that doesn’t have any of the old code or history.

Here’s how you can create an empty branch (without existing code) using the --orphan option in Git repository.

Git Create Empty Branch

To create a new “orphan” branch, use the command --orphan followed by the name of the branch you want to create. For example, if you want to create a branch named feature_branch, use this command:

git checkout --orphan feature_branch

This command will make a new branch without any history. Next, you can delete all the files in the current directory so they won’t be added to the new branch.

git rm -rf .

Now, you can add new files to this branch and commit them to your repository.

Push the New Branch to the Remote Repository

To make sure the new branch is created, use the git branch command again. This will show all the branches in your repository, and the current branch will have an asterisk (*) next to it.

If you want to upload the new branch to the remote repository (like GitHub), use the command git push command followed by origin and the name of your branch. For example, to upload the feature_branch, execute the following command:

git push -u origin feature_branch

That’s it! You have created an empty “orphan” branch in Git. You can now start working on this new branch without changing the code in the main branch. When you’re ready, you can merge the changes back into the main branch using the git merge command.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top