使用libgit2sharp从远程存储库获取更新

Ale*_* K. 3 libgit2sharp

我需要我的应用程序用Git做两个简单的操作.
1)克隆远程存储库;
2)定期从远程存储库执行更新.

我绝对不知道如何用libgit2sharp做到这一点.API非常复杂.

有人能帮帮我吗?

nul*_*ken 9

1)从远程存储库克隆分支;

// Url of the remote repository to clone
string url = "https://github.com/path/to_repo.git";

// Location on the disk where the local repository should be cloned
string workingDirectory = "D:\\projects\to_repo";

// Perform the initial clone
string repoPath = Repository.Clone(url, workingDirectory);

using (var repo = new Repository(repoPath))
{
    ... do stuff ...
}
Run Code Online (Sandbox Code Playgroud)

2)定期从远程分支执行更新

// "origin" is the default name given by a Clone operation
// to the created remote
var remote = repo.Network.Remotes["origin");

// Retrieve the changes from the remote repository
// (eg. new commits that have been pushed by other contributors)
repo.Network.Fetch(remote);
Run Code Online (Sandbox Code Playgroud)

更新

...文件没有更新.可能是因为Fetch下载了更改,但没有将它们应用于工作副本?

实际上,Fetch()调用不会更新工作目录.它从上游"下载"更改并更新远程跟踪分支的引用.

如果要更新工作目录的内容,则必须要么

  • 通过签出(即repo.Checkout())已获取的远程跟踪分支来替换工作目录的内容
  • 将您感兴趣的远程跟踪分支(一旦被提取)合并到当前的HEAD中(即repo.Merge()`)
  • 将调用替换为将执行获取和合并Fetch()的调用repo.Network.Pull()

注意:您必须在合并期间处理冲突,因为您在本地执行的某些更改会阻止合并干净地应用.

您可以查看测试套件,以便更好地了解这些方法及其选项: