Switch / Checkout Branch 不工作
Switch / Checkout Branch ist not working
我使用的是 0.19 版
我有一个名为 'dev'
的远程分支
克隆后我想切换到这个分支。
我发现了一些对分支执行更新的代码。但对我来说它不起作用。
我也尝试在此之后运行结帐,但也不起作用。
在代码后查看 git 日志时,我看到了 master 分支的变更集。但是本地分支名称是创建分支的给定名称的名称(例如"dev")
我做错了什么?
private static Branch SwitchBranch(Repository repo, RepositoryProperties properties)
{
string branchname = properties.Branch;
Branch result = null;
if (!string.IsNullOrWhiteSpace(properties.Branch))
{
Branch remote = null;
foreach (var branch in repo.Branches)
{
if (string.Equals(branch.Name, "origin/" + branchname))
{
remote = branch;
break;
}
}
string localBranchName = properties.Branch;
Branch localbranch = repo.CreateBranch(localBranchName);
Branch updatedBranch = repo.Branches.Update(localbranch,
b =>
{
b.TrackedBranch = remote.CanonicalName;
});
repo.Checkout(updatedBranch);
result = updatedBranch;
}
return result;
}
您正在使用的 CreateBranch()
重载的 xml 文档说明 "Creates a branch with the specified name. This branch will point at the commit pointed at by Repository.Head
".
根据您的问题,您似乎希望此分支也指向与远程跟踪分支相同的提交。
因此,我建议您按如下方式更改代码:
Branch localbranch = repo.CreateBranch(localBranchName, remote.Tip);
请注意,本地分支只能创建一次。所以你第二次会出错。至少,我做到了。
Branch localbranch = repo.Branches.FirstOrDefault(x => !x.IsRemote && x.FriendlyName.Equals(localBranchName));
if (localbranch == null)
{
localbranch = repo.CreateBranch(localBranchName, remote.Tip);
}
Branch updatedBranch = repo.Branches.Update(localbranch,
b =>
{
b.TrackedBranch = remote.CanonicalName;
});
repo.Checkout(updatedBranch);
我使用的是 0.19 版 我有一个名为 'dev'
的远程分支克隆后我想切换到这个分支。 我发现了一些对分支执行更新的代码。但对我来说它不起作用。 我也尝试在此之后运行结帐,但也不起作用。
在代码后查看 git 日志时,我看到了 master 分支的变更集。但是本地分支名称是创建分支的给定名称的名称(例如"dev")
我做错了什么?
private static Branch SwitchBranch(Repository repo, RepositoryProperties properties)
{
string branchname = properties.Branch;
Branch result = null;
if (!string.IsNullOrWhiteSpace(properties.Branch))
{
Branch remote = null;
foreach (var branch in repo.Branches)
{
if (string.Equals(branch.Name, "origin/" + branchname))
{
remote = branch;
break;
}
}
string localBranchName = properties.Branch;
Branch localbranch = repo.CreateBranch(localBranchName);
Branch updatedBranch = repo.Branches.Update(localbranch,
b =>
{
b.TrackedBranch = remote.CanonicalName;
});
repo.Checkout(updatedBranch);
result = updatedBranch;
}
return result;
}
您正在使用的 CreateBranch()
重载的 xml 文档说明 "Creates a branch with the specified name. This branch will point at the commit pointed at by Repository.Head
".
根据您的问题,您似乎希望此分支也指向与远程跟踪分支相同的提交。
因此,我建议您按如下方式更改代码:
Branch localbranch = repo.CreateBranch(localBranchName, remote.Tip);
请注意,本地分支只能创建一次。所以你第二次会出错。至少,我做到了。
Branch localbranch = repo.Branches.FirstOrDefault(x => !x.IsRemote && x.FriendlyName.Equals(localBranchName));
if (localbranch == null)
{
localbranch = repo.CreateBranch(localBranchName, remote.Tip);
}
Branch updatedBranch = repo.Branches.Update(localbranch,
b =>
{
b.TrackedBranch = remote.CanonicalName;
});
repo.Checkout(updatedBranch);