Skip to content

Instantly share code, notes, and snippets.

@copygirl
Last active August 29, 2015 14:23
Show Gist options
  • Save copygirl/64826fc93b5c33db6621 to your computer and use it in GitHub Desktop.
Save copygirl/64826fc93b5c33db6621 to your computer and use it in GitHub Desktop.
var cloneUrl = ...;
var branchName = ...;
var git = Git.Init().SetDirectory(Location).Call();
Repository = git.GetRepository();
// Original code in question works, is shorter,
// but this is most likely the "proper" way to do it.
var config = Repository.GetConfig();
RemoteConfig remoteConfig = new RemoteConfig(config, "origin");
remoteConfig.AddURI(new URIish(cloneUrl));
// May use * instead of branch name to fetch all branches.
// Same as config.SetString("remote", "origin", "fetch", ...);
remoteConfig.AddFetchRefSpec(new RefSpec(
"+refs/heads/" + Settings.Branch +
":refs/remotes/origin/" + Settings.Branch));
remoteConfig.Update(config);
config.Save();
git.Fetch().Call();
git.BranchCreate().SetName(branchName).SetStartPoint("origin/" + branchName)
.SetUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).Call();
git.Checkout().SetName(branchName).Call();
// To update the branch:
git.Fetch().Call();
git.Reset().SetRef("origin/" + branchName).Call();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment