public void SetRemoteBranch(GitBranchInfo localBranch, string remoteName = "origin")
 {
     using (var repository = GetRepository())
     {
         Remote remote = repository.Network.Remotes[remoteName];
         var branch = GetLib2GitBranch(localBranch);
         repository.Branches.Update(branch,
             b => b.Remote = remote.Name,
             b => b.UpstreamBranch = localBranch.CanonicalName);
     }
 }
        public GitActionResult<GitBranchInfo> Checkout(GitBranchInfo info, bool force = false)
        {
            using (var repository = GetRepository())
            {
                var result = new GitActionResult<GitBranchInfo>();

                CheckoutOptions options = new CheckoutOptions();
                var branch = repository.Branches.FirstOrDefault(
                        x => string.Equals(x.CanonicalName, info.CanonicalName, StringComparison.OrdinalIgnoreCase));

                if (force)
                {
                    options.CheckoutModifiers = CheckoutModifiers.Force;

                }
                try
                {
                    var checkoutBranch = repository.Checkout(branch, options);
                    if (checkoutBranch != null)
                    {
                        result.Item = new GitBranchInfo
                        {
                            CanonicalName = checkoutBranch.CanonicalName,
                            RemoteName = checkoutBranch.Remote?.Name,
                            Name = checkoutBranch.FriendlyName,
                            IsRemote = checkoutBranch.IsRemote
                        };
                        result.Succeeded = true;
                        return result;
                    }
                    result.Succeeded = false;
                }
                catch (CheckoutConflictException conflict)
                {
                    result.Succeeded = false;
                    result.ErrorMessage = conflict.Message;
                }

                return result;
            }
        }
 private Branch GetLib2GitBranch(GitBranchInfo info)
 {
     using (var repository = GetRepository())
     {
         return
             repository.Branches.FirstOrDefault(
                 x => string.Equals(x.CanonicalName, info.CanonicalName, StringComparison.OrdinalIgnoreCase));
     }
 }
 public async Task<GitActionResult<GitBranchInfo>> CheckoutAsync(GitBranchInfo info, bool force = false)
 {
     return await Task.Run(() => Checkout(info, force));
 }