user3236635 user3236635 - 10 months ago 87
C# Question

Pushing to bitbucket with libgit2sharp

I'm trying to use libgit2sharp to push to my repo on bitbucket. I'm trying to do this:

repo.Network.Push(repo.Branches[branchName], pushOptions);

Everything seems fine, no exceptions are thrown and I get no errors in the callback, but when I check on bitbucket none of my commits are there. Other methods seem to work fine (ie I can create a new branch on bitbucket and then use libgit2sharp to fetch and see that I now have that branch locally). Is there anything that I might be missing here?


Tried to just make a small sample program to see if I can get this working no go. I don't know if my code will help but here it is:

class Program
static void Main(string[] args)
PushOptions options = new PushOptions();
Credentials creds = new Credentials();
creds.Username = "username";
creds.Password = "password";
options.Credentials = creds;
options.OnPackBuilderProgress = Program.packBuilderProgressHandler;
options.OnPushTransferProgress = Program.pushTransferProgressHandler;
options.OnPushStatusError = Program.pushStatusErrorHandler;
Repository repo = new Repository("E:/Ohad/Work/libgitTest");

repo.Network.Push(repo.Branches["origin/master"], options);

Console.WriteLine("Press enter to close...");

public static bool packBuilderProgressHandler(PackBuilderStage stage, int current, int total)
Console.Out.WriteLine("packBuilder => " + current + " / " + total);
return true;
public static bool pushTransferProgressHandler(int current, int total, long bytes)
Console.Out.WriteLine("pushTransfer => " + current + " / " + total + " , " + bytes);
return true;
public static void pushStatusErrorHandler(PushStatusError error)
Console.Out.WriteLine("error => " + error.Message);

Just make a new repo on bitbucket and add the above code (changing the stuff thats hard coded) and it should be reproducible. I just made a random change, added and commited it and then used the program to try to push to bitbucket. The output that I am getting from the above is:

pushTransfer => 0 / 0 , 12
pushTransfer => 0 / 0 , 32
Press enter to close...

The 0/0 looks suspicious to me but I don't know what I'm doing wrong =/. Thanks for any help!!

edit 2:
I just added this:

repo.Branches.Update(repo.Head, delegate(BranchUpdater updater)
updater.Remote = "origin";
updater.UpstreamBranch= repo.Head.CanonicalName;

to before when I push and it fixed the issue. Not sure exactly why but I'll take it =).

Answer Source

I think that you're willing to push your local master branch rather than the remote tracking one.

repo.Network.Push(repo.Branches["master"], options);


*The branch 'master' ("refs/heads/master") that you are trying to push does not track an upstream branch. *

Provided you have no "origin" remote, the following should work.

Remote remote = localRepo.Network.Remotes.Add("origin", url);

repo.Branches.Update(repo.Head,                              -> (A)
    b => b.Remote = remote.Name,                             -> (B)
    b => b.UpstreamBranch = repo.Head.CanonicalName);        -> (C)

The code above should be read as "The branch pointed at by the HEAD (A) on this local repository will be, by default, configured to track a branch bearing the same name (C) in the distant repository identified by this remote (B)."