I need help in check out the code upto a specific SHA number of git repo using golang
Asked
Active
Viewed 2,084 times
1 Answers
0
This is actually a Go question since it is referring to the go-git
library.
As such, you could do something like the following:
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())
}
Note: the majority of this code was taken from the go-git
library examples directory and starts the entire process by cloning the repository into a new local directory; skip this step if not applicable to your use case.

jonroethke
- 1,152
- 2
- 8
- 16