I've just discovered git checkout --orphan
, but I don't know how to use it. Its help page says it creates a new unparented branch.
In the master
branch, I've tried git checkout --orphan br
, only to see the files in the working directory change to “Changes to be committed”, and the git log
saying fatal: bad default revision 'HEAD'
.
So what's the advantage of using git checkout --orphan
?
An orphan branch, not surprisingly, has no parents (meaning, git history) when it is created. The history of the orphan branch is separate from other branches in the repository, including the main or root branch it was created from.
The git checkout command lets you navigate between the branches created by git branch . Checking out a branch updates the files in the working directory to match the version stored in that branch, and it tells Git to record all new commits on that branch.
7 Answers. Show activity on this post. git checkout -b BRANCH_NAME creates a new branch and checks out the new branch while git branch BRANCH_NAME creates a new branch but leaves you on the same branch. In other words git checkout -b BRANCH_NAME does the following for you.
The core use for git checkout --orphan
is to create a branch in a git init
-like state on a non-new repository.
Without this ability, all of your git branches would have a common ancestor, your initial commit. This is a common case, but in no way the only one. For example, git allows you to track multiple independent projects as different branches in a single repository.
That's why your files are being reported as “changes to be committed”: in a git init
state, the first commit isn't created yet, so all files are new to git.
It's used by e.g. GitHub Pages, which stores a repo's website inside the repo but on a separate branch. There's no reason to store anything but the website's history on this branch.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With