【问题标题】:How to checkout a specific SHA in a git repo using golang如何使用 golang 在 git repo 中签出特定的 SHA
【发布时间】:2019-06-28 16:57:36
【问题描述】:

我需要帮助以使用 golang 将代码检查到特定 SHA 编号的 git repo

【问题讨论】:

  • go-lang有什么关系?

标签: go go-git


【解决方案1】:

这实际上是一个围棋问题,因为它指的是go-gitlibrary

因此,您可以执行以下操作:

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())
}

注意: 大部分代码取自go-gitexamples目录,并通过将存储库克隆到新的本地目录来启动整个过程;如果不适用于您的用例,请跳过此步骤。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多