3

I have a NodeGit repository object, repo, and a string sha code which represents a commit id, commit_id. How can I perform a git checkout of this commit using NodeGit, which would act the same as the command git checkout $commit_id?

So far, I have tried creating a Reference using the commit id and then using checkoutRef to check out the reference. However, the repository does not change and I get the error Error: the given reference name '<my commit id>' is not valid.

NodeGit.Reference.lookup(repo, commit_id, (reference) => {
    repo.checkoutRef(reference)
});

Thanks for the help.

Sam
  • 3,070
  • 3
  • 20
  • 26
  • What have you tried so far? The [nodegit documentation](https://www.nodegit.org/api/) seems to be a good source of information. – larsks Apr 29 '20 at 12:51
  • 1
    Hi @larsks - I have added my attempt to the question. The documentation does not provide any examples of checking out a commit ID, only the latest commit of a named branch. Thanks! – Sam Apr 29 '20 at 13:20
  • Agree, very confusing – Daniel Stephens Aug 11 '20 at 20:07

1 Answers1

0

The only solution I've found so far is:

const commit = await repo.getCommit(commit_id);
await nodegit.Reset.reset(repo, commit, nodegit.Reset.TYPE.HARD);

But it's so slow... Especially for big repositories!