I just downloaded the nugget package for libgit2sharp. I am finding it difficult to do even basic operations.
I have an existing git repo (both remote and local). I just need to commit new changes when it occurs and push it to the remote.
I have the code below to explain what I did.
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...");
Where should I specify remote url's ? also is this the right way of doing these operations(commit & push)?
Thanks
To push the commit from the local repo to your remote repositories, run git push -u remote-name branch-name where remote-name is the nickname the local repo uses for the remote repositories and branch-name is the name of the branch to push to the repository. You only have to use the -u option the first time you push.
When you're ready, click Commit or Commit and Push ( Ctrl+Alt+K ) to push the changes to the remote repository immediately after the commit. You will be able to review the current commit as well as all other commits before they are pushed to the remote.
NET code then LibGit2Sharp is your friend. It's the open source library used by the Visual Studio Tools for Git and gives you a very idiomatic way to talk to Git from C# or VB.NET.
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);
}
}
The remote already has an url.
If you wanted to change the url associated with remote named 'origin
', you would need to:
remove that remote:
repo.Network.Remotes.Remove("origin");
# you can check it with:
Assert.Null(repo.Network.Remotes["origin"]);
Assert.Empty(repo.Refs.FromGlob("refs/remotes/origin/*"));
create a new one (default refspec)
const string name = "origin";
const string url = "https://github.com/libgit2/libgit2sharp.git";
repo.Network.Remotes.Add(name, url);
# check it with:
Remote remote = repo.Network.Remotes[name];
Assert.NotNull(remote);
See more at LibGit2Sharp.Tests/RemoteFixture.cs
As updated in the comments by nulltoken, contributor to libgit2:
PR 803 has been merged.
This should allow some code such as
Remote updatedremote =
repo.Network.Remotes.Update(remote, r => r.Url = "http://yoururl");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With