更改 Git 远程 HEAD 以指向除 master 之外的其他内容 [英] Change a Git remote HEAD to point to something besides master

查看:71
本文介绍了更改 Git 远程 HEAD 以指向除 master 之外的其他内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置 Git 遥控器的 HEAD 引用以指向除master"之外的其他内容?

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

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

  1. 在克隆 repo 时,有这个,

    <块引用>

    警告:远程 HEAD 引用不存在的引用,无法结帐.

    这既令人困惑又不方便.

  2. 基于网络的代码浏览器依赖于 HEAD 作为浏览树的基础.那么我需要 HEAD 指向一个有效的分支.

解决方案

几乎有一年前在 GitHub 上提出了同样的问题.

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

git branch -m master 开发git branch -m 发布 mastergit push -f 源主

<块引用>

让master拥有你想让人们使用的东西,并在分支机构中完成所有其他工作.

(git-symbolic-ref HEAD refs/head/published"不会传播到远程仓库)

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


本主题所述:(强调我的)

<块引用>

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

所以总结一下,你有 repo A 并克隆它:

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

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

    开始
  • HEAD 引用了 refs/heads/master 并且不存在
    ->git clone";抱怨

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

(这是你问题的重点,我知道;))


也许唯一的方法是穷人的出版物",您:

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

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


正如我在Git:在裸存储库中更改活动分支的正确方法?"中所述,git remote set-head 不会改变任何远程仓库.

它只会更改本地存储在本地存储库中的远程跟踪分支,位于 remotes//HEAD.


使用 Git 2.29(2020 年第四季度),git remote set-head(man)"那个失败仍然说暗示操作已经完成,这是误导.

请参阅 commit 5a07c6c(2020 年 9 月 17 日),作者 Christian Schlack (cschlack).
(由 Junio C Hamano 合并 -- gitster --commit 39149df,2020 年 9 月 22 日)

<块引用>

remote:不显示成功消息set-head 失败时

签字人:Christian Schlack

<块引用>

在出现错误时抑制消息origin/HEAD set to master".

$ git remote set-head origin -a错误:不是有效的 ref:refs/remotes/origin/master原点/头部设置为主

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.

解决方案

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 the all 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

这篇关于更改 Git 远程 HEAD 以指向除 master 之外的其他内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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