更改 Git 远程 HEAD 以指向除 master 之外的其他内容

发布于 2024-08-06 09:46:30 字数 410 浏览 7 评论 0原文

如何设置 Git 远程的 HEAD 引用以指向“master”之外的其他内容?

我的项目有一个不使用“主”分支的政策(所有分支都应具有有意义的名称)。此外,规范主存储库只能通过 ssh:// 访问,无法通过 shell 访问(如 GitHub 或 Unfuddle)。

我的问题是远程存储库仍然有一个对 refs/heads/master 的 HEAD 引用,但我需要它指向另一个分支。这导致了两个问题:

  1. 克隆存储库时,出现这个,

    <块引用>

    警告:远程 HEAD 引用不存在的引用,无法签出。

    这很混乱且不方便。

  2. 基于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:

  1. When cloning the repo, there this,

    warning: remote HEAD refers to nonexistent ref, unable to checkout.

    That's confusing and inconvenient.

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

若水微香 2024-08-13 09:46:30

一年前,GitHub 上几乎就有同样的问题

这个想法是重命名主分支:

git branch -m master development
git branch -m published master
git push -f origin master 

让master拥有你希望人们使用的东西,并在分支中完成所有其他工作。

(“git-symbolic-ref HEAD refs/head/published”不会传播到远程存储库)

这类似于“如何在 Git 中删除 origin/master"。


正如此帖子中所述: (强调我的)

git clone”仅创建一个本地分支。
为此,它会查看远程存储库的 HEAD ref,并创建一个与其引用的远程分支同名的本地分支。

因此,为了总结这一点,您有存储库 A 并克隆它:

  • HEAD 引用 refs/heads/master 并且存在
    ->你会得到一个名为 master 的本地分支,从 origin/master

    开始

  • HEAD 引用 refs/heads/anotherBranch 并且存在
    ->你会得到一个名为 anotherBranch 的本地分支,从 origin/anotherBranch

    开始

  • HEAD 引用 refs/heads/master 并且不存在
    -> “git clone”抱怨

不确定是否有任何方法可以直接修改存储库中的 HEAD 引用

(我知道,这除了你的问题的重点之外;))


也许唯一的方法是"为穷人提供的出版物",您可以在其中:

 $ git-symbolic-ref HEAD refs/head/published
 $ git-update-server-info
 $ rsync -az .git/* server:/local_path_to/git/myRepo.git/

但这将涉及对服务器的写访问,这并不总是可能的。


正如我在“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 日)

远程:不显示成功消息当 set-head 失败时

签字人:Christian Schlack

在出现错误时禁止显示“origin/HEAD set to master”消息。

$ git Remote set-head origin -a
错误:不是有效的参考:refs/remotes/origin/master
origin/HEAD 设置为 master

There was almost the same question on GitHub a year ago.

The idea was to rename the master branch:

git branch -m master development
git branch -m published master
git push -f origin master 

Making master have what you want people to use, and do all other work in branches.

(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)

"git clone" creates only a single local branch.
To do that, it looks at the HEAD ref of the remote repo, and creates a local branch with the same name as the remote branch referenced by it.

So to wrap that up, you have repo A and clone it:

  • HEAD references refs/heads/master and that exists
    -> you get a local branch called master, starting from origin/master

  • HEAD references refs/heads/anotherBranch and that exists
    -> you get a local branch called anotherBranch, starting from origin/anotherBranch

  • HEAD references refs/heads/master and that doesn't exist
    -> "git clone" complains

Not sure if there's any way to directly modify the HEAD ref in a repo.

(which is all besides the point of your question, I know ;) )


Maybe the only way would be a "publication for the poor", where you:

 $ git-symbolic-ref HEAD refs/head/published
 $ git-update-server-info
 $ rsync -az .git/* server:/local_path_to/git/myRepo.git/

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)

remote: don't show success message when set-head fails

Signed-off-by: Christian Schlack

Suppress the message 'origin/HEAD set to master' in case of an error.

$ git remote set-head origin -a
error: Not a valid ref: refs/remotes/origin/master
origin/HEAD set to master
迷你仙 2024-08-13 09:46:30

更新:这只适用于存储库的本地副本(“客户端”)。请参阅下面其他人的评论。

对于最新版本的 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 branch develop would be:

git remote set-head origin develop

杀手六號 2024-08-13 09:46:30

既然您提到了 GitHub,那么要在他们的网站上执行此操作,只需进入您的项目,然后...

admin >默认分支> (选择某事)

完成。

Since you mention GitHub, to do it on their site simply go into your project, then...

admin > Default Branch > (choose something)

Done.

九命猫 2024-08-13 09:46:30

请参阅:http://www.kernel.org /pub/software/scm/git/docs/git-symbolic-ref.html

这设置 git 存储库中的默认分支。您可以在裸存储库或镜像存储库中运行它。

用法:

$ git symbolic-ref HEAD refs/heads/<branch name>

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 symbolic-ref HEAD refs/heads/<branch name>
友欢 2024-08-13 09:46:30

(已经有基本相同的问题“在远程创建一个 git 符号引用存储库”,没有收到通用答案。)

但是对于各种 git“农场”(多个用户可以通过受限接口管理 git 存储库:通过 http 和 ssh)有特定的答案: http://Github.com, http:// /Gitorious.orghttp://repo.or.czGirar (http ://git.altlinux.org)。

这些具体答案可能对那些阅读本页并考虑这些特定服务的人有用。

(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.

恋竹姑娘 2024-08-13 09:46:30

如果您可以从 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'.

迷爱 2024-08-13 09:46:30

与这个问题相关,我在搜索时最终来到这里:

如何使本地存储库了解 GitHub 上更改的默认分支

为了完整起见,添加答案:

git remote set-head origin -a

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 remote set-head origin -a
瞄了个咪的 2024-08-13 09:46:30

您可以仅使用瓷器 Git 命令创建一个分离的 ma​​ster 分支:

git init
touch GO_AWAY
git add GO_AWAY
git commit -m "GO AWAY - this branch is detached from reality"

这为我们提供了一个带有粗鲁消息的 ma​​ster 分支(您可能想要更有礼貌)。现在我们创建“真正的”分支(为了纪念 SVN,我们将其称为主干)并将其与ma​​ster分离:

git checkout -b trunk
git rm GO_AWAY
git commit --amend --allow-empty -m "initial commit on detached trunk"

嘿,快点! gitk --all 将显示 ma​​stertrunk,它们之间没有链接。

这里的“魔法”是 --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:

git init
touch GO_AWAY
git add GO_AWAY
git commit -m "GO AWAY - this branch is detached from reality"

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:

git checkout -b trunk
git rm GO_AWAY
git commit --amend --allow-empty -m "initial commit on detached trunk"

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.

友欢 2024-08-13 09:46:30

首先,创建您想要设置为默认的新分支,例如:

$>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

送君千里 2024-08-13 09:46:30

对于 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.

江南月 2024-08-13 09:46:30

更改 git Remote HEAD 的最佳且直接的方法是运行

git remote set-head origin DEV

origin 通常是远程名称
DEV 远程分支名称

The best and straight-forward way to change git remote HEAD is to run

git remote set-head origin DEV

origin is usually the remote name
DEV the remote branch name

淡淡離愁欲言轉身 2024-08-13 09:46:30

很简单,只需登录您的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文