Git: Create Empty Branch

By default, the commands git branch <newBranchName> and git checkout -b <newBranchName> create a new branch from the current one.

This means that the new branch will be based on your currently checked out (HEAD) branch, i.e. will contain all the files and commits from the parent branch.

Let’s assume that you want to create a new empty branch without any inherited files or commits.

This short note shows how to do this.

Cool Tip: How to squash commits in Git before merging a branch! Read More →

Create an Empty Branch in Git

To create a new empty branch in Git, we can use the --orphan command line option:

$ git checkout --orphan <newEmptyBranchName>

The command above creates the new empty branch and switches into it.

Once the empty branch s created, we can can delete files from the working directory, so they are not committed in to the new branch:

$ git rm -rf .

Now you are in the empty branch without any inherited files or commits.

If you want to push your empty branch to a remote repository, do the following:

$ git commit --alow-empty -m "Init"
$ git push origin <newEmptyBranchName>

Note, that if you try to merge another branch into the empty one, you will receive the error:

fatal: refusing to merge unrelated histories

Use the --allow-unrelated-history option to force the merge into the empty branch:

$ git merge --allow-unrelated-history <branchName>

Cool Tip: Move back and forth between two branches in Git! Read more →

Leave a Reply