git 如何使用 go get 获取另一个分支而不是默认分支

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

How to get another branch instead of default branch with go get

gitgithubgo

提问by MouseWanted

I have 2 repositories. Let say them repo_a and repo_b. I imported repo_a in repo_b

我有 2 个存储库。假设它们是 repo_a 和 repo_b。我在 repo_b 中导入了 repo_a

When I ran go get, it will get repo_a master branch. Is there any way to get develop branch using go get or another command from repo_b?

当我运行 go get 时,它将获得 repo_a 主分支。有没有办法使用 go get 或 repo_b 中的其他命令来获取开发分支?

I do not want to git pull on each specific package (in this case repo_a)

我不想 git pull 对每个特定的包(在这种情况下是 repo_a)

回答by helmbert

Starting with Go 1.11, this is possible when using Go modules. When installing a dependency for a Go module, you can specify a module querywhich may contain a branch or tag name:

从 Go 1.11 开始,这在使用Go modules时是可能的。在为 Go 模块安装依赖项时,您可以指定一个模块查询,其中可能包含分支或标签名称:

$ go get <path-to-repo>@<branch>

回答by I159

Stable HEAD philosophy

稳定的HEAD理念

It is not possible with pure go get.

go get.

Go takes the most minimal and pragmatic approach of any package manager. There is no such thing as multiple versions of a Go package.

Go 采用了任何包管理器中最简单、最实用的方法。没有 Go 包的多个版本这样的东西。

But this is not as bad as it seems at the first view because there exists a philosophy behind this behavior.

但这并不像乍一看那样糟糕,因为这种行为背后存在着一种哲学。

As a package author, you must adhere to the stable HEAD philosophy. Your default branch must always be the stable, released version of your package. You must do work in feature branches and only merge when ready to release.

作为包作者,您必须坚持稳定的 HEAD 哲学。您的默认分支必须始终是包的稳定、已发布版本。您必须在功能分支中工作,并且只有在准备发布时才合并。

This approach is forced by go getlimitations and it should be treated like Python indentations - it is kind of philosophy forced by language design.

这种方法是受go get限制的,它应该像 Python 缩进一样对待——这是一种语言设计所强迫的哲学。

Development approaches

发展途径

If you want to fork something or try new features you can clone repo then switch to a desired branch and do go build. This way shouldn't go to production.

如果你想 fork 某些东西或尝试新功能,你可以克隆 repo,然后切换到所需的分支并执行go build. 这种方式不应该投入生产。

git clone <repo name>
cd <repo name>
git checkout <branch name>
go build

Also you can use third party package management tools. But most of them support tags and revisions, not branches (since it is implied that you don't need to install feature branch).

您也可以使用第三方包管理工具。但是它们中的大多数都支持标签和修订,而不是分支(因为这意味着您不需要安装功能分支)。

gpm:

通用汽车

You can specify packages with the format, where version can be a revision number (a git/bazaar/mercurial/svn revision hash) or a tag.

您可以使用格式指定包,其中版本可以是修订号(git/bazaar/mercurial/svn 修订哈希)或标签。

回答by zzn

you can use gopkg.in, it will redirect to github.

你可以使用gopkg.in,它会重定向到github。

There are two URL patterns supported:

支持两种 URL 模式:

gopkg.in/pkg.v3      → github.com/go-pkg/pkg (branch/tag v3, v3.N, or v3.N.M)
gopkg.in/user/pkg.v3 → github.com/user/pkg   (branch/tag v3, v3.N, or v3.N.M)

go get gopkg.in/pkg.v3means go get github.com/go-pkg/pkg, but is branch or tag v3.*.

go get gopkg.in/pkg.v3意味着go get github.com/go-pkg/pkg,但是是 branch 或 tag v3.*

for more details, see here

有关更多详细信息,请参阅此处