“(无分支)"与“(无分支)"之间的区别是和“(在abc1234处分离)"和“ [英] Difference between "(no branch)" and "(detached at abc1234)"

查看:104
本文介绍了“(无分支)"与“(无分支)"之间的区别是和“(在abc1234处分离)"和“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,当您在git存储库中运行类似这样的内容时:

Normally when you run something like this inside of a git repository:

git checkout abc1234

您最终处于分离的HEAD状态.如果运行git branch,则输出将如下所示:

You end up in a detached HEAD state. If you run git branch, the output will look something like this:

* (detached from abc1234)
  master

这很好并且是预期的行为.

This is fine and expected behaviour.

我最近一直在玩pygit2,遇到了一些我以前从未见过的东西.假设我执行以下操作:

I've been playing around with pygit2 recently, and have come across something I haven't seen before. Let's say I do the following:

repo = pygit2.discover_repository("/path/to/repo")
repo.head = "abc1234"

我希望存储库处于分离的HEAD状态.出于所有意图和目的,我相信是在这样做之后.但是,git branch的输出看起来有点不同:

I would expect the repository to be in a detached HEAD state. For all intents and purposes, I believe it is after doing this. However, the output from git branch looks a bit different:

* (no branch)
  master

有人知道区别是什么,为什么有区别,这意味着什么?

Does anyone know what the difference is, why there is a difference, and what it means?

以下是使用pygit2克隆存储库,将提交SHA1哈希分配给repo.head,然后运行git checkout master,然后运行git checkout myhash之后的reflog:

Below is the reflog after cloning a repository using pygit2, assigning a commit SHA1 hash to repo.head, then running git checkout master, then running git checkout myhash:

69df316 HEAD@{0}: checkout: moving from master to 69df3161f315e9b13ba4bd811635c11f67616598
d6ece61 HEAD@{1}: checkout: moving from 69df3161f315e9b13ba4bd811635c11f67616598 to master
69df316 HEAD@{2}:
d6ece61 HEAD@{3}: clone: from file:///path/to/repo

推荐答案

处于分离的HEAD"状态时,git会根据引用日志的内容显示(无分支)"或(与abc分离)"项.

When in a "detached HEAD" state, git will show "(no branch)" or "(detached from abc)" depending on the contents of the reflog entry.

在代码中,您仅覆盖值而没有提供任何消息,因此没有消息写入reflog(如您在reflog中的HEAD@{2}条目中所见).如果有类似的"checkout"消息,则会显示分离的文本.

In your code, you simply overwrite the value without providing any message and thus no message is written to the reflog (as you can see in the HEAD@{2} entry from your reflog). If there were a similar "checkout" message, the detached text would appear.

pygit2提供了Reference.log_append()以便将条目追加到日志,因此您可以创建这样的条目,尽管当前它仍会创建一个空条目).解决方案是,一旦它支持libgit2 0.21中引入的更新的reflog处理,就使用update方法.

pygit2 provides Reference.log_append() to append entries to the log, so you can create such an entry, though currently it would still create the empty one). The solution would be to use the update method once it supports the newer reflog handling which was introduced in libgit2 0.21.

引用设置方法Reference.set_target()Repository.set_head()提供了一个放置您自己的身份和消息的地方,可用于提供与git的checkout命令所创建的消息等效的消息.

The reference-setting methods Reference.set_target() and Repository.set_head() provide a place to put your own identity and message for the reflog, which you can use to provide a message equivalent to the one git's checkout command would create.

您可以尝试按当前状态进行更新,然后手动在reflog中写入条目(该条目位于.git/logs/HEAD下,这是一个文本文件),以模仿git编写的内容,您应该看到((与abc分离)"消息.

You can try doing the update as you currently are and writing by hand the entry in the reflog (it's under .git/logs/HEAD and it's a text file) to mimic what git would write and you should see the "(detached from abc)" message appear.

这篇关于“(无分支)"与“(无分支)"之间的区别是和“(在abc1234处分离)"和“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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