如何使用golang签出git回购中的特定SHA
我需要帮助使用golang将代码签出到git回购的特定SHA编号这实际上是一个Go问题,因为它指的是Go git 因此,您可以执行以下操作:如何使用golang签出git回购中的特定SHA,go,go-git,Go,Go Git,我需要帮助使用golang将代码签出到git回购的特定SHA编号这实际上是一个Go问题,因为它指的是Go git 因此,您可以执行以下操作: package main import ( "fmt" git "gopkg.in/src-d/go-git.v4" "gopkg.in/src-d/go-git.v4/plumbing" ) // Basic example of how to checkout a specific commit. func main(
package main
import (
"fmt"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
)
// Basic example of how to checkout a specific commit.
func main() {
// Clone the given repository to the given directory
r, err := git.PlainClone("your-local-repo-name", false, &git.CloneOptions{
URL: "your-repo-clone-URL",
})
// handle error
// ... retrieving the commit being pointed by HEAD
ref, err := r.Head()
// handle error
w, err := r.Worktree()
// handle error
// ... checking out to commit
err = w.Checkout(&git.CheckoutOptions{
Hash: plumbing.NewHash("your-specific-commit-hash"),
})
// handle error
// ... retrieving the commit being pointed by HEAD, it shows that the
// repository is pointing to the giving commit in detached mode
ref, err = r.Head()
// handle error
fmt.Println(ref.Hash())
}
注意:此代码的大部分来自GoGit库目录,并通过将存储库克隆到新的本地目录开始整个过程;如果不适用于您的用例,请跳过此步骤。这与go lang有什么关系?