为什么有时将'remotes/origin/HEAD'映射到'origin/master'而不是? [英] Why is 'remotes/origin/HEAD' sometimes mapped to 'origin/master' and sometimes not?

查看:2958
本文介绍了为什么有时将'remotes/origin/HEAD'映射到'origin/master'而不是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一些存储库中,我看到了...

In some of my repositories I see...

> git branch --all
* master
  remotes/origin/master

...在其他人看来,

...whilst in others I see:

> git branch --all
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

前者使我感到正确,后者使我感到不安.这里发生了什么?

The former strikes me as correct, the latter makes me uneasy. What's going on here?

我以前包括了一个附加的common分支,该分支映射到另一个存储库,并且进行设置可能导致了此问题.现在已删除了这个额外的分支,但后者存储库中的这种奇怪设置仍然存在.在任何一个存储库中都没有其他远程,只有标准的origin远程.

I've previously included an additional common branch mapped to a different repository and setting this up may have caused the issue. This additional branch has now been removed but this odd setup in the latter repository is still there. There are no additional remotes in either repository, just the standard origin one.

在我看来,首先,remotes/origin/masterremotes/origin/master -> origin/master的缩写.仅当本地分支机构和远程分支机构在某些方面有所不同时,才需要明确说明映射吗?那么,有一个奇怪的remotes/origin/HEAD -> origin/master映射,如果那是它的词,似乎优先呢?

It seems to me that, in the first, remotes/origin/master is an abbreviation for remotes/origin/master -> origin/master. Only when the local and remote branches differ in some respect does the mapping need to be spelled out explicitly? So there is this odd remotes/origin/HEAD -> origin/master mapping, if that is the word for it, which seems to be taking precedence?

推荐答案

Mark Adelsberger的答案涵盖了您得到这些(git clone使它们).但是,让我们更笼统地看一下分支名称,然后看一下Git所说的 references symbolicreferences .

Mark Adelsberger's answer covers how you get these (git clone makes them). But let's look more generally at branch names, and then what Git calls references and symbolic references.

您已经知道分支名称相对简单:它们只是像masterdevelop这样的字符串.而且,由于git branch --all输出,您还知道您拥有Git有时所说的远程跟踪分支名称,如origin/master.我更喜欢称呼这些远程跟踪名称,以免过多使用分支"一词.

You already know that branch names are relatively simple: they're just strings like master and develop. And, thanks to git branch --all output, you also know that you have what Git sometimes calls remote-tracking branch names like origin/master. I prefer to call these remote-tracking names, so as to avoid using the word "branch" too much.

您可能不知道的是,这两种都是Git如何将所有内容组合在一起的专门形式.常规分支名称(例如master)实际上是引用,其全名拼写为refs/heads/master.诸如origin/master之类的远程跟踪名称是其全名拼写为refs/remotes/origin/master的引用.标签也只是引用,其全名以refs/tags/开头.

What you may not know is that both of these are just specialized forms of how Git lumps everything together. A regular branch name like master is actually a reference whose full name is spelled refs/heads/master. A remote-tracking name like origin/master is a reference whose full name is spelled refs/remotes/origin/master. Tags, too, are just references whose full name starts with refs/tags/.

如果运行git branch -r(仅列出远程跟踪名称),则会看到origin/master而不是remotes/origin/master.这是因为Git在剥离的refs/remotes/部分中并不总是一致的. (我不知道为什么为什么 Git在这里不一致.)总的来说,Git喜欢将这些内容缩减为更易于管理的内容.由于所有引用始终均以refs/开头-即

If you run git branch -r, which lists only the remote-tracking names, you'll see origin/master instead of remotes/origin/master. This is because Git is not always consistent about how much of the refs/remotes/ part it strips off. (Why Git is inconsistent here, I don't know.) In general Git likes to shrink these things down to something more manageable, though. Since all references always start with refs/—that's how Git defines them—you can, in general, always drop the refs/ part, and usually more: you can usually1 drop refs/heads/, refs/remotes/, and refs/tags/.

引用为您提供的服务-每个引用所做的非常重要的一部分-是保存您在例如git log输出中看到的那些丑陋的Git哈希ID中的一个.在内部,Git 需要这些哈希ID.但是,对于纯人类而言,它们是完全不可能的,因此Git为我们提供了可用来记住ID的名称.

What a reference does for you—well, a very important part of what each reference does—is to save one of those big ugly Git hash IDs that you've seen in, e.g., git log output. Internally, Git needs those hash IDs. They're quite impossible for mere humans to work with, though, so Git gives us names we can use to remember the IDs.

但是任何引用都有一个特殊情况,尽管最明智的做法是将其保留给使用HEAD一词的用户.任何引用都可以是符号引用.引用可以包含另一个引用的名称,而不是包含一个大的丑陋的哈希ID.然后,Git会将名称转换为存储在 other 引用中的哈希ID.

But there's a special case allowed for any reference, although it's wisest to reserve it for ones that use the word HEAD. Any reference can be a symbolic reference. Instead of containing a big ugly hash ID, a reference can contain the name of another reference. Git will then turn the name into the hash ID stored in the other reference.

因此,如果refs/remotes/origin/HEAD包含名称 refs/remotes/origin/master,则可以在可以说origin/master的任何地方说origin/HEAD.这并没有多大帮助-至少,我发现很难用全大写的HEAD键入origin/HEAD,但是Git增加了另一种特殊情况:如果您单独使用名称origin,在Git想要"哈希ID的位置,Git将注意到origin对应于refs/remotes/origin/,然后看到refs/remotes/origin/HEAD存在. Git将阅读它,并看到refs/remotes/origin/HEAD是对refs/remotes/origin/master的符号引用. Git将读取那个并看到一个哈希ID-现在,Git将知道您的意思是哈希ID.

Thus, if refs/remotes/origin/HEAD contains the name refs/remotes/origin/master, you can say origin/HEAD anywhere you could say origin/master. This isn't much help—I, at least, find it harder to type origin/HEAD with its all-uppercase HEAD—but Git adds yet another special case: if you use the name origin by itself, in a place where Git "wants" a hash ID, Git will notice that origin corresponds to refs/remotes/origin/, then see that refs/remotes/origin/HEAD exists. Git will read it and see that refs/remotes/origin/HEAD is a symbolic reference to refs/remotes/origin/master. Git will read that and see a hash ID—and now Git will know the hash ID you meant.

因此,如果origin/HEAD(或refs/remotes/origin/HEAD)指向" origin/master,则意味着您可以在通常必须写出origin/master的几个地方写origin.

So, if origin/HEAD (or refs/remotes/origin/HEAD) "points to" origin/master, that means you can write origin in a few places where you would ordinarily have to write out origin/master.

您可以使用

You can manipulate these special remote-tracking HEAD names using the git remote command, specifically with its set-head sub-command. I never actually bother with any of this myself—I just type in origin/master when I mean that, and ignore the symbolic HEADs that go with these remote-tracking names. But if you like them, they are there for you to use.

1 我在这里说通常"是因为有一种特殊情况:如果您不小心将名称foo作为分支名称(refs/heads/foo)标记名(refs/tags/foo),仅说foo突然变成一个问题.一种解决方案是拼出heads/footags/foo甚至refs/heads/foo.有关详细信息,请参见 gitrevisions文档.

1I say "usually" here because there is a special case: if you accidentally have, say, the name foo as both a branch name (refs/heads/foo) and a tag name (refs/tags/foo), it suddenly becomes a problem to just say foo. One solution is to spell out heads/foo and tags/foo, or even refs/heads/foo. There are more details on this, and much more, in the gitrevisions documentation.

这篇关于为什么有时将'remotes/origin/HEAD'映射到'origin/master'而不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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