LibGit2Sharp CheckoutPaths() 在不指定分支的情况下还原文件

LibGit2Sharp CheckoutPaths() to Revert a File without specifying a Branch

我想做等同于 git checkout -- file 的事情来丢弃尚未准备提交的文件中的更改。

我看到了关于 how to checkout from a specific commit 的问题,但是我不想指定任何特定的提交或分支。

在下面的代码中,commitishOrBranchSpec 的参数似乎是必需的,但不能为 null 或空字符串;有没有可以在这里指定的值表示default,类似于上面git命令行中没有指定任何分支或提交?

using (var repo = new Repository(workingDir))
{
    // $TODO: Figure out what to pass for parameter 1,
    // commitishOrBranchSpec (null and "" don't work)
    repo.CheckoutPaths("", Enumerable.Repeat(filePath, 1));
}

I have seen the question around how to checkout from a specific commit, however I don't want to specify any specific commit or branch.

您必须指定要结帐的内容。如果你想模拟 git checkout -- file,那么你可以简单地使用 HEAD 分支。例如:

repo.checkoutPaths(repo.Head, new string[] { filePath }, checkoutOptions);