Search code examples
git.net-corelibgit2sharpaws-codecommit

Libgit2sharp, Pushing a specific branch to new remote


I am writing a dotnet api to clone from github, then push a specific branch to AWS codecommit using the Libgit2sharp library.

  • I have two branches in the gitHubRepo, origin/release and origin/refactor.
  • origin/release is the DEFAULT git branch
  • I want to clone the gitHubRepo, and push the origin/refactor branch to codecommit

Problem: I can't seem to get it to push my specified branch to codecommit, it always seems to push what is set to HEAD. I don't know how to change the HEAD, or even know if this is the issue.

Code Snippet:

var baseRefBranch = gitHubRepo.Branches[$"origin/refactor"];
if (baseRefBranch == null) throw new Exception("refactor branch was null...");

logger.LogInformation($"Checking out branch: {baseRefBranch.CanonicalName}");
Commands.Checkout(gitHubRepo, baseRefBranch);

logger.LogInformation($"Update branch {baseRefBranch.CanonicalName}, set remote name {codeCommitRemote.Name}, set upstreamBranch to {baseRefBranch.CanonicalName}");
        gitHubRepo.Branches.Update(baseRefBranch,
            b => b.Remote = codeCommitRemote.Name,
            b => b.UpstreamBranch = baseRefBranch.CanonicalName);

var refSpec = $"+{baseRefBranch.CanonicalName}";
logger.LogInformation($"Trying to push to remote: {codeCommitRemote.Name}, refSpec: {refSpec}");
gitHubRepo.Network.Push(codeCommitRemote, refSpec, pushOptions);

Any help appreciated... Cheers


Solution

  • Try and use a complete refsepc when pushing

    The format of a <refspec> parameter is an optional plus +, followed by the source object <src>, followed by a colon :, followed by the destination ref <dst>.

    In your case:

    +refs/heads/refactor:refs/heads/refactor