Search code examples
libgit2sharp

Checkout a file to previous commit using libgit2sharp


i have committed a file. now i want to revert the file to one of the previous commit as we do $git checkout sha filepath. i want to realize this command with libgit2sharp. Can any body tell me the example code.


Solution

  • public void RevertToSpecificVersion() {

            using (var repo = new Repository(@"C:\Users\insiv1\source\GitRevert"))
            {
    
                foreach (LogEntry entry in repo.Commits.QueryBy("ArchitectureDocs.txt").ToList())
                {
                    if (entry.Commit.Id.ToString().Equals("25c20851cfef37492fd008b969408dc730ae7b6b"))
                    {
                        Commands.Checkout(repo, entry.Commit);
                    }
                }
            }
        }