NodeGit:将分支指针移动到不同的提交而没有签出
在git中,我们可以使用以下命令来实现它:
git branch -f branch-name new-tip-commit
我们如何在nodegit中实现相同的function?
http://www.nodegit.org/api/
您可以尝试重新创build分支,即使已经存在,也会强制创build。
请参阅Repository.prototype.createBranch( lib/repository.js#L28-L39
) ,其中包括:
@param {bool} force Overwrite branch if it exists
你可以在examples/create-branch.js#L4-L16
看到一个例子:
var nodegit = require("../"); var path = require("path"); nodegit.Repository.open(path.resolve(__dirname, "../.git")) .then(function(repo) { // Create a new branch on head return repo.getHeadCommit() .then(function(commit) { return repo.createBranch( "new-branch", commit, 0, repo.defaultSignature(), "Created new-branch on HEAD"); }); }).done(function() { console.log("All done!"); });
如果在该示例中将0
replace为1
,那么即使该分支已经存在,也会强制创build该分支,从而有效地将其HEAD重置为新的提交。