更改 Git 远程 HEAD 以指向除 master 之外的其他内容
如何设置 Git 远程的 HEAD 引用以指向“master”之外的其他内容?
我的项目有一个不使用“主”分支的政策(所有分支都应具有有意义的名称)。此外,规范主存储库只能通过 ssh:// 访问,无法通过 shell 访问(如 GitHub 或 Unfuddle)。
我的问题是远程存储库仍然有一个对 refs/heads/master 的 HEAD 引用,但我需要它指向另一个分支。这导致了两个问题:
克隆存储库时,出现这个,
<块引用>警告:远程 HEAD 引用不存在的引用,无法签出。
这很混乱且不方便。
基于Web的代码浏览器依赖于HEAD作为浏览树的基础。那么我需要 HEAD 指向一个有效的分支。
How do I set a Git remote's HEAD reference to point to something besides "master"?
My project has a policy not to use a "master" branch (all branches are to have meaningful names). Furthermore, the canonical master repository is only accessible via ssh://, with no shell access (like GitHub or Unfuddle).
My problem is that the remote repository still has a HEAD reference to refs/heads/master, but I need it to point to a different branch. This is causing two problems:
When cloning the repo, there this,
warning: remote HEAD refers to nonexistent ref, unable to checkout.
That's confusing and inconvenient.
The web-based code browser depends on HEAD as a basis for browsing the tree. I need HEAD to point to a valid branch, then.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
一年前,GitHub 上几乎就有同样的问题。
这个想法是重命名主分支:
(“
git-symbolic-ref HEAD refs/head/published
”不会传播到远程存储库)这类似于“如何在 Git 中删除 origin/master"。
正如此帖子中所述: (强调我的)
(我知道,这除了你的问题的重点之外;))
也许唯一的方法是"为穷人提供的出版物",您可以在其中:
但这将涉及对服务器的写访问,这并不总是可能的。
正如我在“Git:在裸存储库中更改 Active Branch 的正确方法?”中解释的那样,
git Remote set-head
不会更改远程存储库上的任何内容。它只会更改本地存储库中本地存储的远程跟踪分支,位于
remotes//HEAD
中。使用 Git 2.29(2020 年第 4 季度),“
git 远程设置头
(man)" 仍然说了一些暗示操作已完成的内容,这是误导性的。请参阅 提交 5a07c6c(2020 年 9 月 17 日),作者:Christian Schlack (
cschlack
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 39149df,2020 年 9 月 22 日)There was almost the same question on GitHub a year ago.
The idea was to rename the master branch:
(a "
git-symbolic-ref HEAD refs/head/published
" would not be propagated to the remote repo)This is similar to "How do I delete origin/master in Git".
As said in this thread: (emphasis mine)
(which is all besides the point of your question, I know ;) )
Maybe the only way would be a "publication for the poor", where you:
But that would involve write access to the server, which is not always possible.
As I explain in "Git: Correct way to change Active Branch in a bare repository?",
git remote set-head
wouldn't change anything on the remote repo.It would only change the remote tracking branch stored locally in your local repo, in
remotes/<name>/HEAD
.With Git 2.29 (Q4 2020), "
git remote set-head
(man)" that failed still said something that hints the operation went through, which was misleading.See commit 5a07c6c (17 Sep 2020) by Christian Schlack (
cschlack
).(Merged by Junio C Hamano --
gitster
-- in commit 39149df, 22 Sep 2020)更新:这只适用于存储库的本地副本(“客户端”)。请参阅下面其他人的评论。
对于最新版本的 git(2014 年 2 月),正确的过程是:
git remote set-head $REMOTE_NAME $BRANCH
例如,将远程
origin
上的头切换到分支develop
将是:git remote set-head origindevelop
Update: This only works for the local copy of the repository (the "client"). Please see others' comments below.
With a recent version of git (Feb 2014), the correct procedure would be:
git remote set-head $REMOTE_NAME $BRANCH
So for example, switching the head on remote
origin
to branchdevelop
would be:git remote set-head origin develop
既然您提到了 GitHub,那么要在他们的网站上执行此操作,只需进入您的项目,然后...
admin >默认分支> (选择某事)
完成。
Since you mention GitHub, to do it on their site simply go into your project, then...
admin > Default Branch > (choose something)
Done.
请参阅:http://www.kernel.org /pub/software/scm/git/docs/git-symbolic-ref.html
这设置 git 存储库中的默认分支。您可以在裸存储库或镜像存储库中运行它。
用法:
See: http://www.kernel.org/pub/software/scm/git/docs/git-symbolic-ref.html
This sets the default branch in the git repository. You can run this in bare or mirrored repositories.
Usage:
(已经有基本相同的问题“在远程创建一个 git 符号引用存储库”,没有收到通用答案。)
但是对于各种 git“农场”(多个用户可以通过受限接口管理 git 存储库:通过 http 和 ssh)有特定的答案: http://Github.com, http:// /Gitorious.org,http://repo.or.cz,Girar (http ://git.altlinux.org)。
这些具体答案可能对那些阅读本页并考虑这些特定服务的人有用。
<代码>
$ ssh git.alt 帮助 | fgrep 分支
default-branch
$
例如
ssh git.alt default-branch packages/autosshd.git sisyphus
将远程存储库autosshd.git
中的 HEAD 更改为指向sisyphus
代码 > 分支。(There was already basically the same question "create a git symbolic ref in remote repository", which received no universal answer.)
But there are a specific answers for various git "farms" (where multiple users can manage git repos through a restricted interface: via http and ssh): http://Github.com, http://Gitorious.org, http://repo.or.cz, Girar (http://git.altlinux.org).
These specific answers might be useful for those reading this page and thinking about these specific services.
$ ssh git.alt help | fgrep branch
default-branch <path to git repository> [<branch>]
$
for example
ssh git.alt default-branch packages/autosshd.git sisyphus
to change the HEAD in the remote repoautosshd.git
to point to thesisyphus
branch.如果您可以从 shell 访问远程存储库,只需进入 .git(如果是裸存储库,则进入主目录)并更改 HEAD 文件以指向正确的头。例如,默认情况下它始终包含“refs: refs/heads/master”,但如果您需要 foo 作为 HEAD,只需编辑 HEAD 文件并将内容更改为“refs: refs/heads/foo”。
If you have access to the remote repo from a shell, just go into the .git (or the main dir if its a bare repo) and change the HEAD file to point to the correct head. For example, by default it always contains 'refs: refs/heads/master', but if you need foo to be the HEAD instead, just edit the HEAD file and change the contents to 'refs: refs/heads/foo'.
与这个问题相关,我在搜索时最终来到这里:
如何使本地存储库了解 GitHub 上更改的默认分支
为了完整起见,添加答案:
Related to the question, I ended up here when searching for:
How do I make a local repo aware of a changed default branch on GitHub
For completeness, adding the answer:
您可以仅使用瓷器 Git 命令创建一个分离的 master 分支:
这为我们提供了一个带有粗鲁消息的 master 分支(您可能想要更有礼貌)。现在我们创建“真正的”分支(为了纪念 SVN,我们将其称为主干)并将其与master分离:
嘿,快点! gitk --all 将显示 master 和 trunk,它们之间没有链接。
这里的“魔法”是 --amend 导致 git commit 创建一个与当前 HEAD 具有相同父级的新提交,然后使 HEAD 指向它。但是当前的 HEAD 没有父级,因为它是存储库中的初始提交,因此新的 HEAD 也没有父级,从而使它们彼此分离。
旧的 HEAD 提交不会被 git-gc 删除,因为 refs/heads/master 仍然指向它。
--allow-empty 标志仅在我们提交空树时才需要。如果在git rm之后有一些git add,那么就没有必要了。
事实上,您可以随时创建分离分支,方法是对存储库中的初始提交进行分支,删除其树,添加分离树,然后执行git commit --amend。
我知道这并不能回答如何修改远程存储库上的默认分支的问题,但它给出了如何创建分离分支的清晰答案。
You can create a detached master branch using only porcelain Git commands:
That gives us a master branch with a rude message (you may want to be more polite). Now we create our "real" branch (let's call it trunk in honour of SVN) and divorce it from master:
Hey, presto! gitk --all will show master and trunk with no link between them.
The "magic" here is that --amend causes git commit to create a new commit with the same parent as the current HEAD, then make HEAD point to it. But the current HEAD doesn't have a parent as it's the initial commit in the repository, so the new HEAD doesn't get one either, making them detached from each other.
The old HEAD commit doesn't get deleted by git-gc because refs/heads/master still points to it.
The --allow-empty flag is only needed because we're committing an empty tree. If there were some git add's after the git rm then it wouldn't be necessary.
In truth, you can create a detached branch at any time by branching the initial commit in the repository, deleting its tree, adding your detached tree, then doing git commit --amend.
I know this doesn't answer the question of how to modify the default branch on the remote repository, but it gives a clean answer on how to create a detached branch.
首先,创建您想要设置为默认的新分支,例如:
$>gitbranch main
接下来,将该分支推送到 origin:
$>git push origin main
现在,当您登录 GitHub 帐户时,您可以转到存储库并选择“设置”>“默认分支”,然后选择“main”。
然后,如果您愿意,您可以删除 master 分支:
$>git push origin :master
First, create the new branch you would like to set as your default, for example:
$>git branch main
Next, push that branch to the origin:
$>git push origin main
Now when you login to your GitHub account, you can go to your repository and choose Settings>Default Branch and choose "main."
Then, if you so choose, you can delete the master branch:
$>git push origin :master
对于 gitolite 人来说,gitolite 支持一个名为 --wait it --
symbolic-ref
的命令。如果您对存储库具有 W(写入)权限,它允许您远程运行该命令。For gitolite people, gitolite supports a command called -- wait for it --
symbolic-ref
. It allows you to run that command remotely if you have W (write) permission to the repo.更改 git Remote HEAD 的最佳且直接的方法是运行
origin 通常是远程名称
DEV 远程分支名称
The best and straight-forward way to change git remote HEAD is to run
origin is usually the remote name
DEV the remote branch name
很简单,只需登录您的 GitHub 帐户,然后在导航菜单的最右侧选择设置,在设置选项卡中选择默认分支并返回回到你的存储库的主页,它对我有用。
Simple just log into your GitHub account and on the far right side in the navigation menu choose Settings, in the Settings Tab choose Default Branch and return back to main page of your repository that did the trick for me.