LibGitSharp:Checkout Remote Branch

use*_*236 5 git c#-4.0 libgit2sharp

我尝试通过LibGitSharp结帐Remotecranch.在git中你使用这个命令:

git fetch origin
git checkout -b test origin/test
Run Code Online (Sandbox Code Playgroud)

在较新的版本中它只是:

git fetch
git checkout test
Run Code Online (Sandbox Code Playgroud)

所以我尝试了这个代码:

repo.Fetch("origin");
repo.Checkout("origin/" + Name);
Run Code Online (Sandbox Code Playgroud)

Fetch和Checkout运行没有任何问题,但没有Remotebranch的副本.

有没有人有想法用其他方法检查远程?

我的另一种方法是在Repository中创建Branch并将其推送到Remote:

Branch newBranch = repo.Branches.Add(Name, repo.Branches["master"].Commits.First());
repo.Network.Push(newBranch);
Run Code Online (Sandbox Code Playgroud)

但我得到这个例外:

您尝试推送的分支"Test1"("refs/heads/Test1")不会跟踪上游分支.

也许我可以将Branch设置为上游分支,但我不知道如何.

编辑:我没有正确解释,所以我试着更好地描述Fetch和Checkout在我的程序中的作用.Fetch命令正确执行.现在,如果我使用checkout命令,它应该创建Remotebranch的本地分支,但它不会.我也试过repo.Checkout(name),没有"origin /",但是它会抛出异常:No valid git object identified by '...' exists in the repository.

nul*_*ken 22

如果我正确理解您的问题,您愿意创建一个本地分支,该分支将配置为跟踪获取的远程跟踪分支.

换句话说,一旦获取存储库,您的引用就包含远程跟踪分支(例如.origin/theBranch),并且您希望创建一个具有相同名称的本地分支(例如theBranch).

以下示例应演示如何执行此操作

const string localBranchName = "theBranch";

// The local branch doesn't exist yet
Assert.Null(repo.Branches[localBranchName]);

// Let's get a reference on the remote tracking branch...
const string trackedBranchName = "origin/theBranch";
Branch trackedBranch = repo.Branches[trackedBranchName];

// ...and create a local branch pointing at the same Commit
Branch branch = repo.CreateBranch(localBranchName, trackedBranch.Tip);

// The local branch is not configured to track anything
Assert.False(branch.IsTracking);

// So, let's configure the local branch to track the remote one.
Branch updatedBranch = repo.Branches.Update(branch,
    b => b.TrackedBranch = trackedBranch.CanonicalName);

// Bam! It's done.
Assert.True(updatedBranch.IsTracking);
Assert.Equal(trackedBranchName, updatedBranch.TrackedBranch.Name);
Run Code Online (Sandbox Code Playgroud)

注意:可以在BranchFixture.cs测试套件中找到更多示例.