如何提交和推送libgit2sharp

use*_*196 6 libgit2 libgit2sharp

我刚刚下载了libgit2sharp的nugget包.我发现即使是基本的操作也很难.

我有一个现有的git仓库(远程和本地).我只需要在它发生时提交新的更改并将其推送到远程.

我有下面的代码来解释我做了什么.

string path = @"working direcory path(local)";
Repository repo = new Repository(path);
repo.Commit("commit done for ...");

Remote remote = repo.Network.Remotes["origin"];          
var credentials = new UsernamePasswordCredentials {Username = "*******", Password = "******"};
var options = new PushOptions();
options.Credentials = credentials;
var pushRefSpec = @"refs/heads/master";                      
repo.Network.Push(remote, pushRefSpec, options, null, "push done...");
Run Code Online (Sandbox Code Playgroud)

我应该在哪里指定远程网址?这也是做这些操作(提交和推送)的正确方法吗?

谢谢

use*_*196 9

public void StageChanges() {
    try {
        RepositoryStatus status = repo.Index.RetrieveStatus();
        List<string> filePaths = status.Modified.Select(mods => mods.FilePath).ToList();
        repo.Index.Stage(filePaths);
    }
    catch (Exception ex) {
        Console.WriteLine("Exception:RepoActions:StageChanges " + ex.Message);
    }
}

public void CommitChanges() {
    try {

        repo.Commit("updating files..", new Signature(username, email, DateTimeOffset.Now),
            new Signature(username, email, DateTimeOffset.Now));
    }
    catch (Exception e) {
        Console.WriteLine("Exception:RepoActions:CommitChanges " + e.Message);
    }
}

public void PushChanges() {
    try {
        var remote = repo.Network.Remotes["origin"];
        var options = new PushOptions();
        var credentials = new UsernamePasswordCredentials { Username = username, Password = password };
        options.Credentials = credentials;
        var pushRefSpec = @"refs/heads/master";
        repo.Network.Push(remote, pushRefSpec, options, new Signature(username, email, DateTimeOffset.Now),
            "pushed changes");
    }
    catch (Exception e) {
        Console.WriteLine("Exception:RepoActions:PushChanges " + e.Message);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • LibGit2Sharp 版本 `0.26.2` 中的重大更改:```var status = repo.RetrieveStatus(); var filePaths = status.Modified.Select(mods =&gt; mods.FilePath).ToList(); foreach (var filePath in filePaths) { repo.Index.Add(filePath); repo.Index.Write(); }``` (9认同)