LibGitSharp:结帐远程分支 [英] LibGitSharp: Checkout Remote Branch

查看:103
本文介绍了LibGitSharp:结帐远程分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过LibGitSharp检出Remotebranch。在git本身中,您可以使用以下命令:

  git获取来源
git checkout -b测试来源/测试

在新版本中只是:

  git fetch 
git checkout test

所以我尝试了此代码:

  repo.Fetch( origin); 
repo.Checkout( origin / + Name);

Fetch and Checkout运行没有问题,但是没有Remotebranch的副本。



有人想出其他方法签出遥控器吗?



我的替代方法是在存储库中创建分支并将其推送到Remote:

  Branch newBranch = repo.Branches.Add(Name,repo.Branches [ master]。 Commits.First()); 
repo.Network.Push(newBranch);

但是我得到了这个例外:


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


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



编辑:我没有正确解释它,因此我尝试更好地描述它在程序中的访存功能。提取命令已正确执行。现在,如果我使用checkout命令,则应该创建一个Remotebranch的本地分支,但是没有。我还尝试了repo.Checkout(name),不带 origin /,但它引发了一个异常:存储库中不存在由'...'标识的有效git对象。

解决方案

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



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



下面的示例应演示如何操作

  const string localBranchName = theBranch; 

//本地分支尚不存在
Assert.Null(repo.Branches [localBranchName]);

//让我们在远程跟踪分支上获取引用...
const string trackedBranchName = origin / theBranch;
分支trackedBranch = repo.Branches [trackedBranchName];

// ...并创建指向同一Commit
的本地分支Branch branch = repo.CreateBranch(localBranchName,trackedBranch.Tip);

//本地分支未配置为跟踪任何东西
Assert.False(branch.IsTracking);

//因此,我们将本地分支配置为跟踪远程分支。
Branch UpdatedBranch = repo.Branches.Update(branch,
b => b.T​​rackedBranch = trackedBranch.CanonicalName);

// Bam!完成。
Assert.True(updatedBranch.IsTracking);
Assert.Equal(trackedBranchName,UpdatedBranch.TrackedBranch.Name);

注意:更多示例可在 BranchFixture.cs 测试套件。 p>

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 newer Versions it is just:

git fetch
git checkout test

So i tried this Code:

repo.Fetch("origin");
repo.Checkout("origin/" + Name);

The Fetch and Checkout runs without any problems but there is no copy of the Remotebranch.

Does anyone have an idea to Checkout the Remote with other methods?

My alternative would be to create the Branch in the Repository and push it into Remote:

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

but i get this Exception:

The branch 'Test1' ("refs/heads/Test1") that you are trying to push does not track an upstream branch.

maybe i could set the Branch to an upstream Branch, but i don't know how.

Edit: I haven't explained it properly, so I try to describe it better what the Fetch and Checkout does in my program. The Fetch command is performed correctly. Now if i use the checkout command it should be create an local Branch of the Remotebranch, but it doesn't. I've also tried repo.Checkout(name), without "origin/" ,but it cast an Exception: No valid git object identified by '...' exists in the repository.

解决方案

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.

这篇关于LibGitSharp:结帐远程分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆