Git

What is “git checkout –orphan” Used For?

While dealing with large development projects on Git, developers work on multiple branches. They work on a separate branch for each feature and combine it with the main branch. However, sometimes, you do not want to share the history of a specific branch with the main branch. In this situation, Git allows you to create a separate orphan branch.

This article will explain:

Why is “git checkout –orphan” Used?

The “git checkout –orphan” is a command in Git used to create or make a new orphan branch in Git with no commit history. The orphan branch is an independent branch that starts with a separate root commit. Its main purpose is to create/make a branch in a Git init-like state on the repository.

How to Use the “git checkout –orphan” Command in Git?

To utilize the “git checkout –orphan” command in Git, first, redirect to the local repository and view its commit history. Then, create an orphan branch using the “git checkout –orphan <branch-name>” command. Lastly, check the Git log to verify changes.

Step 1: Navigate to Local Repository

First, utilize the following command and redirect to the desired local repository:

cd "C:\Git\Repo1"

Step 2: Check Git Log

Then, display the commit history of the current repository:

git log --oneline

In the below output, all the commits history of the current repository can be seen:

Step 3: Create Orphan Branch

To create an orphan branch in Git, use the “git checkout” command with the “–orphan” option and the desired orphan branch name. For instance, we have specified the “orpBranch” name for our orphan branch:

git checkout --orphan orpBranch

It can be observed that the orphan branch has been created, and we have switched to it successfully:

Step 4: View Commit History

Lastly, view the Git log to check the commit history of the orphan branch:

git log --oneline

As you can see the orphan branch does not contain any commit history:

That was all about the “git checkout –orphan” command in Git.

Conclusion

The “git checkout –orphan” is a Git command used for creating a new orphan branch in Git with no commit history. The orphan branch is a separate branch that begins with a distinct root commit. Its main purpose is to create a branch in a Git init-like state on the repository. To create/make an orphan branch, utilize the “git checkout –orphan <branch-name>” command. This article explained the usage of the “git checkout –orphan” command in Git.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.