为什么在运行“git branch -r”时会显示“origin/HEAD”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/354312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:00:50  来源:igfitidea点击:

Why is "origin/HEAD" shown when running "git branch -r"?

gitversion-controlgithub

提问by Ben Hamill

When you run git branch -rwhy the blazes does it list origin/HEAD? For example, there's a remote repo on GitHub, say, with two branches: master and awesome-feature. If I do git cloneto grab it and then go into my new directory and list the branches, I see this:

当你跑步时,git branch -r为什么会列出火焰origin/HEAD?例如,GitHub 上有一个远程仓库,有两个分支:master 和 awesome-feature。如果我git clone抓住它然后进入我的新目录并列出分支,我会看到:

$ git branch -r
origin/HEAD
origin/master
origin/awesome-feature

Or whatever order it would be in (alpha? I'm faking this example to keep the identity of an innocent repo secret). So what's the HEADbusiness? Is it what the last person to pushhad their HEADpointed at when they pushed? Won't that always be whatever it was they pushed? HEADs move around... why do I care what someone's HEADpointed at on another machine?

或者它的顺序是什么(alpha?我在伪造这个例子是为了保密一个无辜回购的身份)。那么到底是做什么HEAD生意的呢?是他们推动时最后一个push被他们HEAD指着的人吗?那不会永远都是他们push编的吗?HEAD四处走动……我为什么要在意别人HEAD在另一台机器上指着什么?

I'm just getting a handle on remote tracking and such, so this is one lingering confusion. Thanks!

我刚刚掌握了远程跟踪等方面的知识,所以这是一个挥之不去的困惑。谢谢!

EDIT: I was under the impression that dedicated remote repos (like GitHub where no one will ssh in and work on that code, but only pull or push, etc) didn't and shouldn't have a HEAD because there was, basically, no working copy. Not so?

编辑:我的印象是,专用远程存储库(例如 GitHub,没有人会通过 ssh 进入并处理该代码,而只会拉或推等)没有也不应该有 HEAD,因为基本上,没有工作副本。不是这样?

采纳答案by cdunn2001

@robinstis correct.

@robinst是正确的。

In git, you can select which branch is checked out by default (i.e. when you clone). By default, origin/HEADwill point at that.

在 git 中,您可以选择默认检出哪个分支(即克隆时)。默认情况下,origin/HEAD将指向那个。

On GitHub, You can change thisin the Admin settings for your GitHub repo. You can also do it from the command-line via

在 GitHub 上,您可以在 GitHub 存储库的管理设置中更改此设置。您也可以通过命令行执行此操作

git remote set-head origin trunk

or delete it altogether via

或通过

git remote set-head origin -d

Example. Look at the 'Switch Branches' drop-down. trunkis checked, so origin/HEADfollows trunk.

例子。查看“切换分支”下拉列表。trunk被检查,所以origin/HEAD如下trunk

回答by robinst

The reason a bare repository can have a HEAD, is that because it determines which branch is initially checked out after a clone of the repository.

裸仓库可以有 HEAD 的原因是因为它决定了在克隆仓库后最初检出哪个分支。

Normally, HEAD points to master, and that is the branch that is checked out when people clone the repository. Setting it to another branch (by editing HEAD in the bare repository) results in that branch being checked out on clone.

通常情况下,HEAD 指向 master,这是人们克隆存储库时检出的分支。将其设置为另一个分支(通过在裸存储库中编辑 HEAD)会导致该分支在克隆时被检出。

回答by boblu

I was under the impression that dedicated remote repos (like GitHub where no one will ssh in and work on that code, but only pull or push, etc) didn't and shouldn't have a HEAD because there was, basically, no working copy. Not so?

我的印象是专用的远程存储库(例如 GitHub,没有人会通过 ssh 进入并处理该代码,而只会拉或推等)没有也不应该有 HEAD,因为基本上没有工作复制。不是这样?

I had the exact same impression like you said.

我和你说的一模一样的印象。

And I even can not delete that origin/HEAD remote-tracking branch cloned from github by doing

我什至无法删除从 github 克隆的 origin/HEAD 远程跟踪分支

git branch -d -r origin/HEAD

This had no effect.

这没有效果。

Can some one tell me how I can delete that origin/HEAD remote-tracking branch?

有人能告诉我如何删除那个 origin/HEAD 远程跟踪分支吗?

update

更新

Though I did not found why there is a origin/HEAD created when clone from github, I find a way to delete it.

虽然我没有发现为什么从 github 克隆时会创建一个 origin/HEAD,但我找到了一种删除它的方法。

The new version of git provide

新版git提供

git remote set-head <name> -d

to delete the useless HEAD pointer of remote-tracking branch.

删除远程跟踪分支无用的 HEAD 指针。

And we can also change the dumb default name 'origin' to whatever we want by using

我们还可以使用

git remote rename origin <new_name>

Hope this can help. :)

希望这能有所帮助。:)

回答by Paul

You're right that pushing todedicated remote repos work much better when they are 'bare', that is, when they don't have working directories. Git's architecture is designed for updating by patches or pull(fetch), which makes sense in a distributed VCS. As the docs say somewhere, pushing to a branch which is currently checked out can result in "unexpected results".

您是对的,当它们“裸”时,即当它们没有工作目录时,推送到专用远程存储库的效果会更好。Git 的架构旨在通过补丁或pull( fetch)进行更新,这在分布式 VCS 中很有意义。正如文档在某处所说,推送到当前已签出的分支可能会导致“意外结果”

The HEAD is part of the requirements for a valid repository. Git Repository Layoutsays, in part:

HEAD 是有效存储库要求的一部分。Git 存储库布局部分说:

HEAD

A symref (see glossary) to the refs/heads/ namespace describing the currently active  
branch. It does not mean much if the repository is not associated with any working tree  
(i.e. a bare repository), but a valid git repository must have the HEAD file; some  
porcelains may use it to guess the designated "default" branch of the repository  
(usually master). It is legal if the named branch name does not (yet) exist.

So you're going to see HEAD as part of the branch list, even if "it does not mean much..."

所以你会看到 HEAD 作为分支列表的一部分,即使“它没有多大意义......”

回答by Walker Hale IV

If "origin" is a remote repository, then origin/HEAD identifies the default branch on that remote repository.

如果“origin”是远程存储库,则 origin/HEAD 标识该远程存储库上的默认分支。

Example:

例子:

$ git remote show
origin
$ git remote show origin
* remote origin
  Fetch URL: [email protected]:walkerh/pipe-o-matic.git
  Push  URL: [email protected]:walkerh/pipe-o-matic.git
  HEAD branch: master
  Remote branch:
    master tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (fast-forwardable)

Note the line that says "HEAD branch: master". This is where the remote repository lets clients know which branch to checkout by default.

请注意“HEAD 分支:master”这一行。这是远程存储库让客户知道默认签出哪个分支的地方。

回答by codelogic

There's always a HEAD that pointsto the currently checked out branch on the remote repo (which may or may not be master). Even remote repositories have current branches. Usually it is master, and off the top of my head I can't think of any reason why one would want to change it, but it can be changed.

总是有一个 HEAD指向远程仓库上当前签出的分支(可能是也可能不是主)。即使是远程存储库也有当前的分支。通常它是主人,在我的脑海里,我想不出任何想要改变它的理由,但它是可以改变的。

回答by Dustin

My guess is that someone pushed a branch and called it HEAD:

我的猜测是有人推了一个分支并将其称为 HEAD:

git push origin HEAD