简体   繁体   English

无法使用 LibGit2Sharp 将本地目录推送到 GitHub

[英]Unable to push a local directory to GitHub using LibGit2Sharp

I need to push a local folder to a Git repository.我需要将本地文件夹推送到 Git 存储库。 Following is what I have tried till now:以下是我到目前为止所尝试的:

public static async Task CommitAllChanges(string message,string filePath, string cloneUrl)
    {
        try
        {
            var _folder = new DirectoryInfo(filePath);
            string path = LibGit2Sharp.Repository.Init(_folder.FullName);
            using (var repo = new LibGit2Sharp.Repository(path))
            {                    
                var files = _folder.GetFiles("*", SearchOption.AllDirectories).Select(f => f.FullName);
                Commands.Stage(repo, "*");

                repo.Commit(message, new LibGit2Sharp.Signature("sormita", "sormita@gmail.com", DateTimeOffset.Now),
                     new LibGit2Sharp.Signature("sormita", "sormita@gmail.com", DateTimeOffset.Now));

                //push files                
                string name = "origin";
                repo.Network.Remotes.Add(name, cloneUrl);
                var remote = repo.Network.Remotes.FirstOrDefault(r => r.Name == name);

                var options = new PushOptions
                {
                    CredentialsProvider = (_url, _user, _cred) =>
                        new UsernamePasswordCredentials { Username = "email", Password = "password" }
                };

                string pushRefSpec = @"refs/heads/master";

                repo.Network.Push(remote, pushRefSpec, options);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        
        
    }

I am able to push but no files are shown in the repository in GitHub.我可以推送,但 GitHub 的存储库中没有显示任何文件。

The above code will successfully push a directory to Github repository.上面的代码将成功将目录推送到 Github 存储库。

How can I use GitHub PAT token for this Push?如何使用 GitHub PAT 令牌进行此推送?

Set that as your password to the credentials provider.将其设置为凭据提供程序的密码。 A PAT is a password. PAT 是密码。

And if you've used your actual password in your question, please change it immediately .如果您在问题中使用了实际密码,请立即更改

Please refer to my question code details for full implementation of how to push a local directory to GitHub repository.有关如何将本地目录推送到 GitHub 存储库的完整实现,请参阅我的问题代码详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM