git checkout --orphan的作用是什么? [英] What is `git checkout --orphan` used for?

查看:1701
本文介绍了git checkout --orphan的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆