Git:在裸存储库中更改活动分支的正确方法? [英] Git: Correct way to change Active Branch in a bare repository?

查看:35
本文介绍了Git:在裸存储库中更改活动分支的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个裸存储库,用作我的项目的中央存储.所有的开发者都做 git clone 与它分享.当他们执行克隆时,他们会得到主分支的检出(除非他们执行 git clone -n​​),因为 repo.git/HEAD 包含 ref: refs/heads/master,使其成为活动分支.

问题是,如何正确更改活动分支?我可以简单地直接破解 repo.git/HEAD 文件,但这看起来很讨厌,而且很糟糕.

我尝试在 repo .git 目录中执行 git checkout <otherbranch>,但失败了,因为我不在工作树中.

我尝试了 git update-ref HEAD refs/heads/otherbranch 但只是将 refs/heads/master 更新为与 refs/heads/otherbranch 相同(好吧,我在一个虚拟存储库,不是我的生产存储库!)

我尝试了 git update-ref --no-deref HEAD refs/heads/otherbranch 并且几乎奏效了.它更新了 HEAD 文件,但将其设置为 refs/heads/otherbranch 指向的提交的 SHA1.

我正在使用 git 版本 1.7.0.2.msysgit.0 进行测试.

我猜没有办法通过 git push 做到这一点,因为允许 all 和 sundry 更改您的默认分支似乎有点不安全(!),但肯定有更好的方法它在 repo .git 目录中,而不是直接破解 HEAD 文件.

解决方案

如果你可以访问远程裸仓库,这个 文章建议:

git 符号引用 HEAD refs/heads/mybranch

<块引用>

这将更新存储库中的 HEAD 文件,使其包含:

ref: refs/heads/mybranch

git 中所述-symbolic-ref

<小时>

如果您无权访问远程仓库,请参阅 我之前的回答.

<小时>

记住像 git remote set-head:

  • 不会更改远程存储库的默认分支.
    它只更改存储在您的本地 存储库中的远程跟踪分支refs/remotes//HEAD

  • 不会改变 HEAD 本身(同样,只有 refs/remotes//HEAD),因此需要git符号引用.

所以 git remote set-head 不是这里的答案.
git symbols-ref HEAD 是,如果您可以直接访问远程存储库.

I have a bare repository that's used as the central store for my project. All the developers do git clone <repo> to share with it. When they do the clone, they get a checkout of the master branch (unless they do git clone -n) because repo.git/HEAD contains ref: refs/heads/master, making this the Active Branch.

The question is, how do I change the Active Branch properly? I could simply hack the repo.git/HEAD file directly, but that seems nasty and, well, hacky.

I tried doing git checkout <otherbranch> in the repo .git directory, but that failed because I wasn't in a work tree.

I tried git update-ref HEAD refs/heads/otherbranch but that just updated refs/heads/master to be the same as refs/heads/otherbranch (okay, I did that one in a dummy repository, not my production one!)

I tried git update-ref --no-deref HEAD refs/heads/otherbranch and that almost worked. It updated the HEAD file, but it set it to the SHA1 of the commit pointed to by refs/heads/otherbranch.

I'm testing with git version 1.7.0.2.msysgit.0.

I'm guessing there's no way to do this through git push, as allowing all and sundry to change your default branch seems a bit unsafe (!), but surely there's a better way to do it in the repo .git directory than directly hacking the HEAD file.

解决方案

If you have access to the remote bare repo, this article suggests:

git symbolic-ref HEAD refs/heads/mybranch

Which will update the HEAD file in your repository so that it contains:

ref: refs/heads/mybranch

as documented in the git-symbolic-ref


If you don't have access to the remote repo, see my previous answer.


Remember that a command like git remote set-head:

  • doesn't change the default branch of the remote repo.
    It only changes a remote tracking branch stored in your local repo as refs/remotes/<name>/HEAD

  • doesn't change HEAD itself (again, only refs/remotes/<name>/HEAD), hence the need for git symbolic-ref.

So git remote set-head is not the answer here.
git symbolic-ref HEAD is, if you have direct access to the remote repo.

这篇关于Git:在裸存储库中更改活动分支的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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