git checkout --orphan的作用是什么? [英] What is `git checkout --orphan` used for?
问题描述
我刚刚发现了git checkout --orphan
,但是我不知道如何使用它.其帮助页面上说,它创建了一个新的非父分支.
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.
在master
分支中,我尝试了git checkout --orphan br
,只是看到工作目录中的文件更改为要提交的更改",并且git log
说了fatal: bad default revision 'HEAD'
.
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'
.
那么使用git checkout --orphan
有什么优势?
推荐答案
git checkout --orphan
的核心用途是在非新存储库上以类似git init
的状态创建分支.
The core use for git checkout --orphan
is to create a branch in a git init
-like state on a non-new repository.
如果没有此功能,您的git分支的 all 将会有一个共同的祖先,即您的最初提交.这是很常见的情况,但绝不是唯一的情况.例如,git允许您将多个独立项目作为单个存储库中的不同分支进行跟踪.
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.
这就是为什么您的文件被报告为要提交的更改"的原因:在git init
状态下,尚未创建第一个提交,因此所有文件都是git的新功能.
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.
这篇关于git checkout --orphan的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!