LibGitSharp: Checkout Remote Branch

前端 未结 1 577
一生所求
一生所求 2021-01-12 22:27

i try to Checkout a Remotebranch via LibGitSharp. In git itself you use this comands:

git fetch origin
git checkout -b test origin/test

in

相关标签:
1条回答
  • 2021-01-12 22:53

    If I correctly understand your question, you're willing to create a local branch which would be configured to track the fetched remote tracking branch.

    In other words, once you fetch a repository, your references contains remote tracking branches (eg. origin/theBranch) and you'd like to create a local branch bearing the same name (eg. theBranch).

    The following example should demonstrate how to do this

    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);
    

    Note: More examples can be found in the BranchFixture.cs test suite.

    0 讨论(0)
提交回复
热议问题