Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is `git checkout --orphan` used for?

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?

like image 693
jsvisa Avatar asked Nov 14 '13 14:11

jsvisa


People also ask

What is git 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.

What is checkout used for in git?

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.

What is the difference between git checkout and Branchname checkout?

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.


2 Answers

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.

like image 179
JB. Avatar answered Oct 13 '22 21:10

JB.


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.

like image 43
Fred Foo Avatar answered Oct 13 '22 20:10

Fred Foo