简体   繁体   English

如何使用 libgit2sharp 正确取消暂存文件

[英]How to Properly Unstage File With libgit2sharp

I'm trying my best to figure out how to unstage a file with libgit2sharp.我正在尽力弄清楚如何使用 libgit2sharp 取消暂存文件。

My current approach is to to remove the file from the index, but that seems to delete the file instead of unstaging it.我目前的方法是从索引中删除文件,但这似乎是删除文件而不是取消暂存。

        public bool Unstage(params string[] filePaths)
    {
        using (var repo = LocalRepo)
        {
            try
            {
                foreach (var filePath in filePaths)
                {
                    repo.Index.Remove(filePath);
                    repo.Index.Write();
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        return true;
    }

I've tried to do a soft reset as well, but I can't figure out how to pass in the filename or use the commitish parameter in one of the reset function overloads.我也尝试过软重置,但我不知道如何在重置函数重载之一中传递文件名或使用 commitish 参数。

Been trying to follow this post: Why are there two ways to unstage a file in Git?一直在尝试关注这篇文章: 为什么有两种方法可以在 Git 中取消暂存文件? , but I can't seem to figure out how to recreate that approach in libgit2sharp. ,但我似乎无法弄清楚如何在 libgit2sharp 中重新创建该方法。

After searching for quite some time, I finally found out that libgit2sharp has a Commands static class with almost every command you would need built in and ended up doing it like this:经过一段时间的搜索,我终于发现 libgit2sharp 有一个 Commands 静态类,其中包含几乎所有您需要内置的命令,并最终这样做:

      public bool Unstage(params string[] filePaths)
    {
        using (var repo = LocalRepo)
        {
            try
            {
                foreach (var filePath in filePaths)
                {
                   Commands.Unstage(repo, filePath);
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        return true;
    }

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

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