2014-04-10 8 views
6

Voglio creare ed eliminare un ramo su git usando libgit2sharp. Sono venuto con questo codice, ma genera un errore a repo.Network.Push(localBranch, pushOptions);Come utilizzare libgit2sharp per creare un nuovo ramo da locale a remoto?

using (var repo = new Repository(GIT_PATH)) 
{ 
    var branch = repo.CreateBranch(branchName); 

    var localBranch = repo.Branches[branchName]; 

    //repo.Index.Stage(GIT_PATH); 
    repo.Checkout(localBranch); 
    repo.Commit("Commiting at " + DateTime.Now); 

    var pushOptions = new PushOptions() { Credentials = credentials }; 

    repo.Network.Push(localBranch, pushOptions); // error 

    branch = repo.Branches["origin/master"]; 
    repo.Network.Push(branch, pushOptions); 
} 

Il messaggio di errore è The branch 'buggy-3' ("refs/heads/buggy-3") that you are trying to push does not track an upstream branch.

Ho provato a cercare questo errore su internet, ma nessuna soluzione che ho trovato potrebbe risolvere il problema. È possibile farlo usando libgit2sharp?

risposta

14

È necessario associare il ramo locale a un telecomando a cui si desidera inviare push.

Per esempio, dato un "origin" a distanza già esistente:

Remote remote = repo.Network.Remotes["origin"]; 

// The local branch "buggy-3" will track a branch also named "buggy-3" 
// in the repository pointed at by "origin" 

repo.Branches.Update(localBranch, 
    b => b.Remote = remote.Name, 
    b => b.UpstreamBranch = localBranch.CanonicalName); 

// Thus Push will know where to push this branch (eg. the remote) 
// and which branch it should target in the target repository 

repo.Network.Push(localBranch, pushOptions); 

// Do some stuff 
.... 

// One can call Push() again without having to configure the branch 
// as everything has already been persisted in the repository config file 
repo.Network.Push(localBranch, pushOptions); 

Nota ::Push() espone altra overloads che consentono di fornire dinamicamente tali informazioni senza memorizzarlo nella configurazione.

+0

Vedere anche questo ** [SO risposta] (http://stackoverflow.com/a/22617675/335418) ** che dovrebbe fornire ulteriori dettagli riguardanti la configurazione della diramazione – nulltoken

+0

Dov'è la differenza tra 'localRepo' e 'repo'? – BendEg

+0

@BendEg Si è verificato un errore. Buona pesca! Fisso. – nulltoken

Problemi correlati