libgit2sharp中的孤立分支

5

如何在libgit2sharp中创建孤立分支?

我只能找到创建指向提交的分支的方法。
我正在寻找类似于以下命令的效果:

git checkout --orphan BRANCH_NAME  
1个回答

6

git checkout --orphan BRANCH_NAME 实际上将 HEAD 移动到未创建的分支 BRANCH_NAME,而不更改工作目录或索引。

您可以使用LibGit2Sharp执行类似的操作,通过更新 HEAD 引用的目标来完成,方法为 repo.Refs.UpdateTarget()

以下测试演示了这一点。

[Fact]
public void CanCreateAnUnbornBranch()
{
    string path = CloneStandardTestRepo();
    using (var repo = new Repository(path))
    {
        // No branch named orphan
        Assert.Null(repo.Branches["orphan"]);

        // HEAD doesn't point to an unborn branch
        Assert.False(repo.Info.IsHeadUnborn);

        // Let's move the HEAD to this branch to be created
        repo.Refs.UpdateTarget("HEAD", "refs/heads/orphan");
        Assert.True(repo.Info.IsHeadUnborn);

        // The branch still doesn't exist
        Assert.Null(repo.Branches["orphan"]);

        // Create a commit against HEAD
        var signature = new Signature("Me", "me@there.com", DateTimeOffset.Now);
        Commit c = repo.Commit("New initial root commit", signature, signature);

        // Ensure this commit has no parent
        Assert.Equal(0, c.Parents.Count());

        // The branch now exists...
        Branch orphan = repo.Branches["orphan"];
        Assert.NotNull(orphan);

        // ...and points to that newly created commit
        Assert.Equal(c, orphan.Tip);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接