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:
Step 2: Check Git Log
Then, display the commit history of the current repository:
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:
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:
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.