I want to create a local branch from master, commit stuff into it and then push the changes to the newly created branch. This is what I did:
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)
This is what happens: Cloning succeedes. Creating that branch also works. Pushing that branch to the remote repository also work. BUT: the Commit is not not happening to my created branch but to the master branch which I cloned at the start. So all I am missing is one of the following:
- How to set my created branch as "active" OR
- How to inform the Commit which branch to use
None of the examples/docs from lib2gitsharp or even intellisense give me a clue what to do
CodePudding user response:
When you work with git locally in command line: after creating a branch, you need to check it out.
$ git checkout -b iss53
Switched to a new branch "iss53"
This is shorthand for:
$ git branch iss53
$ git checkout iss53
https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging
So, after creating a branch in your code, you need to run a Checkout
command.
Commands.Checkout(repo, localBranch);