我想從 master 創建一個本地分支,將內容提交到其中,然后將更改推送到新創建的分支。這就是我所做的:
string branchname="jodel";
string _repoPath="C:\\gitstuff";
var _author=new Signatur("bot","@bot", DataTimeOffset.Now);
// Clone Repository
Repository.Clone(_settings.Config.Git.Url, _repoPath, new CloneOptions { BranchName="master"});
// Create Branch:
var repo=new Repository(_repoPath);
Remote remote = repo.Network.Remotes["origin"];
var localBranch = repo.CreateBranch(branchname);
repo.Branches.Update(localBranch,
b => b.Remote = remote.Name,
b => b.UpstreamBranch = localBranch.CanonicalName);
// Commit
// Create dummy file:
File.WriteAllText(_repoPath "/" Guid.NewGuid().ToString() ".txt", "Hallo, Welt");
// Add to Index:
var status=repo.RetrieveStatus();
foreach (var file in status.Untracked) repo.Index.Add(file.FilePath);
repo.Index.Write();
// do Commit
repo.Commit("hi there", _author, _author);
// Push
var pushOptions=new PushOptions { CredentialsProvider=...};
repo.Network.Push(repo.Branches[branchname],options)
這就是發生的事情:克隆成功。創建該分支也有效。將該分支推送到遠程存盤庫也有效。但是:提交不會發生在我創建的分支上,而是發生在我一開始克隆的主分支上。所以我所缺少的只是以下之一:
- 如何將我創建的分支設定為“活動”或
- 如何通知 Commit 使用哪個分支
來自 lib2gitsharp 甚至智能感知的示例/檔案都沒有給我一個線索
uj5u.com熱心網友回復:
當你在命令列本地使用 git 時:創建一個分支后,你需要檢查它。
$ git checkout -b iss53
切換到新分支“iss53”
這是以下的簡寫:
$ git 分支 iss53
$ git 結帳 iss53
https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging
因此,在您的代碼中創建分支后,您需要運行一個Checkout命令。
Commands.Checkout(repo, localBranch);
https://github.com/libgit2/libgit2sharp/wiki/git-checkout
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/401747.html
標籤:C# lib2gitsharp
上一篇:如何將文字物件陣列傳遞給函式?
下一篇:C#二叉樹,最大值和最小值
