gitgogo-git

go-git: Is it possible to perform a hard reset to origin branch?


I'm trying to achieve the result of the following git commands programmatically using go-git:

git fetch
git reset --hard origin/<some_branch>

So there's git.Worktree.Reset(), which gets git.ResetOptions, which has a Commit property which is of type plumbing.Hash. Unfortunately, I can't figure out how to get the hash for origin/<some_branch>.

How can I do that?


Solution

  • When using Git from the command line you'd need to use rev-parse (see How to find the hash of branch in Git?).

    It seems to be implemented partially in go-git:

    There exists a method (*Repository).ResolveRevision which satisfies part of the behavior of rev-parse

    Source: https://github.com/src-d/go-git/issues/599