質問

I need my application to do two simple operations with Git.
1) Clone a remote repository;
2) Perform updates from the remote repository on regular basis.

I am absolutely at a lost how to do it with libgit2sharp. The API is quite complicated.

Can anybody help me please?

役に立ちましたか?

解決

1) Clone a branch from remote repository;

// 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 ...
}

2) Perform updates from the remote branch on regular basis

// "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);

Update

...files are not updating. May it be because Fetch downloads changes, but not applies them to working copy?

Indeed, a Fetch() call doesn't update the working directory. It "downloads" the changes from upstream and update the references of the remote tracking branches.

If you want to update the content of the working directory, then you'll have to either

  • Replace the content of your working directory by checking out (ie. repo.Checkout()) the remote tracking branch that has been fetched
  • Merge the remote tracking branch you're interested in (once it's been fetched) into your current HEAD (ie. repo.Merge()`)
  • Replace the call to Fetch() with a call to repo.Network.Pull() which will perform the fetch and the merge

Note: You'll have to cope with conflicts during the merge is some changes you've locally performed prevent the merge from applying cleanly.

You can peek at the test suites to get a better understanding of those methods and their options:

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top