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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1485578/
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 07:07:28  来源:igfitidea点击:

Change a Git remote HEAD to point to something besides master

gitgit-branchgit-remote

提问by JasonSmith

How do I set a Git remote's HEAD reference to point to something besides "master"?

如何设置 Git 遥控器的 HEAD 引用以指向除“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).

我的项目有一个政策,不使用“主”分支(所有分支都必须有有意义的名称)。此外,规范的主存储库只能通过 ssh:// 访问,不能访问 shell(如 GitHub 或 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:

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

  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.

  1. 克隆回购时,有这个,

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

    这令人困惑和不便。

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

采纳答案by VonC

There was almost the same question on GitHuba year ago.

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

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.

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

(a "git-symbolic-ref HEAD refs/head/published" would not be propagated to the remote repo)

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

This is similar to "How do I delete origin/master in Git".

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



As said in this thread: (emphasis mine)

正如在这个线程中所说:(强调我的)

"git clone" creates only a single local branch.
To do that, it looks at the HEAD refof 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:

  • HEADreferences refs/heads/masterand that exists
    -> you get a local branch called master, starting from origin/master

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

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

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

" 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” 抱怨

不确定是否有任何方法可以直接修改HEADrepo 中的ref

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

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



Maybethe 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-headwouldn't change anything on the remote repo.

正如我在“ Git:在裸存储库中更改活动分支的正确方法?”中解释的那样,git remote set-head不会更改远程存储库上的任何内容。

It would only change the remote tracking branch stored locally in your local repo, in remotes/<name>/HEAD.

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

回答by jrhorn424

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(2014 年 2 月),正确的程序是:

git remote set-head $REMOTE_NAME $BRANCH

git remote set-head $REMOTE_NAME $BRANCH

So for example, switching the head on remote originto branch developwould be:

因此,例如,将远程头切换origin到分支develop将是:

git remote set-head origin develop

git remote set-head origin develop

回答by srcspider

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

既然你提到了 GitHub,要在他们的网站上做,只需进入你的项目,然后......

admin > Default Branch > (choose something)

admin > Default Branch > (choose something)

Done.

完毕。

回答by mani-fresh

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

请参阅: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.

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

Usage:

用法:

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

回答by imz -- Ivan Zakharyaschev

(There was already basically the same question "create a git symbolic ref in remote repository", which received no universal answer.)

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

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

但是对于各种 git “farms”(多个用户可以通过受限接口管理 git repos:通过 http 和 ssh)有一个特定的答案:http: //Github.comhttp://Gitorious.orghttp:// /repo.or.czGirarhttp://git.altlinux.org)。

These specific answers might be useful for those reading this page and thinking about these specific services.

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

回答by squeegee

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

如果您可以从 shell 访问远程存储库,只需进入 .git(或主目录,如果它是一个裸存储库)并更改 HEAD 文件以指向正确的头部。例如,默认情况下它总是包含 'refs: refs/heads/master',但如果你需要 foo 作为 HEAD,只需编辑 HEAD 文件并将内容更改为 'refs: refs/heads/foo'。

回答by kbro

You can create a detached masterbranch using only porcelain Git commands:

你可以只使用瓷器 Git 命令创建一个分离的分支:

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

That gives us a masterbranch with a rude message (you may want to be more polite). Now we create our "real" branch (let's call it trunkin honour of SVN) and divorce it from master:

这给了我们一个带有粗鲁信息的分支(你可能想要更有礼貌)。现在我们创建我们的“真正的”分支(为了纪念 SVN,我们称之为主干)并将它与master 分离

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

Hey, presto! gitk --allwill show masterand trunkwith no link between them.

嘿,快点! gitk --all将显示mastertrunk之间没有链接。

The "magic" here is that --amendcauses git committo 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.

这里的“魔法”是--amend导致git commit创建一个与当前 HEAD 具有相同父级的新提交,然后使 HEAD 指向它。但是当前的 HEAD 没有父级,因为它是存储库中的初始提交,所以新的 HEAD 也没有,使它们彼此分离。

The old HEAD commit doesn't get deleted by git-gcbecause refs/heads/master still points to it.

旧的 HEAD 提交不会被git-gc删除,因为 refs/heads/master 仍然指向它。

The --allow-emptyflag is only needed because we're committing an empty tree. If there were some git add's after the git rmthen it wouldn't be necessary.

--allow空时,才需要标志,因为我们正在犯一个空的树。如果在git rm之后有一些git add ,那么就没有必要了。

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.

实际上,您可以通过在存储库中对初始提交进行分支、删除其树、添加分离的树,然后执行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.

我知道这并没有回答如何修改远程存储库上的默认分支的问题,但它给出了如何创建分离分支的明确答案。

回答by 4mnes7y

First, create the new branch you would like to set as your default, for example:

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

$>git branch main

$>git branch main

Next, push that branch to the origin:

接下来,将该分支推送到原点

$>git push origin main

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

现在,当您登录到您的 GitHub 帐户时,您可以转到您的存储库并选择 Settings>Default Branch 并选择“ main ”

Then, if you so choose, you can delete the master branch:

然后,如果您愿意,您可以删除 master 分支:

$>git push origin :master

$>git push origin :master

回答by friederbluemle

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

如何让本地存储库知道 GitHub 上已更改的默认分支

For completeness, adding the answer:

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

git remote set-head origin -a

回答by sitaram

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.

对于 gitolite 人来说,gitolite 支持一个名为 --wait for it -- 的命令symbolic-ref。如果您对 repo 具有 W(写)权限,它允许您远程运行该命令。