Git:在裸存储库中更改活动分支的正确方法?

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

Git: Correct way to change Active Branch in a bare repository?

git

提问by kbro

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/HEADcontains ref: refs/heads/master, making this the Active Branch.

我有一个裸存储库,用作我的项目的中央存储。所有的开发者都git clone <repo>愿意与它分享。当他们进行克隆时,他们会得到 master 分支的结帐(除非他们这样做git clone -n),因为repo.git/HEADcontains ref: refs/heads/master,使其成为Active Branch

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

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

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

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

I tried git update-ref HEAD refs/heads/otherbranchbut 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!)

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

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

我试过了git update-ref --no-deref HEAD refs/heads/otherbranch,几乎奏效了。它更新了HEAD文件,但将其设置为refs/heads/otherbranch.

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

我正在测试 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 .gitdirectory than directly hacking the HEADfile.

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

回答by VonC

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:

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

ref: refs/heads/mybranch

as documented in the git-symbolic-ref

如记录在 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:

请记住,像这样的命令git remote set-head

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

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

  • 不会更改远程仓库的默认分支。
    它只会将存储在本地存储库中的远程跟踪分支更改为refs/remotes/<name>/HEAD

  • 不会改变HEAD自己(同样,只有refs/remotes/<name>/HEAD),因此需要 git symbolic-ref.

So git remote set-headis notthe answer here.
git symbolic-ref HEADis, if you have direct access to the remote repo.

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

回答by Saul Rosales

To change the branch you need to change HEAD reference to the branch you want to use.

要更改分支,您需要将 HEAD 引用更改为要使用的分支。

First list all the references in the bare repository by doing

首先通过执行列出裸存储库中的所有引用

$find ref

Then find the reference for your branch, the format will be as follows refs/heads/<my_branch>. So next step is to check current reference, just type:

然后找到你的分支的参考,格式如下refs/heads/<my_branch>。所以下一步是检查当前参考,只需键入:

$git symbolic-ref HEAD

so you know which is the current branch then update it as needed.

所以你知道哪个是当前分支然后根据需要更新它。

$git sumbolic-ref HEAD ref/heads/<my_branch>

Thant's it. Enjoy.

就是这样。享受。

回答by c-tools

How to change the Active Branch properly ?

如何正确更改活动分支?

  • status: git checkout in the repo .git directory returns fatal: This operation must be run in a work tree

  • tips: just add the --work-tree argument

  • 状态:repo .git 目录中的 git checkout 返回致命:此操作必须在工作树中运行

  • 提示:只需添加 --work-tree 参数

detailed example : assumptions: bare git on remote server:

详细示例:假设:远程服务器上的裸 git:

~/bare_git_repository.git detached work tree: /var/www/myappremote

~/bare_git_repository.git 分离的工作树:/var/www/myappremote

on local server:create branch version.1.7 (our otherbranch)

在本地服务器上:创建分支 version.1.7(我们的其他分支)

git branch version.1.7

git 分支 version.1.7

git push origin version.1.7

git push origin version.1.7

on the remote server with git bare repo:

在带有 git 裸仓库的远程服务器上:

$ cd ~/bare_git_repository.git

$ cd ~/bare_git_repository.git

$ git branch

$ git 分支

  • master
    version.1.7

  • 版本.1.7

As stated,following command

如上所述,遵循命令

git checkout version.1.7

git checkout version.1.7

return

返回

fatal: This operation must be run in a work tree

致命:此操作必须在工作树中运行

Using the following command

使用以下命令

git --work-tree=/var/www/myappremote checkout version.1.7

git --work-tree=/var/www/myappremote checkout version.1.7

successfully change the Active Branch propely

成功更改 Active Branch prop

$ git branch

$ git 分支

master

掌握

  • version.1.7
  • 版本.1.7

check the results with the following

使用以下检查结果

ll /var/www/myappremote

ll /var/www/myappremote

hope it will help

希望它会有所帮助

回答by dvdvck

Also, if you don't have access to the bare repository, by doing a git remote set-headand you are done

此外,如果您无权访问裸存储库,则执行 agit remote set-head就完成了

See this previous response

看这个之前的回复

回答by mcjh

I also have a bare repo on our server and was able to successfully retrieve files using

我在我们的服务器上也有一个裸仓库,并且能够使用成功检索文件

git clone //server/repo/directory -b branch_name

into a new local repository even though the manpage says this is only for non-bare repositories.

进入一个新的本地存储库,即使联机帮助页说这仅适用于非裸存储库。

回答by boryn

I compared two directories before and after applying

我在申请前后对比了两个目录

git symbolic-ref HEAD refs/heads/mybranch

git symbolic-ref HEAD refs/heads/mybranch

and it appears that only repo.git/HEAD file was changed so probably it is quite safe just to "hack" the file.

并且似乎只更改了 repo.git/HEAD 文件,因此“破解”该文件可能是非常安全的。