什么决定了“git clone”之后的默认分支?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18726037/
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
What determines default branch after "git clone"?
提问by Christian Goetze
My understanding is that the default branch of a cloned repository is to be whatever HEAD points to in the repo being cloned.
我的理解是克隆存储库的默认分支是 HEAD 在被克隆的存储库中指向的任何分支。
I now have a case where this is not true. My understanding is obviously flawed, so what does determine the default checkout branch when cloning a (bare) repo?
我现在有一个案例,这不是真的。我的理解显然是有缺陷的,那么在克隆(裸)repo 时,是什么决定了默认的 checkout 分支?
The last commit on that repo was a merge between the branch referenced in the bare repo's HEAD into the branch I'm getting as the checkout branch in the clone.
该存储库的最后一次提交是将裸存储库的 HEAD 中引用的分支合并到我作为克隆中的结帐分支获得的分支中。
Running git remote show origin
returns:
运行git remote show origin
返回:
Fetch URL: ...
Push URL: ...
HEAD branch (remote HEAD is ambiguous, may be one of the following):
<bad-branch>
live
Remote branches:
...
Bare repo uses Git version 1.8.2.1, client uses 1.7.12.4, transport is SSH.
Bare repo 使用 Git 版本 1.8.2.1,客户端使用 1.7.12.4,传输是 SSH。
Maybe the answer is actually this one here. This answerconfirms it. If there is a choice of symbolic refs all pointing to the same revision as HEAD, the client will guesswhich branch to use.
也许答案实际上就是这里。这个答案证实了这一点。如果可以选择所有符号引用都指向与 HEAD 相同的修订版,则客户端将猜测要使用哪个分支。
采纳答案by Edward Thomson
Beginning with Git 1.8.5, the server will send the actual branch name that HEAD
points to, in the "symref" capability. If you have both a client and server newer than Git 1.8.5, it will update HEAD
correctly.
从Git 1.8.5开始,服务器将HEAD
在“symref”功能中发送指向的实际分支名称。如果你的客户端和服务器都比 Git 1.8.5 更新,它会HEAD
正确更新。
Prior to that, the client will guess what HEAD may have pointed to by comparing the object ID that HEAD (ultimately) points to with all the object IDs of all the branches. It prefers a branch named refs/heads/master
: if both HEAD
and master
point to the same object ID, then clone will set the default branch in the new repository to master
.
在此之前,客户端将通过将 HEAD(最终)指向的对象 ID 与所有分支的所有对象 ID 进行比较来猜测 HEAD 可能指向什么。它喜欢一个名为分支refs/heads/master
:如果两个HEAD
并master
指向同一个对象ID,然后克隆将设置默认分支在新的存储库master
。
Otherwise, the first branch with a matching OID (when the branches are sorted alphanumerically) will be the default branch. If no branches have matching OIDs, then HEAD
will be set directly to the object ID (ie, a detached HEAD).
否则,具有匹配 OID 的第一个分支(当分支按字母数字排序时)将是默认分支。如果没有分支具有匹配的 OID,则将HEAD
直接设置为对象 ID(即分离的 HEAD)。
回答by Valentin Lorentz
It is actually what HEAD points to. Use git symbolic-ref HEAD refs/heads/mybranch
for setting HEAD.
(source: http://feeding.cloud.geek.nz/posts/setting-default-git-branch-in-bare/)
这实际上是 HEAD 所指向的。使用git symbolic-ref HEAD refs/heads/mybranch
设置HEAD。(来源:http: //feeding.cloud.geek.nz/posts/setting-default-git-branch-in-bare/)
回答by Carl Norum
A bare repo has a HEAD
, too. That's what you get when you clone it.
一个裸仓库也有一个HEAD
。这就是你克隆它时得到的。
From the git clone
documentation:
Clones a repository into a newly created directory, creates remote-tracking branches for each branch in the cloned repository (visible using
git branch -r
), and creates and checks out an initial branch that is forked from the cloned repository's currently active branch.
将存储库克隆到新创建的目录中,为克隆存储库中的每个分支创建远程跟踪分支(使用 可见
git branch -r
),并创建并签出从克隆存储库当前活动分支分叉的初始分支。
The bit about "currently active branch" is referring to the remote's HEAD
revision.
关于“当前活动分支”的部分指的是遥控器的HEAD
修订版。
If you want different behaviour, you can use --branch
or -b
:
如果你想要不同的行为,你可以使用--branch
或-b
:
--branch <name>
-b <name>
Instead of pointing the newly createdHEAD
to the branch pointed to by the cloned repository'sHEAD
, point to<name>
branch instead. In a non-bare repository, this is the branch that will be checked out.--branch
can also take tags and detaches theHEAD
at that commit in the resulting repository.
--branch <name>
-b <name>
不是将新创建的指向HEAD
克隆存储库的HEAD
指向的<name>
分支,而是指向分支。在非裸存储库中,这是将被检出的分支。--branch
还可以获取标签并HEAD
在结果存储库中分离该提交。
回答by Archimedes Trajano
It's origin/HEAD
. However if you want the name like 'origin/master' you'd have to parse the file like this
它是origin/HEAD
。但是,如果您想要像“origin/master”这样的名称,则必须像这样解析文件
cat .git/refs/remotes/origin/HEAD | awk '{ print }'