Git
Branch Management
Repository Integration
Version Control
Git Workflow

In git, is there a simple way of introducing an unrelated branch to a repository?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Yes. If you want a branch with no shared commit history, the simple Git mechanism is an orphan branch. An orphan branch starts a brand-new root commit inside the same repository, so its history is disconnected from the existing branches.

Create An Orphan Branch

In modern Git, the clearest command is:

bash
git switch --orphan new-history

Older Git versions use:

bash
git checkout --orphan new-history

Git's documentation describes an orphan branch as a new history whose first commit has no parents. That is exactly what you want when the branch should be unrelated to the rest of the repository.

Decide Whether To Keep The Current Tree

When you create an orphan branch, Git adjusts the index and working tree as if you checked out the chosen start point. That means you can either:

  • keep the current files and make a root commit from them
  • clear the tree and replace it with totally different content

If you want a completely different project tree, remove the tracked files first.

bash
git rm -rf .

Then add the new files and commit them.

bash
echo "# fresh project" > README.md
git add README.md
git commit -m "Initial commit on unrelated branch"

Now new-history has its own independent root commit.

When This Is Useful

An orphan branch is useful when you want to:

  • publish a cleaned-up snapshot without exposing old history
  • keep documentation or a static site in the same repository
  • import a separate project tree while preserving a disconnected history
  • experiment with a one-off branch that should not inherit old commits

This is often simpler than creating an entirely separate repository when the storage location should remain shared.

If You Need To Merge Histories Later

An unrelated branch can still be merged later, but Git will refuse by default because the histories have no common ancestor. If that is intentional, use:

bash
git merge other-branch --allow-unrelated-histories

That flag is for the later integration step, not for creating the branch in the first place.

Orphan Branch Versus Separate Repository

An orphan branch keeps everything under one .git directory, including remotes, hooks, and repository permissions. A separate repository gives cleaner isolation.

Choose an orphan branch when the shared repository is intentional. Choose a separate repository when the projects should have fully independent lifecycle and access control.

Be Careful With Existing Files

Because orphan-branch creation can leave the working tree populated, it is easy to accidentally make the first root commit with files you did not intend to carry over. Check git status carefully before the first commit.

bash
git status

That first commit defines the visible starting point for the disconnected history.

Common Pitfalls

A common mistake is creating a normal branch and expecting it to be unrelated. A regular branch always starts from an existing commit, so it shares history.

Another mistake is forgetting to clear the working tree when the new branch should contain totally different files. Git's orphan mode does not automatically produce an empty directory.

It is also easy to confuse orphan branches with later merges. Creating the disconnected history uses --orphan; combining two unrelated histories later may require --allow-unrelated-histories.

Summary

  • Use git switch --orphan name or git checkout --orphan name to create an unrelated branch.
  • The first commit on that branch becomes a new root commit with no parents.
  • Clear the working tree if the new history should contain different files.
  • Use --allow-unrelated-histories only when merging disconnected histories later.
  • Check git status before the first commit so the root snapshot is intentional.

Course illustration
Course illustration

All Rights Reserved.