libgit2sharp - Git - 无法推送不可快进的引用

4

我正在使用 libgit2sharp 将生成的文件推送到代码库。

using (var repo = new Repository(RepositoryPath))
{
    // Stage the file
    Commands.Stage(repo, "*");

    // Create the committer's signature and commit
    Signature author = new Signature("translator", "example.com", DateTime.Now);
    Signature committer = author;

    // Commit to the repository
    Commit commit = repo.Commit($"Resx files updated {DateTime.Now}", author, committer);

    Remote remote = repo.Network.Remotes["origin"];
    var options = new PushOptions
    {
        CredentialsProvider = (_url, _user, _cred) =>
           new UsernamePasswordCredentials
           {
               Username = _settings.GitUserName,
               Password = _settings.GitPassword
           }
     };
     repo.Network.Push(remote, @"refs/heads/master", options);
 } 

这段时间一直很顺利,可以将文件推送到仓库中,但是现在有些文件出现以下错误信息,无法将任何文件推送:

错误信息(来自服务器日志):

Message: cannot push non-fastforwardable reference
StackTrace:  at LibGit2Sharp.Core.Ensure.HandleError(Int32 result)
   at LibGit2Sharp.Core.Proxy.git_remote_push(RemoteHandle remote, IEnumerable`1 refSpecs, GitPushOptions opts)
   at LibGit2Sharp.Network.Push(Remote remote, IEnumerable`1 pushRefSpecs, PushOptions pushOptions)
   at LibGit2Sharp.Network.Push(Remote remote, String pushRefSpec, PushOptions pushOptions)
   at Westwind.Globalization.Core.Utilities.DbResXConverter.SyncToGit(String resourceSet)

我已经搜索了错误信息,但是没有找到任何相关内容。同时,由于这个仓库位于Azure Web服务器上,我认为我必须通过libgit2sharp C#代码运行任何Git命令,因为我没有权限运行命令行Git命令。

非常感谢您的帮助。

1个回答

10
如果您的更改不能从远程分支进行快进,那么就意味着有人更新了该存储库的分支。您需要从远程获取,与远程分支合并,然后才能推送。另外,您可以通过在 refspec 前加上 +(例如 +refs/heads/master)来强制推送。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接