Hrishikesh Singh
Hrishikesh Singh

Reputation: 11

Checkout using Libgit2sharp on an empty repo

By using git checkout -b <branchname> I am able to create a new branch in an empty repo and the start committing files on that branch. I am not able to achieve this via libgit2sharp. By using repo.Checkout(branchName) it throws following error:

LibGit2Sharp.NotFoundException: No valid git object identified by exists in the repository.

Upvotes: 1

Views: 883

Answers (1)

SushiHangover
SushiHangover

Reputation: 74164

The current version of the native libgit2 library used by libgit2sharp requires a HEAD to exist as it is used during the branch creation. Using a empty(null) committish is valid in the official git version and thus creating a new branch and checking it out works fine on a completely bare repo. Maybe this is covered in the next release and/or an already known bug.

But either way, just create an initial commit that is empty in content and it works:

using System;
using System.IO;
using LibGit2Sharp;

namespace stackoverflow
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            var rPath = Path.Combine (Path.GetTempPath (), "StackOverFlow");
            var rootedPath = Repository.Init (rPath, false);
            var repo = new Repository (rootedPath);
            repo.Commit ("Initial Commit");
            repo.CreateBranch ("EmptyBranch");
            repo.Checkout ("EmptyBranch");
        }
    }
}

Upvotes: 1

Related Questions