Skip to content

Instantly share code, notes, and snippets.

@smellymonk
Last active April 19, 2016 18:54
Show Gist options
  • Save smellymonk/a77b391fe7e8850ca7cc51a3b57d63a9 to your computer and use it in GitHub Desktop.
Save smellymonk/a77b391fe7e8850ca7cc51a3b57d63a9 to your computer and use it in GitHub Desktop.
function createBranch(repo, newBranchName, remoteName, remoteBranch) {
var remoteRefBase = 'refs/remotes/' + remoteName;
var remoteRefName = remoteRefBase + '/' + remoteBranch;
var remoteRefHead = remoteRefBase + '/' + 'HEAD';
return NodeGit.Reference.symbolicCreate(repo, remoteRefHead, remoteRefName, 1, 'Setting HEAD')
.then(function () {
return repo.getReference(remoteRefName);
})
.then(function (remoteRef) {
// Create new branch based on the remote ref
return repo.createBranch(newBranchName, remoteRef.target());
})
.then(function (branchRef) {
// checkout branch to update working directory
return repo.checkoutBranch(branchRef);
});
});
}
// example
repo.fetchAll().then(function () {
return createBranch(repo, 'new', 'new', 'master');
}).then(function () {
return repo.mergeBranches('new', 'new/master');
}).then(function () {
return repo.checkoutBranch('master');
}).then(function () {
return repo.mergeBranches('master', 'new');
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment