Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libgit2sharp - Git - cannot push non-fastforwardable reference

I'm using libgit2sharp to push files to a repository once they have been generated.

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);
 } 

This has been working great for some time and pushing to repository without issue, however I am now getting the below error message for some files and the push does not occur for any files.

Error message (from server log):

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)

I have searched for error but cannot find anything that covers this? Also as this repository is sat on a Azure web server I assume I would have run any Git commands via libgit2sharp C# code as I would not have permissions to run command line Git commands.

Any help appreciated on this one.

like image 916
Paolo B Avatar asked Nov 05 '25 10:11

Paolo B


1 Answers

If your changes are not fast-forwardable from the remote's branch, then that means that somebody else has updated the repository's branch. You need to fetch from the remote, merge with the remote's branch, and then you can push.

Alternately, you can force push by prefixing your refspec with a + (eg, +refs/heads/master).

like image 192
Edward Thomson Avatar answered Nov 08 '25 05:11

Edward Thomson